PHP move_uploaded_file() FAILS and i don&#

2019-02-23 14:00发布

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.

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

help??

7条回答
来,给爷笑一个
2楼-- · 2019-02-23 14:04

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.

查看更多
我只想做你的唯一
3楼-- · 2019-02-23 14:04

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.

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.

查看更多
▲ chillily
4楼-- · 2019-02-23 14:10

Did you try to activate error_reporting?

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

查看更多
我想做一个坏孩纸
5楼-- · 2019-02-23 14:11

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

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
查看更多
成全新的幸福
6楼-- · 2019-02-23 14:13

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.

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

查看更多
forever°为你锁心
7楼-- · 2019-02-23 14:22

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.

查看更多
登录 后发表回答