如何将变量放在fopen中? [关闭]

I'm trying to save an image inside a folder. To get this folder I need to pass three directories: Images, id from the user, id from the gallery and the generated name for the image.

$directory = "../images/" . $id . "/" . $row[0] . "/" . $new_url;

I would like to pass the directory variable to the fopen, I tried this way but is not working.

$file = fopen($directory, 'wb');

Full code:

function uploadimg_func($base, $id){


    $binary=base64_decode($base);
    header('Content-Type: bitmap; charset=utf-8');

    $selectgallery = db_queries("SELECT id FROM `galleries` WHERE name = 'Fotos de perfil' AND user_id =  '".$id."' ");

    $row = mysqli_fetch_array($selectgallery);

    $new_url = rand(0, pow(10, 5)) . '_' . time() . '.' . "jpg";

    $directory = "../images/" . $id . "/" . $row[0] . "/" . $new_url;

    $file = fopen($directory, 'wb');

    fwrite($file, $binary);
    fclose($file);

    $createphoto = db_queries("INSERT INTO photos (`name`, `title`, `description`, `gallery_id`,`created_at`) 
    VALUES ('".$new_url."', '', '', '".$row[0]."', now())");

} 

Thanks.

Instead of:

$file = fopen($directory, 'wb');

Do:

$file = fopen($_SERVER['DOCUMENT_ROOT'] . $directory, 'wb');

Also, make sure that you have the correct folder and file permissions setup. The images path should be 0775. If something still goes wrong, check if the folders containing the image files exist....

It's working on my machine perfectly fine.