Download only in browser only, no download requested

I cannot understand this, and I know that it is simple. I am creating a back-end for a very simple content management system. For this particular part, I'm just trying to create a PHP link that allows you to upload a file (client CV).

MY PROBLEM:

When you click the link to download the file instead of the browser prompting you to select the local directory for saving the file, it simply displays the file and a bunch of characters before and after the contents of the document (I assume this is opening and closing exif files for the decryption application).

How can I get the browser to ask the user "Save as ..."?

<?php require("connect.php"); $query = "SELECT * FROM kb_info LIMIT 1"; $result = mysql_query($query, $link); while ($row = mysql_fetch_array($result)) { $file_extension = end(explode(".", $row["extension"])); if ($file_extension == doc) { header('Content-disposition: attachment; filename='.$row["extension"]); header('Content-type: application/doc'); header ("Content-Length: ".filesize($row["extension"])); readfile($row["extension"]); exit; } if ($file_extension == docx) { header('Content-disposition: attachment; filename='.$row["extension"]); header('Content-type: application/docx'); header ("Content-Length: ".filesize($row["extension"])); readfile($row["extension"]); exit; } if ($file_extension == pdf) { header('Content-disposition: attachment; filename='.$row["extension"]); header('Content-type: application/pdf'); header ("Content-Length: ".filesize($row["extension"])); readfile($row["extension"]); exit; } } ?> 

Many thanks,

Joshi

+2
php download content-management-system
source share
2 answers

I think the problem may be that there are some spaces in the PHP files, which is why the headers are not sent correctly, and therefore you see all the output.

I would suggest the following steps:

  • check "connect.php" and find empty lines / spaces at the beginning / end of the file and delete them

  • adapt your php files so that you don’t leave the end tag ?> at the end of the file - this way you will not get empty lines at the end of the file

  • if this is not enough, you need to check your apache and php error log and / or set up error logging, so you will also see warnings - that you will be informed whether the headers are sent correctly or if there is some other error

+1
source share

The headers I use to download:

  header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-type: application/force-download"); header("Content-Disposition: attachment; filename=".$file); header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$bytes.""); 
0
source share

All Articles