PHP reads file instead of loading

I have a script below to download files from a folder using PHP, it works locally, but on the Internet it reads and prints on a page loaded with.

if( $result && count($result) > 0){ $row = $result[0]; $book = true; $Location = './files/'; $bookLocation = $Location . $row['file_name']; // exampe: report.zip if( file_exists( $bookLocation ) ){ $fileLocation = $bookLocation; $file_size = filesize($fileLocation); echo 'Please wait downloading the file....'; header('Content-Description: File Transfer'); header('Content-Type: application/force-download'); header('Content-Disposition: attachment; filename=' . $row['file_name']); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . $file_size); ob_clean(); flush(); readfile($fileLocation); exit; }else{ echo '<h3>Sorry!, file not found...</h3>'; } } 

.htaccess script

 RewriteEngine on RewriteRule ^$ index.html RewriteRule ^index.html$ index.php RewriteRule ^([0-9]{1,9})(/)([^/\.]+[^/\.]+)(_+[^/\.]+)(\.html|)/?$ index.php?id=$1&cat=$3 RewriteRule ^([^/\.]+[^/\.]+)(\.html|)/?$ index.php?page=$1 RewriteRule ^(.*)(/)([0-9]{1,9})(/)(.*)(\.html|)/?$ index.php?view_file=$3&file=$5 RewriteRule ^(.*)(/)([0-9]{1,9})(/)(.*)(\.htm)/?$ index.php?download=$3 RewriteRule ^(author)(/)(.*)(.html)/?$ index.php?user=$1 

Thanks for the help.

0
php .htaccess
source share
3 answers

It is possible that in some browsers you install it to automatically download and run this particular type of file.

If so, it is impossible to β€œfix” it. The browser decides what to do with the file, even if you tell it to download it, the user could say that it starts directly.

Big board

As long as my previous comment is correct, you have a serious problem in your code:

  $file_size = filesize($fileLocation); echo 'Please wait downloading the file....'; header('Content-Description: File Transfer'); 

You must remove this echo statement. It should cause problems, such as headers that are not sent. Given your description, I would say that this is your problem.

See, as soon as you submit any content, you cannot send more headers.

+2
source share

The URL can be used as the file name with this function if fopen wrappers are included. For details on how to specify a file name, see fopen () . See Supported Protocols and Wrappers for links to information on which capabilities various wraps have, notes on their use, and information on any predefined variables that they can provide.

I hope this helps, I found it in here

0
source share

As the Christian suggested, you should remove the echo statement in your code. If this does not work for you even after that, try replacing the line

 header('Content-Type: application/force-download'); 

from

 header('Content-Type: application/octet-stream'); 
0
source share

All Articles