PHP 上传失败,没有错误
PHP upload not working, no error
我有一个用户可以上传图片的网站(几天前运行良好,我什至没有更改与上传相关的任何内容)
文件所有权设置为 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>");
}
}
输出是
"没有错误,文件上传成功。
图片已上传。”
但是文件 change/appear 服务器上没有。
我也可以删除 $writeimage = fopen($imgPath, "w");和 fclose($writeImage),我不能吗?
这部分有问题
$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
检查中的逻辑颠倒了。如果移动成功,您将记录“无法移动 img”。改用这个:
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
在你的代码中,我发现了一些问题。
//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 变量是在哪里定义的?它应该是$图像。
无需使用 fopen 和 fclose
所以你的密码应该是...
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>");
}
}
我有一个用户可以上传图片的网站(几天前运行良好,我什至没有更改与上传相关的任何内容)
文件所有权设置为 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>");
}
}
输出是
"没有错误,文件上传成功。
图片已上传。”
但是文件 change/appear 服务器上没有。
我也可以删除 $writeimage = fopen($imgPath, "w");和 fclose($writeImage),我不能吗?
这部分有问题
$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
检查中的逻辑颠倒了。如果移动成功,您将记录“无法移动 img”。改用这个:
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
在你的代码中,我发现了一些问题。
//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 变量是在哪里定义的?它应该是$图像。 无需使用 fopen 和 fclose
所以你的密码应该是...
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>");
}
}