search

Home  >  Q&A  >  body text

PHP move_uploaded_file() fails and I don't know why

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 tried using ftp_fput, ftp_put, move_uploaded_file for uploading, renaming, copying, anything else I could use. Nothing seems to work.

I can't understand what the problem is because move_uploaded_file only returns true or false and there is no error code.

help??

P粉111927962P粉111927962309 days ago424

reply all(2)I'll reply

  • P粉610028841

    P粉6100288412024-01-30 00:59:13

    But you have to do it.

    This is what error messages are for.
    Do you see any error messages when something goes wrong? If not, then you must check the error log.

    Add this line to the top of the code

    error_reporting(E_ALL);

    Also this if it is your local (non-live) server

    ini_set('display_errors',1);

    So you can see the error on the screen

    For file uploads, you must first check $_FILES['file']['error']). If it is not 0, see the man page for the actual message.

    reply
    0
  • P粉211600174

    P粉2116001742024-01-30 00:17:11

    Are you sure the target directory has write permissions for world? i.e. the third number in the permission representation? Files uploaded by php are owned by and belong to the www-data

    group

    You can change ownership in the following ways

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

    reply
    0
  • Cancelreply