PHP move_uploaded_file 上传成功但 returns 错误
PHP move_uploaded_file successfully uploads but returns false
我正在尝试为网站创建图片上传。我还试图在写入目录之前调整图像大小。图片上传正常,但由于某种原因 move_uploaded_file 函数 returns 错误,即使文件实际上正在上传。请在下面查看我的一些代码:
$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,屏幕returns,"Sorry, there was a problem uploading your file."但是,当我去网站上查看图片时,我可以看到图片成功那里。我检查了错误日志,也没有显示任何内容,所以我不知所措。任何建议或想法都将不胜感激!
问题是您使用 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.";
}
我正在尝试为网站创建图片上传。我还试图在写入目录之前调整图像大小。图片上传正常,但由于某种原因 move_uploaded_file 函数 returns 错误,即使文件实际上正在上传。请在下面查看我的一些代码:
$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,屏幕returns,"Sorry, there was a problem uploading your file."但是,当我去网站上查看图片时,我可以看到图片成功那里。我检查了错误日志,也没有显示任何内容,所以我不知所措。任何建议或想法都将不胜感激!
问题是您使用 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.";
}