PHP move_uploaded_file() 失败,我不知道为什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8457683/
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-26 04:46:38  来源:igfitidea点击:

PHP move_uploaded_file() FAILS and i don't know why

phpfile-upload

提问by eladyanai

this is my code:

这是我的代码:

$uploaddir = '/temp/';
$uploadfile = $uploaddir.basename($_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
    send_OK();
else
    send_error("ERROR - uploading file");

i have tried to upload with ftp_fput, ftp_put, move_uploaded_file, rename, copy and anything i can put my hands on. nothing seems to work.

我曾尝试使用 ftp_fput、ftp_put、move_uploaded_file、重命名、复制和任何我可以动手的东西上传。似乎没有任何效果。

i can't understand what is the problem since move_uploaded_file returns only true or false and no error code.

我不明白是什么问题,因为 move_uploaded_file 只返回真或假,没有错误代码。

help??

帮助??

回答by Your Common Sense

i don't know why

我不知道为什么

But you have to.

但你必须这样做。

That's what error messages are for.
Do you see any error message when something goes wrong? If not, then you have to check error logs.

这就是错误消息的用途。
当出现问题时,您是否看到任何错误消息?如果没有,那么您必须检查错误日志。

Add this line at the top of your code

在代码顶部添加此行

error_reporting(E_ALL);

and this one, if it's your local (not live) server

而这个,如果它是您的本地(非实时)服务器

ini_set('display_errors',1);

so you'll be able to see errors onscreen

这样您就可以在屏幕上看到错误

For the file uploads you have to check $_FILES['file']['error'])first. it it's not 0, refer to the manual page for the actual message.

对于文件上传,您必须先检查$_FILES['file']['error'])。它不是0,请参阅手册页了解实际消息。

回答by rjv

Are you sure that the target directory has write permissions for world?ie,the third number in permission representation? The files uploaded by php are owned by and comes under the group www-data

您确定目标目录具有写入权限world吗?即权限表示中的第三个数字?php上传的文件归组所有www-data

You can change the ownership by

您可以通过以下方式更改所有权

[sudo] chown -R www-data folder // change owner
[sudo] chown -R www-data:www-data folder // change group and owner

回答by abriggs

I experienced a similar problem when using move_uploaded_filewhich would fail to upload particular files with an $_FILES['filename']['error']code of 0.

我在使用move_uploaded_file时遇到了类似的问题,它无法上传$_FILES['filename']['error']代码为 0 的特定文件。

It turns out that the name of the file needs to be unique in relation to the destination directory. move_uploaded_filedoes not know how to handle identical files names.

事实证明,文件的名称需要相对于目标目录是唯一的。 move_uploaded_file不知道如何处理相同的文件名。

回答by Eddie

This caught me out too. Be aware of:

这也让我措手不及。意识到:

move_uploaded_file() is both safe mode and open_basedir aware. However, restrictions are placed only on the destination path as to allow the moving of uploaded files in which filename may conflict with such restrictions. move_uploaded_file() ensures the safety of this operation by allowing only those files uploaded through PHP to be moved.

move_uploaded_file() 既是安全模式又是 open_basedir 感知。但是,限制仅在目标路径上设置,以允许移动文件名可能与此类限制冲突的上传文件。move_uploaded_file() 通过只允许移动通过 PHP 上传的文件来确保此操作的安全性。

These settings can cause the upload to fail if you try to move the file outside of your website base directory for example.

例如,如果您尝试将文件移到网站基本目录之外,这些设置可能会导致上传失败。

回答by josegil

Have you check the limit of the file size? One of the reason if crashing could be that you are trying to upload a file bigger than the limit in your configuration. Look at the config var "upload_max_filesize" in your php.ini and check the size of the file.

您是否检查过文件大小的限制?崩溃的原因之一可能是您尝试上传的文件大于配置中的限制。查看 php.ini 中的配置变量“upload_max_filesize”并检查文件的大小。

回答by zyamys

In addition to permissions, be sure to check that there is disk space available on your server. If not, move_uploaded_file() will fail with error 0.

除了权限之外,请务必检查您的服务器上是否有可用的磁盘空间。如果不是,move_uploaded_file() 将失败并返回错误 0。

回答by LordOfBerlin

Did you try to activate error_reporting?

您是否尝试激活 error_reporting?

You should check your php-config if file uploads are allowed.

如果允许文件上传,您应该检查您的 php-config。