2017-05-25 63 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