2012-01-18 86 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