【问题标题】:PHP move_uploaded_file successfully uploads but returns falsePHP move_uploaded_file 成功上传但返回 false
【发布时间】:2015-02-15 19:10:52
【问题描述】:

我正在尝试将图片上传到网站。我还尝试在写入目录之前调整图像大小。图片上传正常,但无论出于何种原因 move_uploaded_file 函数返回 false,即使文件实际上正在上传。请在下面查看我的一些代码:

$fileType = $_FILES['photo_one']['type'];

 //This gets all the other information from the form 
 $pic=($_FILES['photo_one']['name']); 
$tmppic = ($_FILES['photo_one']['tmp_name']);
$front=uniqid (rand (),false);
$newpic=$front.'_'.$pic;
$newtmppic=$front.'_'.$tmppic;



if($fileType == 'image/jpg' || $fileType == 'image/jpeg' )
{
$tmppic = ($_FILES['photo_one']['tmp_name']);
$src = imagecreatefromjpeg($tmppic);
}
else if($fileType == 'image/png')
{
$tmppic = ($_FILES['photo_one']['tmp_name']);
$src = imagecreatefrompng($tmppic);
}
else 
{
$src = imagecreatefromgif($tmppic);
}


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

$newwidth='700px';
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

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

 //This is the directory where images will be saved 
 $target = "../secure/profile_images/"; 
 $newtarget = $target . basename($newpic); 

imagejpeg($tmp,$newtarget,100);

 //Writes the photo to the server 
 if(move_uploaded_file($tmp, $newtarget)) 
 { 

 //Tells you if its all ok 
header('Location: Picture.php#A1');
 } 
 else { 
  //Gives an error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 

屏幕没有被重定向到 Picture.php,而是返回“抱歉,上传您的文件时出现问题”。但是,当我在网站上查看图片时,我可以看到图像成功存在。我检查了错误日志,也没有任何显示,所以我很茫然。任何建议或想法表示赞赏!

【问题讨论】:

  • 如果您使用 imagick,请先尝试移动上传的文件或 file_put_contents,然后调整图像大小,创建缩略图或修改文件,并保存您修改后的副本 mh?

标签: php file-upload image-uploading image-upload


【解决方案1】:

问题是您使用 imagejpeg($tmp,$newtarget) 创建图像并将其存储到指定路径,然后您尝试移动已创建并存储在服务器上的相同文件。 imagejpeg 将创建图像并将其存储在服务器上,而不使用move_uploaded_file 函数。如果要检查文件是否已创建,请使用以下代码删除move_uploaded_file 函数。

//Writes the photo to the server 
if(imagejpeg($tmp,$newtarget,100)) 
{ 

 //Tells you if its all ok 
 header('Location: Picture.php#A1');
} 
  else { 
 //Gives an error if its not 
  echo "Sorry, there was a problem uploading your file."; 
 } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多