Search code examples
phpfile-uploadimage-uploading

PHP Mobile Camera Upload Not Working


I have been working on this for a few days now, looking on previous questions/answers on here from other people and I have yet to find an answer. The idea behind this is to allow a mobile camera to upload a photo, compress it, store it and record the name of the file in the database.

If I use this code on my PC, it works, no problem. If I use my phone camera, I get either Page not working - ERROR 500 or the form says it has uploaded and I check, the database has the name, but on the server, 'File Not Found'. I have also tried uploading a saved image from my phone and the same happens.

Below is the PHP Script:

$db = new PDO('mysql:host=host;dbname=db', 'user', 'pass');

function compress($source, $destination, $quality) {

    $info = getimagesize($source);

    if($info['mime'] == 'image/jpeg') {
        $image = imagecreatefromjpeg($source);
    } else if($info['mime'] == 'image/gif') {
        $image = imagecreatefromgif($source);
    } else if($info['mime'] == 'image/png') {
        $image = imagecreatefrompng($source);
    }

    imagejpeg($image, $destination, $quality);

    return $destination;

}

if(isset($_POST['submit'])) {

    $target = $_SERVER["DOCUMENT_ROOT"].'/images/uploads/'
    $target = $target . basename($_FILES['file']['name']);

    $photo_name = ($_FILES['file']['name']);
    $imageFileType = pathinfo($target, PATHINFO_EXTENSION);
    $file = $photo_name;

    $query = "INSERT INTO jrtwall_images (imgurl) VALUES ('$file')";
    $result = $db->query($query);

    if($result && compress($_FILES['file']['tmp_name'], $target, 50)) {

        header("Location: /?pid=1&success=1");
        exit();

    } else {

        header("Location: /?pid=1&error=1");
        exit();

    }

}

Here is my HTML:

<form action="core/engines/upload-v2.eng.php" method="post" autocomplete="off" enctype="multipart/form-data">

    <input type="file" name="file" />
    <input type="submit" name="submit" value="Upload &amp; Send" />

</form>

print_r($_FILES['file']); shows:

Array([name] => 15316729868972697354228305840469.jpg [type] => image/jpeg [tmp_name] => /tmp/phpAvNZZF [error] => 0 [size] => 4649129)

Any help what-so-ever would be greatly appreciated. I'm running PHP 5.6.

Things I Have Tried:

  • Enabling Cross Origin
  • Increasing both Upload and Post size limits in my php.ini
  • Uploading with out the compression function (Works, but I NEED the compression)
  • Adding 'capture' and 'image/*' to the input on the form
  • Checking $_FILES array for errors, None/Error 0
  • Adding $db (connection) to the Compress method
  • Tried code on XAMPP and it worked after changing $target to $target = $_SERVER["DOCUMENT_ROOT"].'/images/uploads/'; however, the tweak did not work once uploaded to the live server, still get ERROR 500. (Awaiting tech support to send me a copy of the server error log as I can't access it through my FTP or cPanel).
  • Tried upgrading to PHP 7, 7.1, 7.2. No luck so reverted back to PHP 5.6.

Solution

  • I have fixed it. When I said I changed the Post and Upload size, I didn't think about increasing the memory_limit...

    The answer to this is to change the memory_limit to 128M.