Uploading a file through a PHP script leads to an incorrect / excellent md5 checksum - why?

I am trying to implement indirect loading via PHP. On the client side, I check if the downloaded file is correct or not using md5.

When I upload the file directly ( http: //server/folder/file.apk ), I get the same md5 checksum as on the file system, but when loading using the PHP script ( http: //server/some_page.php ) I get a completely different checksum. What for?

Here is my PHP script:

<?php $name_file="test2.apk"; $path="/home/user/public_html/apk/"; $dimension_file=(string)filesize($name_file); header("Content-Type: application/vnd.android.package-archive ; name=".$name_file); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$dimension_file); header("Content-Disposition: inline; filename=".$name_file); header("Expires: 0"); header("Cache-Control: no-cache, must-revalidate"); header("Cache-Control: private"); header("Pragma: public"); readfile($path.$name_file); ?> 
+4
source share
3 answers

I found an error:

  $name_file="test2.apk"; $path="/home/user/public_html/apk/"; $dimension_file=(string)filesize($name_file); //<-- HERE! --- 

i retrieved the size using only the file name instead of using the full path

 filesize($name_file) ---> filesize( $path . $name_file) 

the error was hidden from

 header("Content-Type: application/vnd.android.package-archive"); 

and the response to the php error is added to the contents of the downloaded file.

So, I suggest who has such problems to comment on the "Content-Type" during debugging, to find out if there are any errors in the PHP code, and when all the code seems to activate the "Content-Type" again, heading.

On my server spaces in code before

 readfile($path.$name_file); 

does not affect the checksum

Thanks to Vladimir and Rocket for good practice tips.

+1
source

how about that decide?

I found an error: $ name_file = "test2.apk"; $ PATH = "/ home / user / public_html / APK /"; $ Dimension_file = (string) file size ($ name_file); // <- HERE! --- I was extracting the size using only the file name, instead of using the full path filesize ($ name_file) ---> filesize ($ path. $ name_file) the error was hidden from the header ("Content-Type: application / vnd.android .package-archive ") and the response php error is added to the contents of the downloaded file. Thanks to Vladimir and Rocket for good practice tips - yesterday Zorb

+1
source

I had problems downloading PDF files via fread (although readfile gave the same problem). My possible problem was solved by disabling the debug output of the application, which in my case was Wordpress . I have WP_DEBUG set to true, which caused the differences in MD5.

0
source

Source: https://habr.com/ru/post/1382727/


All Articles