我有一个网站,用户可以上传图片(几天前工作完美,我甚至没有更改与上传相关的任何内容)
文件所有权设置为www-data,我也尝试了777权限,因此不应该是它无法正常工作的原因。
enctype / data等也设置
这是代码:
switch ($_FILES['upload']['error']) {
case UPLOAD_ERR_OK:
$msg="There is no error, the file uploaded with success.";
break;
case UPLOAD_ERR_INI_SIZE:
$msg="The uploaded file exceeds the upload_max_filesize directive in php.ini.";
break;
case UPLOAD_ERR_FORM_SIZE:
$msg="The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
break;
case UPLOAD_ERR_PARTIAL:
$msg="The uploaded file was only partially uploaded.";
break;
case UPLOAD_ERR_NO_FILE:
$msg="No file was uploaded.";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$msg="Missing a temporary folder. Introduced in PHP 5.0.3.";
break;
case UPLOAD_ERR_CANT_WRITE:
$msg="Failed to write file to disk. Introduced in PHP 5.1.0.";
break;
default:
$msg="unknown error with upload";
}
echo("<script>console.log('".$msg."'); </script>");
//process image
if($_FILES['upload']['tmp_name']!=null){
$imgPath="../assets/img/horses/horse".$id.".jpg";
if((bool)getimagesize($_FILES["upload"]["tmp_name"])){
$writeimage = fopen($imgPath, "w");
if(!move_uploaded_file($_FILES["upload"]["tmp_name"], $imgPath)){
echo("<script>console.log('couldn't move img');</script>");
} else{
echo("<script>console.log('image uploaded');</script>");
}
fclose($writeimage);
} else{
echo(" <div class='alert alert-warning' id='notSaved' style='margin-top:20px;'>
Fehler: Die Datei die hochgeladen werden soll, scheint kein Bild zu sein.
</div>");
}
}
输出是
&#34;没有错误,文件上传成功。
图像上传。&#34;
但是文件没有改变/出现在服务器上。
我也可以删除$ writeimage = fopen($ imgPath,&#34; w&#34;);和fclose($ writeImage),可以吗?
答案 0 :(得分:0)
此部分存在问题
$writeimage = fopen($imgPath, "w");
if(move_uploaded_file($_FILES["upload"]["tmp_name"], $imgPath)){
echo("<script>console.log('couldn't move img');</script>");
} else{
echo("<script>console.log('image uploaded');</script>");
}
fclose($writeimage);
请勿在您希望执行其他文件操作的路径上调用fopen
。此外,您在if
检查中将逻辑颠倒了。如果移动成功,则记录&#34; 无法移动img &#34;。请改用:
if(!move_uploaded_file($_FILES["upload"]["tmp_name"], $image)){
echo '<script>console.log("couldn\'t move img");</script>';
} else{
echo '<script>console.log("image uploaded");</script>';
}
请注意,您还遇到了其他一些问题,我已在此处修复过这些问题。
echo
,echo
(您不应该$image
切换为$imgPath
答案 1 :(得分:0)
在您的代码中,我可以看到一些问题。
//process image
if($_FILES['upload']['tmp_name']!=null){
$image="../assets/img/horses/horse".$id.".jpg";
if((bool)getimagesize($_FILES["upload"]["tmp_name"])){
$writeimage = fopen($imgPath, "w");
if(move_uploaded_file($_FILES["upload"]["tmp_name"], $imgPath)){
echo("<script>console.log('couldn't move img');</script>");
} else{
echo("<script>console.log('image uploaded');</script>");
}
fclose($writeimage);
} else{
echo(" <div class='alert alert-warning' id='notSaved' style='margin-top:20px;'>
Fehler: Die Datei die hochgeladen werden soll, scheint kein Bild zu sein.
</div>");
}
}
请检查$ imgPath变量的定义在哪里?它应该是$ image。 无需使用fopen和fclose
所以你的cose shouled是......
if($_FILES['upload']['tmp_name']!=null){
$image="../assets/img/horses/horse".$id.".jpg";
if((bool)getimagesize($_FILES["upload"]["tmp_name"])){
if(move_uploaded_file($_FILES["upload"]["tmp_name"], $image)){
echo("<script>console.log('couldn't move img');</script>");
} else{
echo("<script>console.log('image uploaded');</script>");
}
} else{
echo(" <div class='alert alert-warning' id='notSaved' style='margin-top:20px;'>
Fehler: Die Datei die hochgeladen werden soll, scheint kein Bild zu sein.
</div>");
}
}