16

I've been slowly learning PHP and have found an array of information on the subject and solutions posted by other developers. I am attempting to have an android application upload a file to PHP server via HTTP post. However something is not working on my server side wile attempting to write to file in PHP.

Here is the PHP code:

// Where the file is going to be placed
$target_path = "/var/www/media2net/uploads/uploads";

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);

if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']) .
        " has been uploaded";
    chmod("uploads/" . basename($_FILES['uploadedfile']['name']), 755);
} else {
    echo "There was an error uploading the file, please try again!";
    echo "filename: " . basename( $_FILES['uploadedfile']['name']);
    echo " target_path: " .$target_path;
}

I already know from inspecting wire shark on client side that http post is sent out correctly, also I have ensured that the directory I'm writing the file to has the correct permissions, and php safe mode is set to off.

the output from apache2 error.log file reads

[Wed Dec 05 09:25:36 2012] [error] [client 74.14.162.250] PHP Warning:  
move_uploaded_file(): Unable to move '/tmp/phpVLOnn3' to  
'/var/www/media2net/uploads/downloaded_file.png' 
in /var/www/media2net/upload.php on line 9

Any help with this problem or further ways to trouble shoot this would be appreciated.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
brendosthoughts
  • 1,683
  • 5
  • 21
  • 38
  • 3
    is the upload folder writable? – Dale Dec 05 '12 at 12:27
  • Are you sure about the correct permisions? can you post the output of `ls -l /var/www/media2net | grep uploads` command? – Nelson Dec 05 '12 at 12:29
  • 1
    First check $target_path variable it contain double upload and check var/www have write permission.I think it will help you. – Sandip Karanjekar Dec 05 '12 at 12:30
  • also if this makes a difference , this is an amazon ec2 instance running ubuntu server 12.04 – brendosthoughts Dec 05 '12 at 12:30
  • I appologize, it was definately the file permission (frustrating) – brendosthoughts Dec 05 '12 at 12:32
  • I appologize, it was definately the file permission (frustrating) , chmod 777 -R uploads did the trick for future as i know that this is not best practice what permission should this folder have to function properly and more secure ( for upload and download) as i know this is not safest permissions – brendosthoughts Dec 05 '12 at 12:37
  • @brendanmorrison that is the worst solution. Instead of settling on `chmod -R 777`, what you _should_ do is reexamine the situation and ask yourself: "Ok, now that I see that the _PHP_ side of this works (that is, file uploading _does work_), so _what is wrong with the permissions?_" – Yes Barry Dec 05 '12 at 12:41
  • -1 for chmodding to 777. That is not a solution, it's cheating. – Yes Barry Dec 05 '12 at 12:45
  • Also check whether folder where you want to save file exists. – Somnium Jan 11 '16 at 14:10

3 Answers3

20

Change upload permissions for /var/www/media2net/uploads/ either by changing owner with "chown" or by "chmod"

Examples

$ sudo chown apache:apache /var/www/media2net/uploads/
$ sudo chmod 755 /var/www/media2net/uploads/

Also, if downloaded_file.png already exists in that directory and it's owned by another user, then you would need to change ownership on that file as well.

$ sudo chown apache:apache /var/www/media2net/uploads/downloaded_file.png

This way, it can be successfully overwritten by Apache.

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
Niclas Larsson
  • 1,317
  • 8
  • 13
  • Yeah, I know. It was just an example, changed it. – Niclas Larsson Dec 05 '12 at 12:37
  • Switching ownership to apache:apache did not work for me. I made ownership *the same as the owner of the originating php script.* This also allowed me to use the more restrictive chmod 770. – Parapluie Jun 29 '17 at 18:55
  • must use the same user as the webserver (if you're using mod_php) or the same user as php-fpm if you're using that. Debian for instance, uses www-data for apache. – Niclas Larsson Jul 02 '17 at 19:51
5

This solved the problem for me:

$ sudo chown -R www-data:www-data /var/www/html/
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
0

This one worked well in Ubuntu Operating system. You only need to change the ownership

sudo chown -R www-data:www-data (path to the image folder)

MaartenDev
  • 5,631
  • 5
  • 21
  • 33