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

时间:2022-06-10 16:03:46

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只返回true或false,没有错误代码。

help??

帮助吗? ?

7 个解决方案

#1


10  

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

#2


11  

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,实际消息请参考手册页。

#3


4  

I experienced a similar problem when using move_uploaded_file which 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_file does not know how to handle identical files names.

结果表明,文件的名称与目标目录的关系必须是唯一的。move_uploaded_file不知道如何处理相同的文件名。

#4


2  

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中的配置var“upload_max_filesize”。并检查文件的大小。

#5


1  

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。

#6


0  

Did you try to activate error_reporting?

是否尝试激活error_reporting?

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

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

#7


0  

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.

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

#1


10  

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

#2


11  

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,实际消息请参考手册页。

#3


4  

I experienced a similar problem when using move_uploaded_file which 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_file does not know how to handle identical files names.

结果表明,文件的名称与目标目录的关系必须是唯一的。move_uploaded_file不知道如何处理相同的文件名。

#4


2  

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中的配置var“upload_max_filesize”。并检查文件的大小。

#5


1  

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。

#6


0  

Did you try to activate error_reporting?

是否尝试激活error_reporting?

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

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

#7


0  

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.

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