Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP move_uploaded_file() error?

I using following code and it is successfully uploading files on my local machine. It is showing "Successfully uploaded" on my local machine.

// Upload file $moved = move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . "myFile.txt" );  if( $moved ) {   echo "Successfully uploaded";          } else {   echo "Not uploaded"; } 

But when I used this code on my online server then it is not uploading file and just showing message "Not uploaded".

How can I know that what is the problem and how can I get the actual problem to display to the user ?

like image 856
Awan Avatar asked Aug 17 '10 11:08

Awan


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 change upload image name in PHP?

You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file . $temp = explode(".", $_FILES["file"]["name"]); $newfilename = round(microtime(true)) .

What is $_ files in PHP?

PHP $_FILES The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.


2 Answers

Edit the code to be as follows:

// Upload file $moved = move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . "myFile.txt" );  if( $moved ) {   echo "Successfully uploaded";          } else {   echo "Not uploaded because of error #".$_FILES["file"]["error"]; } 

It will give you one of the following error code values 1 to 8:

UPLOAD_ERR_INI_SIZE = Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

UPLOAD_ERR_FORM_SIZE = Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

UPLOAD_ERR_PARTIAL = Value: 3; The uploaded file was only partially uploaded.

UPLOAD_ERR_NO_FILE = Value: 4; No file was uploaded.

UPLOAD_ERR_NO_TMP_DIR = Value: 6; Missing a temporary folder. Introduced in PHP 5.0.3.

UPLOAD_ERR_CANT_WRITE = Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.

UPLOAD_ERR_EXTENSION = Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help.

like image 156
Denver Chiwakira Avatar answered Sep 28 '22 05:09

Denver Chiwakira


Try this:

$upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/images/";  if (is_dir($upload_dir) && is_writable($upload_dir)) {     // do upload logic here } else {     echo 'Upload directory is not writable, or does not exist.'; } 

This will instantly flag any file permission errors.

like image 39
Martin Bean Avatar answered Sep 28 '22 05:09

Martin Bean