1

I want to upload a file from my android application. The first time it happens successfully but the second time if I want to replace the file it won't do anything.

 <?php
    $file_path = "usuario/";

    $file_path = $file_path . basename( $_FILES['uploaded_imagen']['name']);

    if(file_exists($file_path))
    {
      unlink($file_path);
    }

    if(move_uploaded_file($_FILES['uploaded_imagen']['tmp_name'], $file_path)) {
          echo "success1";
    } else{
          echo "fail1";
    }
 ?>
Bruno
  • 299
  • 1
  • 7
  • 23
  • This should work. Does it work when you try it in a browser? Try to eliminate as many possible causes as possible. –  May 19 '15 at 19:09
  • if you don't want to replace the image, why are you deleting it? "I had supper on this plate, I ate the supper. Now the supper is gone. Why is that?". – Marc B May 19 '15 at 19:10
  • 1
    @MarcB I think he means "but for the second time (if I want to replace the old file, for example), it doesn't do anything". –  May 19 '15 at 19:13
  • This work when I use instead basename( $_FILES['uploaded_imagen']['name']); the name of the file directly in the php script – Bruno May 19 '15 at 19:13
  • 1
    most likely because mobile phones default uploaded name is `image.jpg` and since it already exists, it fails; I learned that one the hard way once. You need to rename the file before it gets uploaded. – Funk Forty Niner May 19 '15 at 19:13
  • Ok I know what is the problem. If I manually change the permissions I can overwrite the file or delete throught my mobile app. The problem is that the command chmod() to change permissions doesnt work... If i do it in a browser it works, but not in my app. – Bruno May 19 '15 at 19:18
  • so, why not just rename the file with a unique name? seems like you'll only be overwriting the same file over and over; if that's the case(?). seems to be, to me anyway. Correct me if I'm wrong ;-) – Funk Forty Niner May 19 '15 at 19:26
  • @Fred-ii- because the files are profile images and i want an image for each user. If the user change the image i dont want to have lots of images so i would like to replace the old one by the new one. If I can´t do it i'll rename all the time – Bruno May 19 '15 at 19:30

1 Answers1

0

you could try to change the file permissions before deleting the existing file (if your www-user is allowed to do that)

<?php
$basename = basename($_FILES['uploaded_imagen']['name']);
//todo: (play safe)  $basename = hash('sha256', $basename) . '.EXT';
$target_file = 'usuario/' . $basename;
if(file_exists($target_file))
{
    chmod($target_file,0755); //change perms if possible
    unlink($target_file); //remove the file
}
if(move_uploaded_file($_FILES['uploaded_imagen']['tmp_name'], $target_file))
{
    echo "<P>FILE UPLOADED TO: $target_file</P>";
}
else
{
    echo "<P>MOVE UPLOADED FILE FAILED!</P>";
    print_r(error_get_last());
}

alternatively - just change the permissions on the target directory (after first finding out what the www-user is your server is running as):

ps aux | egrep '(apache|httpd)'

chown www-user:www-group '/path/to/files'
chmod 0700 '/path/to/files'
Ian Carter
  • 26
  • 4
  • Hi thank you, my problem is that if i run your code with my app doesnt work, but doing in the browser it works... or maybe if i change manually the permissions in my directory it works...How it could be possible? – Bruno May 19 '15 at 19:28
  • Thank you all guys, it works now I dont know why.. maybe connection problems or something like that... – Bruno May 19 '15 at 19:49