php move_uploaded_file 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3856032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:19:14  来源:igfitidea点击:

move_uploaded_file failure

phpimage

提问by YsoL8

Can someone help me understand why this is returning false?

有人能帮我理解为什么这返回 false 吗?

if ((move_uploaded_file($_FILES[$field]['tmp_name'], $path))) {

As in, potential causes. I have checked the variables are correct and I have checked permissions on the destination folder which is 777.

如,潜在原因。我检查了变量是否正确,并且检查了目标文件夹 777 的权限。

Cheers!

干杯!

For those wanting to see var_dump ($_FILES);

对于那些想要查看 var_dump ($_FILES);

array(1) { ["image"]=>  array(5) { ["name"]=>  string(14) "accountile.png" 
["type"]=>  string(9) "image/png" ["tmp_name"]=>  string(14) "/tmp/php28IQhv" 
["error"]=>  int(0) ["size"]=>  int(394) } }

回答by Your Common Sense

I have checked the variables

我已经检查了变量

Do not check variables but check error messages.
It's the only thing you need.
Add these lines at the top of your code

不要检查变量,而是检查错误消息。
这是你唯一需要的东西。
在代码顶部添加这些行

ini_set('display_errors',1);
error_reporting(E_ALL);

and see what it says.
If move_uploaded_file failed, it will alwaysraise an error with detailed explanation.
You won't believe it, but reading error messages is way more efficient way to find a problem than guessworkyou tried before

看看它说了什么。
如果 move_uploaded_file 失败,它总是会引发一个带有详细解释的错误。
您不会相信,但阅读错误消息是比您之前尝试的猜测更有效的发现问题的方法

I can't believe noone mentioned it already.

我不敢相信没有人已经提到过。

回答by tim

Did you edit your php.ini to make sure upload_tmp_dir points to a temporary directory?

您是否编辑了 php.ini 以确保 upload_tmp_dir 指向临时目录?

回答by Boris

Lo?c Février, thank you, you've saved me a lot of time!

Lo?c Février,谢谢你,你帮我节省了很多时间!

Here is my part... printing possible errors during upload.

这是我的部分......在上传过程中打印可能的错误。

First create directory Uploader/UploadedFiles Then use code below...

首先创建目录 Uploader/UploadedFiles 然后使用下面的代码...

$destination=$_SERVER[DOCUMENT_ROOT]."/Uploader/UploadedFiles/" . $_FILES["file"]["name"];

if(move_uploaded_file($_FILES["file"]["tmp_name"],  $destination)){
   echo ("Stored in".$_SERVER[DOCUMENT_ROOT]."/Uploader/UploadedFiles/".$_FILES["file"]["name"]);
}else{
   $html_body = '<h1>File upload error!</h1>';
   switch ($_FILES[0]['error']) {
   case 1:
      $html_body .= 'The file is bigger than this PHP installation allows';
      break;
   case 2:
      $html_body .= 'The file is bigger than this form allows';
      break;
   case 3:
      $html_body .= 'Only part of the file was uploaded';
      break;
   case 4:
      $html_body .= 'No file was uploaded';
      break;
   default:
      $html_body .= 'unknown errror';
   } 
   echo ($html_body);
}

回答by Webnet

With move_uploaded_fileyou don't need 777 permissions. What is the output of $path? Have you verified that $pathexists? Have you verified that $fieldexists?

有了move_uploaded_file你不需要 777 权限。的输出是$path什么?你验证过它$path存在吗?你验证过它$field存在吗?

Either $field or $path don't exist, or open_basedir is in effect is my guess.

$field 或 $path 不存在,或者 open_basedir 有效是我的猜测。

Is open_basedirrestriction enabled? That could prevent the destination of the uploaded file from being written to. Look in your php.ini for open_basedir, if there's a path specified then it is enabled and you want to see if the destination path of your uploaded file is within this path. If it is, that's why it's failing.

是否open_basedir启用限制?这可能会阻止写入上传文件的目的地。在您的 php.ini 中查找open_basedir,如果指定了路径,则它已启用,并且您想查看上传文件的目标路径是否在此路径内。如果是,那就是它失败的原因。

update

更新

$path cannot be a URL, it must be a local path such as /home/user/public_html/

$path 不能是 URL,它必须是本地路径,例如 /home/user/public_html/

回答by ITroubs

Did you check permission on the temporary upload folder?

您是否检查了临时上传文件夹的权限?

What does php tell you if you do:

如果你这样做,php 会告诉你什么:

var_dump($_FILES);

回答by Lo?c Février

Don't use

不要使用

$path = "http://www.barbadostravelbuddy.co.uk/demo/images/carhire
    /accountile10420103260403000000pm.png"

but

$path = "/home/sites/barbadostravelbuddy.co.uk/public_html/demo/images/carhire/
    accountile10420103260403000000pm.png"

It needs to be a path on the system, not an URL.

它需要是系统上的路径,而不是 URL。

回答by Pekka

Basic debugging steps:

基本调试步骤:

  • What does a print_r($_FILES)look like?
  • Does the source file actually exist?
  • Is the "error" flag of the file upload zero (will be visible when doing the print_r)
  • What does the target $pathlook like?
  • Are you specifying a full file name in the target path?
  • 什么是一个print_r($_FILES)什么样子?
  • 源文件真的存在吗?
  • 文件上传的“错误”标志是否为零(执行 print_r 时可见)
  • 目标$path长什么样?
  • 您是否在目标路径中指定了完整的文件名?

My guess is that $pathis only a path to a folder, not to a full file name.

我的猜测是这$path只是一个文件夹的路径,而不是完整的文件名。

Update: You need to specify a filesystem pathas $path.

更新:您需要将文件系统路径指定为 $path。

回答by A user

$pathhas to be a file name and not a path to a directory.

$path必须是文件名而不是目录的路径。

回答by Robert Sinclair

Check:

查看:

enctype="multipart/form-data"

Inside the form itself, without it file upload won't work at all.

在表单内部,没有它,文件上传根本无法工作。

回答by francovici

I was facing the same problem.

我面临同样的问题。

If your error says "Failed to open stream: Permission Denied"it means the PHP Serveris not being capable of creating the new file inside your destination directory.

如果您的错误显示“无法打开流:权限被拒绝”,则意味着PHP 服务器无法在您的目标目录中创建新文件。

Once you have set the Linux permissions on the directory (wich sounds like you did by making it 777) you should give that special permission to the PHP Server.

一旦您在目录上设置了 Linux 权限(听起来就像您将其设置为 777),您应该将该特殊权限授予 PHP 服务器。

If your folder is named "uploads", you should cd to the previous directory and use the next command:

如果您的文件夹名为“uploads”,您应该 cd 到上一个目录并使用下一个命令:

chcon -R -t httpd_sys_rw_content_t uploads

That definetly solved my problem.

这绝对解决了我的问题。

Hope it helps.

希望能帮助到你。