0

I have any troubles with file upload, with JPG files. But not with all, I don't find any reason... :-( And of course, I tried to search first, but no results...

I have an image, printscreen from website, opened and saved in Photoshop as JPG with no compression, 100 quality. It's normally working in Mac Finder, I can open the image, it seems to be not corrupted (have you ever seen corrupted printscreen :-) ). When I have another one image (JPG), open it in Photoshop, resave and try to upload, file is uploaded correctly.

Upload script is shortened to this code

$files = [$_FILES['image'], $_FILES['imageHP']];
foreach ($files as $src) {
    $path = '';
    $name = prettyname($src['name']) . '-' . substr(time(), -4) . '.' . strtolower(pathinfo($src['name'], PATHINFO_EXTENSION)); // prettyname just make cool image name, strtolower, no spaces, accents, etc.
    if (!move_uploaded_file($src['tmp_name'], __DIR__ . "/../uploads/" . $path . 'orig_' . $src['name'])) {
        die ('Corrupted file'); // without this condition it the same
    }
}

After uploading a file(s) I sometimes gets

Array
(
    [image] => Array
        (
            [name] => centrum.jpg
            [type] => image/jpeg
            [tmp_name] => /private/var/tmp/phpzDyQE4 // file should be uploaded?
            [error] => 0           // strange error = 0
            [size] => 0            // strange, size = 0
        )

)

When I try to upload another one image, the dump is correct and file is uploaded.

Array

(
    [image] => Array
        (
            [name] => team.jpg
            [type] => image/jpeg
            [tmp_name] => /private/var/tmp/phpc5wmkE
            [error] => 0       // no error
            [size] => 601807   // correct file size
        )

Any idea? Thanks

pavel
  • 26,538
  • 10
  • 45
  • 61
  • 1
    @vivek_23 8M/2M, image size is 604kB – pavel Aug 07 '20 at 11:52
  • I hope permissions for the files are ok. – nice_dev Aug 07 '20 at 11:53
  • 1
    @vivek_23: sure, permission are OK. Another images are uploaded correctly. – pavel Aug 07 '20 at 11:54
  • Wether the image might be corrupted or not, should make no difference for the actual upload part of this. The combination of error=0 and size=0 indicates that simply a completely _empty_ file was uploaded. If no file was uploaded at all or it exceeded any of the limits, then you should get an error code in the range of 1 to 4. – CBroe Aug 07 '20 at 12:01
  • Can you repeatedly reproduce this with the same file, or does it only fail some times? – CBroe Aug 07 '20 at 12:03
  • @CBroe: no, I have at least two images (real images, I have them there in a dir). One is uploaded correctly, second one not. If it fails, it fails always with that image. Both images has non-zero size (as I wrote above, limits are 2/8M, image size 600kB), the 'error one' is saved from Photoshop (screen from web). – pavel Aug 07 '20 at 12:05
  • And it also happens with the “shortened” version of your upload script as shown above? _“After uploading a file(s) I sometimes get”_ - where from, your shortened code does not appear to make any debug outputs? Unless you can definitively say that it happens with the code as shown as well, I’d say probably some of your other processing beforehand might be messing something up. – CBroe Aug 07 '20 at 12:08
  • @CBroe: yes, I try to use this code too (it's shortened in my app for debug reason), I've put you here the 'full' current version. – pavel Aug 07 '20 at 12:08
  • Then I’d try and check what actually lands in the upload temp dir next. (PHP will delete the temp file automatically at script end, if it was not moved - so maybe put a big `sleep(…)` at the beginning of the script, so that you get a bit of time to check the temp dir.) If what lands in there has a size of 0 bytes already, then the problem is probably not with your actual code. – CBroe Aug 07 '20 at 12:12

1 Answers1

0

It seems like a problem with cache. Try to clear cache out, remove temporary files, etc.