Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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??

like image 589
eladyanai Avatar asked Dec 10 '11 15:12

eladyanai


People also ask

What is move_uploaded_file in PHP?

Definition and Usage The move_uploaded_file() function moves an uploaded file to a new destination. Note: This function only works on files uploaded via PHP's HTTP POST upload mechanism. Note: If the destination file already exists, it will be overwritten.

How can upload file in specific folder in PHP?

Create The Upload File PHP Script$target_dir = "uploads/" - specifies the directory where the file is going to be placed. $target_file specifies the path of the file to be uploaded. $uploadOk=1 is not used yet (will be used later) $imageFileType holds the file extension of the file (in lower case)

How can get upload file name in PHP?

In PHP, we can access the actual name of the file which we are uploading by keyword $_FILES[“file”][“name”]. The $_FILES is the by default keyword in PHP to access the details of files that we uploaded.

How do I move a file into permanent directory?

How To Move Uploaded Files To Permanent Directory in PHP? PHP stores uploaded files in a temporary directory with temporary file names. You must move uploaded files to a permanent directory, if you want to keep them permanently. PHP offers the move_uploaded_file() to help you moving uploaded files.


2 Answers

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
like image 156
rjv Avatar answered Sep 21 '22 14:09

rjv


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.

like image 33
Your Common Sense Avatar answered Sep 21 '22 14:09

Your Common Sense