2012-01-18 88 views
1

我有一個要求在服務器上以2大小,正常和小尺寸上傳圖像。PHP腳本無法上傳服務器上的圖像

所以我發送HTTP請求到PHP文件,我是能夠成功地以正常形式上傳圖片服務器上,

但是,當我試圖調整大小,然後上傳在服務器上,它沒有上傳。 。

因此,這裏是我的PHP腳本

<php 

define ("MAX_SIZE","400"); 

if($_SERVER["REQUEST_METHOD"] == "POST") 
{ 
    $image =$_FILES['userfile']['name']; 
    $uploadedfile = $_FILES['userfile']['tmp_name']; 

if ($image) 
{ 

    $filename = stripslashes($_FILES['userfile']['name']); 

    $extension = getExtension($filename); 
    $extension = strtolower($extension); 

      if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
    { 
     $errors=1; 
    } 
    else 
    { 
        $size=filesize($_FILES['userfile']['tmp_name']); 
        if ($size > MAX_SIZE*1024) 
        { 
         $errors=1; 
        } 
        if($extension=="jpg" || $extension=="jpeg") 
        { 
          $uploadedfile = $_FILES['userfile']['tmp_name']; 
          $src = imagecreatefromjpeg($uploadedfile); 
        } 


        list($width,$height)=getimagesize($uploadedfile); 

        $newwidth = 70;     
        $newheight = 70; 

        $tmp = imagecreatetruecolor($newwidth,$newheight); 

        imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 



        $file_name = substr (md5(uniqid(rand(),1)), 5, 15); 


        $iconuploaddir = "/home/announce/public_html/TravelApp/images/$output/icons/"; 

        $file = basename($_FILES['userfile']['name']); 
        $newname = $file_name . $file; 

        $uploadIconfile = $iconuploaddir . $newname; 


        if (move_uploaded_file($temp, $uploadIconfile)) { 
          $imagepath = $baseuploaddir;// Give the path where the image saves. or print some messages 
        } 

        imagedestroy($src); 
        imagedestroy($tmp);      
      } 
     } 
} 
?> 

我猜move_uploaded_file($溫度,$ uploadIconfile))是問題的主要原因

請幫助我。

+0

爲什麼有人會喜歡閱讀大量部分評論的代碼? – k102 2012-01-18 11:33:55

+0

@ K102,因爲他正在求救 – 2012-01-18 11:40:21

回答

2

您不能使用move_uploaded_file爲縮略圖因爲縮略圖文件沒有上傳

您可以使用此函數調用move_uploaded_file對於上傳

function make_thumb($src,$dest,$desired_width) 
{ 

    /* read the source image */ 
    $source_image = imagecreatefromjpeg($src); 
    $width = imagesx($source_image); 
    $height = imagesy($source_image); 

    /* find the "desired height" of this thumbnail, relative to the desired width */ 
    $desired_height = floor($height*($desired_width/$width)); 

    /* create a new, "virtual" image */ 
    $virtual_image = imagecreatetruecolor($desired_width,$desired_height); 

    /* copy source image at a resized size */ 
    imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height); 

    /* create the physical thumbnail image to its destination */ 
    imagejpeg($virtual_image,$dest); 
}

對於文件之後創建縮略圖$src帕拉姆通$imagepath$dest,把你想要的目的地縮略圖圖像保存。

+0

當我試圖使用此功能, PHP的警告:imagejpeg()[function.imagejpeg]:無法打開 '/首頁/宣佈/的public_html/TravelApp /圖片/ TajMahal /圖標/'寫作:是/home/announce/public_html/TravelApp/php/test-upload.php中的目錄.. Whta錯誤? – 2012-01-18 12:30:55

+0

將目錄的寫入權限更改爲777或者可以讓你寫入它的東西 – 2012-01-18 12:38:08

+0

嘿,我能夠解決它,我GOOGLE了它發現$ dest的路徑需要是正確的。因此,在做它之後已成功上傳。感謝了很多@yankitwizzy – 2012-01-18 12:54:43

相關問題