2017-05-25 28 views
0

我有一个简单的多图像上传脚本,可以调整图像的大小以保持纵横比。调整大小工作正常。然而,我似乎无法将图像发送到正确的文件夹,即使语法正确。在上传时发送调整大小的图像的路径php

以下是我简而言之正在做:

如果文件输入“图像”不为空,然后创建“../company_images”内的新文件夹创建的文件夹的名称是uniqid由“$ photo_directory_name”变量定义。在为每个图像运行调整大小功能后,将调整大小的图像放入由“$ total_path”变量定义的上传文件夹中。

if(!empty($_FILES["Image"])){ 

$photo_directory_name = uniqid(rand(100, 1000));  
$photos_path = "company_images/" . $photo_directory_name;  
$directory = "../company_images/";  

if (!file_exists($directory . $photo_directory_name)) { 
$upload_dir = mkdir($directory . $photo_directory_name, 0777, TRUE); 
}else{ 
$upload_dir = $directory . $photo_directory_name; 
}  


function resize($width, $height){ 

    /* Get original image x y*/ 
    list($w, $h) = getimagesize($_FILES['Image']['tmp_name']); 
    /* calculate new image size with ratio */ 
    $ratio = max($width/$w, $height/$h); 
    $h = ceil($height/$ratio); 
    $x = ($w - $width/$ratio)/2; 
    $w = ceil($width/$ratio); 
    /* new file name */ 
    $path = $width.'x'.$height.'_'.$_FILES['Image']['name']; 

    $total_path = $directory . $photo_directory_name . $path; 


    /* read binary data from image file */ 
    $imgString = file_get_contents($_FILES['Image']['tmp_name']); 
    /* create image from string */ 
    $image = imagecreatefromstring($imgString); 
    $tmp = imagecreatetruecolor($width, $height); 
    imagecopyresampled($tmp, $image, 
    0, 0, 
    $x, 0, 
    $width, $height, 
    $w, $h); 
    /* Save image */ 
    switch ($_FILES['Image']['type']) { 
    case 'image/jpeg': 
     imagejpeg($tmp, $total_path, 100); 
     break; 
    case 'image/png': 
     imagepng($tmp, $total_path, 0); 
     break; 
    case 'image/gif': 
     imagegif($tmp, $total_path); 
     break; 
    default: 
     exit; 
     break; 
    } 
    return $total_path; 
    /* cleanup memory */ 
    imagedestroy($image); 
    imagedestroy($tmp); 
}  


$max_file_size = 1024*1000; // 1mb 
$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); 
// thumbnail sizes 
$sizes = array(1200 => 1000); 

if ($_SERVER['REQUEST_METHOD'] == 'POST' AND isset($_FILES['Image'])) { 
    if($_FILES['Image']['size'] < $max_file_size){ 

    // get file extension 
    $ext = strtolower(pathinfo($_FILES['Image']['name'], PATHINFO_EXTENSION)); 
    if (in_array($ext, $valid_exts)) { 


     /* resize image */ 
     foreach ($sizes as $w => $h) { 
     $files[] = resize($w, $h); 
     } 

    } else { 
    $response["message"] = 'photos_invalid_format'; 
    $errors++; 
    } 
    } else{ 
    $response["message"] = 'photos_file_too_large'; 
    $errors++; 
    } 
} 
echo $total_path; 

} 

任何帮助,非常感谢。由于

+0

所以调整大小的图像存储在错误的文件夹?哪个文件夹? – James

+0

如果没有保存,它可能是一个权限/访问被拒绝的错误。在许多系统上,PHP现在以“nobody”身份运行,而不是apache/ftp/WWW用户。 –

+0

@詹姆斯是的,这是正确的。它将它们存储在company_images中,而不是company_images/$ photo_directory_name – bob

回答

2

您有您正在使用的大小调整功能里面的一些全局变量:

$directory 
$photo_directory_name 

他们需要或者作为参数传入,或声明为函数中全局:

function resize($width, $height){ 
    global $directory, $photo_directory_name; 
    // rest of function 
+0

不幸的是,这并没有解决问题。它正在创建新文件夹,但仍将图片放入company_images中。 – bob

+0

我觉得这行'$ total_path = $目录。 $ photo_directory_name。 $ path;'工作不正常。我的回答给出了一个可能的解释,但可能有其他解释。尝试调试该行,并检查'$ total_path'的值是你所期望的。 – James