我正在尝试为网站创建图片上传。我也试图在写入目录之前调整图像大小。图片正在上传很好,但无论出于何种原因,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。但是,当我在网站上查看图片时,我可以看到图像在那里成功。我检查了错误日志,没有显示在那里,所以我不知所措。任何建议或想法都表示赞赏!
答案 0 :(得分: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.";
}