views:

79

answers:

4

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 ?

+1  A: 

Check that the web server has permissions to write to the "images/" directory

Andrei Serdeliuc
I can upload files in this folder manually using FileZilla, it means it has the rights. Any other possibility ?
Awan
FileZilla doesn't prove that the web server has permissions. The web server will most likely run under a different user than you are. To make sure it has permissions, just set the permissions to 777, then test. This should prove if it's a permission issue or something else.
Andrei Serdeliuc
+2  A: 

Do you checks that file is uploaded ok ? Maybe you exceeded max_post_size, or max_upload_filesize. When login using FileZilla you are copying files as you, when uploading by PHP wiritng this file is from user that runs apache (for exaplme www-data), try to put chmod 755 for images.

killer_PL
+2  A: 

How can I know that what is the problem

Easy. Refer to the error log of the webserver.

how can I get the actual problem to display to the user ?

NEVER do it.
An average user will unerstand nothing of this error.
A malicious user should get no feedback, especially in a form of very informative error message.

Just show a page with excuses.

If you don't have access to the server's error log, your task become more complicated.
There are several ways to get in touch with error messages.

To display error messages on screen you can add these lines to the code

ini_set('display_errors',1);
error_reporting(E_ALL);

or to make custom error logfile

ini_set('log_errors',1);
ini_set('error_log','/absolute/path/tp/log_file');

and there are some other ways.
but you must understand that without actual error message you can't move. It's hard to be blind in the dark

Col. Shrapnel
Well, it's fine to show errors on a development site. It's not always possible to redirect errors to a log file (e.g. on shared hosting).
Pekka
@Pekka we are talking of a live one atm. And, honestly, such a hosting should be abandoned immediately.
Col. Shrapnel
+1  A: 

Try this:

$upload_dir = "images/";
if (file_exists($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.

Martin Bean
Not any of them. And such error handling is useless.
Col. Shrapnel
Care to elaborate for those uninformed?
Martin Bean