JPEG files uploaded to a PHP script arrive corrupt, but not all the time

I have a PHP script into which I upload JPEG images (via an HTML form). You can see the code here , but I will try to present the relevant parts in this post. The form is declared as follows:

<form action="adm_addphoto.php" method="POST" enctype="multipart/form-data" name="myform"> 

The form field MAX_FILE_SIZE set to 5 MB:

 <input type="hidden" name="MAX_FILE_SIZE" value=5242880> 

The images I want to upload are about 3 MB in size.

After downloading it, I turn the image file into GD jpeg:

 $filename = $_FILES['file']['tmp_name']; $myImage = imagecreatefromjpeg($filename); 

Sometimes the download works fine, and sometimes imagecreatefromjpeg gives warnings about JPEG corruption. For example (line breaks added for readability):

 Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 47 extraneous bytes before marker 0xd9 in /path/adm_addphoto.php on line 97 Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/tmp/phpwlSS9x' is not a valid JPEG file in /path/adm_addphoto.php on line 97 

The fact is that this does not happen reliably. That is, if I try the same image several times in a row, sometimes it will load successfully, and sometimes there will be errors. And as a result of attempts that lead to errors, the specifics of the error message will also change. (With a particular photograph that produced the above messages, the number of โ€œextraneous bytesโ€ is sometimes 47, sometimes 20, sometimes 68.)

What can lead to file corruption in some attempts, but not in others?

PS. I know that there is an ini setting that tells GD to try to work with damaged JPEG files. But this is not so, I want to know why the download result is incompatible.

SFC. Here are the meanings of some of the possibly relevant PHP ini settings:

 memory_limit .......... 128M post_max_size ......... 8M file_uploads .......... On max_file_uploads ...... 20 upload_max_filesize ... 128M upload_tmp_dir ........ no value 

UPDATE: I added code to display the size and checksum of the MD5 file after downloading it. Size is always the correct file size (the size of my local copy of the file). However, MD5 varies between attempts. When MD5 is correct (matches the MD5 of my local copy), there is no error message. When an error message appears, MD5 is always different than expected. Moreover, this is just a perceived difference, but errors seem to occur more often in the last night than this morning - only a small number of translations this morning led to an error.

Here are some of the โ€œbadโ€ downloads (curiously, there were several downloads that gave an MD5 checksum that was different than expected, but didn't cause errors):

  Correct MD5: f7b9587f39c7332e62a08adf34cefbd0 ----------------------------------------------------------------- 38 extraneous bytes: 2a28c46079071d9d2e2fd49865b35d59 ce1c69f798953b201dcc35f85f3b29b4 b013a0428a71adff674a46e92372d46b 71 extraneous bytes: a271928f3559b6deaa19804704b5bcb3 92 extraneous bytes: cab2a10ad8535addaca3b19bcb607a30 premature end of data segment: 4514d39db1d94ab691d6da26c0832cdb No error (!): 83b2e3624ddcfdeb3efc10be81631916 07d0a97b21d423fdeb4c6f88d76f8cd3 

UPDATE: In response to Andre and Pekka: Here are links to two versions of the same picture. This file is the original copy of the image stored on my machine; I uploaded it the way I usually upload files to my web hosting, that is, I used the FTP client. This file is the same image uploaded using my script (but with an added line to move it to this location using move_uploaded_file() ). The error message during the download was "Corrupt JPEG data: 30 extraneous bytes ...". You will notice that the bottom of the image is not displayed correctly. I apologize for the large size of the images, but I can only use examples of image types that bring me problems.

  • I must clearly indicate that this is another image that I used for the above MD5 checksums. I did not think to use the same image.

UPDATE: Using the two images that I linked above, I discovered the differences between the two files. There are two sequences of 32 bytes that differ from each other in the same image. Files are otherwise identical.

 nb the first character is numbered as character 1, not character 0 nb 2116624 and 2493456 are both 16 modulo 32. The difference between them is 368 * 1024. character 2116624 through line 2116655 (32 characters): original image: 3c bc 20 19 eb 93 cd 34 db 93 68 7c 8d 5e 37 d4 d3 84 91 70 7e 7c 82 3e e9 e8 3d eb 2e ff 00 ce bad image: 89 c9 54 a6 e5 3d f4 fc 8e 7f 68 d5 47 14 41 f6 55 11 7d a6 55 24 a8 e0 f7 a8 a4 43 06 18 c2 c1 character 2493456 through character 2493487 (32 characters): original image: 3d a4 61 37 19 75 63 2e 51 62 ca 07 e0 9e 49 35 5d 65 8d 22 01 7f 5e b4 a7 19 3a 7a ef 72 1c e3 bad image: 4f 99 26 39 6a db d9 cd 3e 5b ec 63 46 3c b4 ef 2d b6 30 35 0f 12 a1 b6 9a 11 68 1a 69 07 0c 49 
+8
php file-upload
source share
3 answers

Are you sure there are no problems connecting to the Internet? Have you tried this from another connection or even with an ISP?

+3
source share

Its stretching, but one of the possible problems that I can think of is that you are working on a temporary file, try using move_uploaded_file to move the file to where you pointed, and see if that matters?

0
source share

Instead of trying to convert at this point, upload the temp file information and compare the file size, etc.

Due to the random nature of this, this is probably some kind of transmission problem. You can probably exclude your own hardware by trying to move a decent zip file size (for example) from a computer that you run on something else on the local network (several times), and then if the zip file passes the crc check or gets corrupted .

Also: do you use a firewall device that sniffs http traffic? (e.g. untangling)

0
source share

All Articles