mberna
mberna

Reputation: 343

File upload works on desktop, but not on mobile using camera

I am trying to create an HTML and PHP script to upload images to a webserver from a user's phone. It is working good in that it will prompt the user to either select an image from their gallery, or to capture an image with their camera. When I capture a picture, it successfully fills in the information

"foo.jpg"

but when I pass the data onto my PHP script, it gives me the error

"Warning getimagesize() filename cannot be empty.... Sorry, there was an error uploading your file."

What confuses me, is that the filename was not empty in my form. Also, when I submit the form using a desktop computer (it just prompts for a file upload), it works perfectly as it should.

HTML:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" capture="camera" accept="image/*" name="imageFileToUpload">  
    <label for="rma">RMA: </label>
    <input type="text" name="rma">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

PHP:

<?php
$rma = $_POST['rma'];
echo "RMA: " . $rma. "<br>";
$target_dir = "uploads/". $rma;

if (file_exists($target_dir)==false) 
{
    if(mkdir ($target_dir))
    {
        echo "folder created at: " .$target_dir . "<br>";
    }
    else
    {
        echo "failed to create folder " . $target_dir. "<br>";
    }
}

$target_file = $target_dir ."/" .basename($_FILES["imageFileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) 
{
    $check = getimagesize($_FILES["imageFileToUpload"]["tmp_name"]);
    if($check !== false) 
    {
          //echo "File is an image - " . $check["mime"] . ".";
          $uploadOk = 1;
    } 

    else 
    {
      echo "File is not an image.<br>";
          $uploadOk = 0;
    }

    // Check if file already exists
    if (file_exists($target_file)) 
    {
       echo "Sorry, file already exists.<br>";
        $uploadOk = 0;
    }

    // if everything is ok, try to upload file
    else 
    {
       if (move_uploaded_file($_FILES["imageFileToUpload"]["tmp_name"], $target_file)) 
       {
          echo "The file ". basename( $_FILES["imageFileToUpload"]["name"]). " has been uploaded.<br>";
       } 

       else 
       {
             echo "Sorry, there was an error uploading your file.";
       }
    }
}

?>

I would like to thank everyone for their kind and patient help so far and I appreciate any answers and guidance.


UPDATE:

I am using an Ipad with Safari as well as an android device running 6.0.1 Marshmallow and the chrome browser. The dump file did show some information.

Here is the echo from the ipad:

RMA: 2468
C:\wamp64\www\upload.php:5:
array (size=5)
  'name' => string 'image.jpg' (length=9)
  'type' => string '' (length=0)
  'tmp_name' => string '' (length=0)
  'error' => int 1
  'size' => int 0
folder created at: uploads/2468

( ! ) Warning: getimagesize(): Filename cannot be empty in C:\wamp64\www\upload.php on line 31
Call Stack
#   Time    Memory  Function    Location
1   0.0006  376976  {main}( )   ...\upload.php:0
2   0.0011  377176  createImageFile( )  ...\upload.php:20
3   0.0012  377208  getimagesize ( )    ...\upload.php:31
File is not an image.
Sorry, there was an error uploading your file.

Android 6.0.1 chrome:

RMA: 1996
C:\wamp64\www\upload.php:5:
array (size=5)
  'name' => string '20160322_125659.jpg' (length=19)
  'type' => string '' (length=0)
  'tmp_name' => string '' (length=0)
  'error' => int 1
  'size' => int 0
folder created at: uploads/1996

( ! ) Warning: getimagesize(): Filename cannot be empty in C:\wamp64\www\upload.php on line 30
Call Stack
#   Time    Memory  Function    Location
1   0.0006  377880  {main}( )   ...\upload.php:0
2   0.0012  378096  createImageFile( )  ...\upload.php:20
3   0.0012  378128  getimagesize ( )    ...\upload.php:30
File is not an image.
Sorry, there was an error uploading your file.

Working echo from computer:

    RMA: 1234
C:\wamp64\www\upload.php:5:
array (size=5)
  'name' => string '58832_300x300.jpg' (length=17)
  'type' => string 'image/jpeg' (length=10)
  'tmp_name' => string 'C:\wamp64\tmp\phpF5F3.tmp' (length=25)
  'error' => int 0
  'size' => int 3801
The file 58832_300x300.jpg has been uploaded.
New record created successfully

It seems like it never sends the image type or the filesize, only the name when using mobile devices. Any ideas?

Upvotes: 4

Views: 7192

Answers (1)

It seems to be a CORS problem.

There is an error when you try to upload from an another location.

See log file of your wamp server for the detailled error information...

For more informations of that mechanism, see : CORS wiki

Upvotes: 2

Related Questions