Home  >  Article  >  Backend Development  >  What to do if php fails to move files

What to do if php fails to move files

藏色散人
藏色散人Original
2022-01-10 09:54:241847browse

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.

What to do if php fails to move files

#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.&#39;HeadImages&#39;; # 上传的文件目录
$err_msg=false;
$err_msg_behind=&#39;,请重新<a href="&#39;.$uploadPage.&#39;">上传</a>。&#39;;
if (!isset($_FILES[&#39;upfile&#39;])) $err_msg=&#39;表单不完全&#39;.$err_msg_behind;
else $fileImg=$_FILES[&#39;upfile&#39;];
switch($fileImg[&#39;Error&#39;]){
case 1:
$err_msg=&#39;文件超过最大上传限制&#39;.$err_msg_behind;
break;
case 3:
$err_msg=&#39;文件上传不完全&#39;.$err_msg_behind;
break;
case 4:
$err_msg=&#39;没有选择文件&#39;.$err_msg_behind;
break;
case 2:
$err_msg=&#39;文件超过页面最大上传限制&#39;.$err_msg_behind;
break;
case 7:
$err_msg=&#39;文件写入失败&#39;.$err_msg_behind;
break;
case 6:
$err_msg=&#39;没有临时文件夹&#39;.$err_msg_behind;
break;
case 0:
break;
default:
$err_msg=&#39;未知错误&#39;.$err_msg_behind;
}
if (!in_array($fileImg[&#39;type&#39;],array(&#39;image/jpeg&#39;,&#39;image/pjpeg&#39;,&#39;image/png&#39;))) $msg="只允许上传png或jpg图片".$err_msg_behind;
if (!$err_msg){
if (!move_uploaded_file($fileImg[&#39;temp_name&#39;],$dir.$fileImg[&#39;name&#39;])){
$err_msg=&#39;移动文件失败&#39;.$err_msg_behind;
}
}
if ($err_msg) error ($err_msg);
else{
echo &#39;完成&#39;;
}
include("../Include/Close.php");
?>

Solution:

if (!move_uploaded_file($fileImg[&#39;temp_name&#39;],$dir.$fileImg[&#39;name&#39;])){
$err_msg=&#39;移动文件失败&#39;.$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn