How to debug move_uploaded_file() in PHP

2019-07-31 16:14发布

move_uploaded_file() won't work for me anymore, it was working fine and just stopped out of nowhere. Is there a way for me to check why it's not working anymore? Here's what I currently have, but it only returns TRUE or FALSE.

$status = move_uploaded_file($tempFile, $targetFile);
if($status) {
  echo 'its good';
} else {
  echo 'it failed';
}

I know the path is 100% correct and the directory is CHMOD 755. Is there anything I might be doing wrong?

3条回答
Lonely孤独者°
2楼-- · 2019-07-31 16:31

Maybe this will work:

if(!move_uploaded_file($_FILES['attachement']['tmp_name'], $uploadfile)) {

echo 'Your file was not uploaded please try again
here are your debug informations:
'.print_r($_FILES);

      } else {

          echo 'image succesfully uploaded!';

      } 
查看更多
在下西门庆
3楼-- · 2019-07-31 16:42

Check your error reporting level (see error_reporting function). You should get a warning or notice that's a bit more descriptive.

Also, check that the user your PHP script runs as (usually the server's user, which is nobody or www-data on a lot of systems, but YMMV) owns the directory. With 755, only the owner of the directory can write to it.

查看更多
Evening l夕情丶
4楼-- · 2019-07-31 16:49

Permissions of 755 means that only the owner of the directory can write to that directory.

So the question is, who is the owner and as what user is the web-server / php running?

If they don´t match, you can either change the ownership or the group (also changing the permissions to 775).

查看更多
登录 后发表回答