Home >Backend Development >PHP Problem >What to do if php fails to move files
Solution to the failure of php to move files: 1. Check whether the file path of "$dir.$fileImg['name']" is correct; 2. Check whether $dir has writable permissions and modify it.
#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
php What should I do if the file movement fails?
php file upload, prompts that the file movement failed?
Such as:
<?php include("../Include/Conn.php"); include("../Include/Function.php"); include("../Include/Data.php"); $uploadPage="edit_headimage.php"; $dir=$file_upload_root.'HeadImages'; # 上传的文件目录 $err_msg=false; $err_msg_behind=',请重新<a href="'.$uploadPage.'">上传</a>。'; if (!isset($_FILES['upfile'])) $err_msg='表单不完全'.$err_msg_behind; else $fileImg=$_FILES['upfile']; switch($fileImg['Error']){ case 1: $err_msg='文件超过最大上传限制'.$err_msg_behind; break; case 3: $err_msg='文件上传不完全'.$err_msg_behind; break; case 4: $err_msg='没有选择文件'.$err_msg_behind; break; case 2: $err_msg='文件超过页面最大上传限制'.$err_msg_behind; break; case 7: $err_msg='文件写入失败'.$err_msg_behind; break; case 6: $err_msg='没有临时文件夹'.$err_msg_behind; break; case 0: break; default: $err_msg='未知错误'.$err_msg_behind; } if (!in_array($fileImg['type'],array('image/jpeg','image/pjpeg','image/png'))) $msg="只允许上传png或jpg图片".$err_msg_behind; if (!$err_msg){ if (!move_uploaded_file($fileImg['temp_name'],$dir.$fileImg['name'])){ $err_msg='移动文件失败'.$err_msg_behind; } } if ($err_msg) error ($err_msg); else{ echo '完成'; } include("../Include/Close.php"); ?>
Solution:
if (!move_uploaded_file($fileImg['temp_name'],$dir.$fileImg['name'])){ $err_msg='移动文件失败'.$err_msg_behind; }
move_upload_file cannot move the file
1. Please confirm whether the file path of $dir.$fileImg['name'] is correct.
$dir=$file_upload_root.'HeadImages' Whether you need to add "/" at the end
2. Please Make sure $dir has writable permission
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if php fails to move files. For more information, please follow other related articles on the PHP Chinese website!