2015-06-17 31 views
0

我试图保存一个文件夹内的图像。要获得这个文件夹,我需要传递三个目录:图像,来自用户的ID,来自图库的ID和为图像生成的名称。如何将变量放在fopen中?

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

我想将目录变量传递给fopen,我试过这种方式,但没有工作。

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

全码:

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())"); 

} 

感谢。

+0

它给你一个错误? –

+0

我没有收到任何错误。 –

+0

通过$ id和$ row [0]引用的目录是否已经存在? fopen创建文件,但不包含缺少的目录。只是要清楚:你试图创建一个名为存储在$ new_url中的*文件*?那么你的变量名是误导性的 – Marged

回答

1

相反的:

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

务必:

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

此外,请确保您有正确的文件夹和文件的权限设置。图像路径应该是0775.如果仍有问题,请检查包含图像文件的文件夹是否存在....

它在我的机器上工作得很好。