1

(essentially a copy of this question, which has no responses)

I have an app that is trying to take a picture using the phone camera, and send that image to our server. Here is the PHP code behind it:

    <?php

  if (isset($_FILES['idimage'])) {
    $img = $_FILES['idimage']['name'];
    $tmpimg = $_FILES['idimage']['tmp_name'];

    copy($tmpimg, "C:/MAMP/htdocs/ids/" . "id.png");
    exit();
  } else {
    echo "there is no data with name [idimage]";
  }

 ?>

I have followed 3 different tutorials and all of them used the method you are seeing above. This code works sometimes, from testing it works 3/22 times (~14%). Why is this? What is causing the file to only upload some of the time?

  • can you please mentioned what kind of files is works, like the size or extension, olso the files that are didn't work – GNassro Jul 20 '20 at 21:25
  • Turn on full warnings and check the warning messages. – Barmar Jul 20 '20 at 21:27
  • 1
    You should use `move_uploaded_file()` rather than `copy()`. – Barmar Jul 20 '20 at 21:28
  • @GNassro There is no correlation between the files, one was 1500KB, one 1900KB, and one 2500KB. The 2500KB one was taken on a phone with a higher res intentionally to see if filesize was a factor. By this logic, the 1920x1080 res cam I am using should work all the time (as files are under 2000KB) but they don't. – Eugene Walsh Jul 20 '20 at 21:30
  • @Barmar I am using copy because move_uploaded_file did not work either. What is full warnings and how do I enable it? – Eugene Walsh Jul 20 '20 at 21:31
  • See https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – Barmar Jul 20 '20 at 21:32
  • please check the php.ini file, and look what value take the upload_max_filesize – GNassro Jul 20 '20 at 21:35
  • I upped the max size to 256M, no success. Turning on full warnings is also giving me no relevant information to this question, but it did help with some other errors in other scripts. Any other suggestions for this question in particular? – Eugene Walsh Jul 20 '20 at 21:49
  • and what about Barmar monsioned, did you use move_uploaded_file() instead of copy() ? – GNassro Jul 20 '20 at 22:11
  • The code above always uses the same target filename "id.png". If there are multiple uploads at the same time files can get overwritten which looks like the copy did not work – Marco 'Lubber' Wienkoop Jul 20 '20 at 22:51

2 Answers2

0

Check out this tutorial, pay particular attention to the verification and path validations employed...

https://www.tutorialspoint.com/php/php_file_uploading.htm

Unless you do proper sanity checks, you will be blind...

Dharman
  • 30,962
  • 25
  • 85
  • 135
rexfordkelly
  • 1,623
  • 10
  • 15
0

The tutorials you found seem to omit relevant information.

  1. You absolutely need to verify the upload status:

    if ($_FILES['idimage']['error'] === UPLOAD_ERR_OK) {
        // Successful upload
    } else {
        // Everything else
    }
    
  2. The recommended function to copy the file is the move_uploaded_file() function, although it shouldn't make any difference from the functionality standpoint, it's mainly a security mechanism.

Last but not least, double-check that your app has full error reporting set and sends stuff to a log file you can check.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Accepted this as it was the most in-depth answer. I am now handling upload errors with this. I was able to get my problem solved, but another arose. I will debug that and hopefully get past this. Thank you everyone for the help! – Eugene Walsh Jul 21 '20 at 16:24