I have a problem with php force download.
I tried a lot of code but no one resolve problem. I need to download file with php (the name of file is sent with querystring and files are stored in a folder protected by htaccess). With this code I can download for example a pics (the browser correctly ask with file dialog where saving it) but when I try to download a pdf file the browser don't open file dialog and save it in default download folder and automatically open pdf with default pdf viewer.
Is there a possibility to open file dialog for pdf file and download like other file like pics without automatic download and opening it?
$file = $_GET['file'];
$url = "{$_SERVER['DOCUMENT_ROOT']}images/files/".$file;
header("Pragma: public");
header("Expires: 0");
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header('Content-disposition: attachment; filename=' . basename($url));
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($url));
#readfile($url);
exit(0);
Related
I'm having some trouble getting a zip file downloaded from a remote server to another using file_put_contents()
This is currently from the api code that serves the binary data of the zip file:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
//header("Content-Type: application/zip");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$update_filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($update_path));
ob_end_flush();
#readfile($update_path);
exit();
And this one is from the server saving the remote file:
all curl request.....
$response = curl_exec($curl);
// some validation ....
// to simplify the process
file_put_contents($local_file, $response);
At this point the actual file is created, and if i open it using windows or winrar it does open just fine, but when i use ZipArchive:
$file = '2019-07-07-update-1.0.4.9.zip';
$zip = new ZipArchive;
$zip->open($file);
$zip->extractTo($directory);
echo "<pre>";
print_r(error_get_last()); // Says it's an invalid zip object or damaged file.
die;
I've tried some other configurations for the headers, but same problem, can't get to extract the file with php.
I've used PHP to mask the directory for PDF downloads for years with the following code with no issues. Then I upgraded from PHP 5.6 to 7.2 yesterday and the code no longer works, but I'm stuck figuring out what the problem is.
Currently, if I click on the download link, the file downloads, but then, at the very end, there is an error message reading "Failed-Network Error" (in Chrome) and "The Network connection is interrupted" (in Safari) and the file can't be opened.
The error log on the server is empty.
Code snippet follows:
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/pdf");
header("Content-Disposition: attachment; filename=".$filename.";");
header("Content-Length: ".filesize($filepath.$filename)."");
ob_end_flush();
readfile($filepath.$filename);
Now if I change the following:
Content-Disposition: attachment;
to:
Content-Disposition: inline;
Then the above works, but as some of the PDFs are large (they range in size from 2 MB to about 80MB), this isn't really a desirable situation and I'd like the file being downloaded to be the default action.
Any thoughts much appreciated.
Thanks!
I am accessing spreadsheets which are not located inside in the web directory and serving them to the browser. I am using the following code to do this:
$filePath = WEBSITE_ROOT_PATH.'/../where_the_spreadsheets_are/file_name.xls';
if(file_exists($filePath)){
$contents = file_get_contents($filePath);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=file_name.xls");
header("Content-Transfer-Encoding: binary");
echo $contents;
}
This code works correctly on 20+ separate servers, but not on one. On the one that fails Excel says that the spreadsheet is corrupt.
The server that fails has a mirror server, so the same file path and file name exists, and on this server the spreadsheet is ok.
It's a Win server 2007 running WAMP. All the rest are also Win Servers running WAMP.
Does anyone know the reason for this?
I have small chunk of code that helps to download a file. But the site not opened/work during file download, but when i open the site on other browser then its working. I don't have any idea what going on with the browser during file download. Here are the headers that i am using to download a zip file:
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/zip");
header("Content-Disposition: attachment; filename=\"".$zipname."\"");
header("Content-Transfer-Encoding: binary");
//header("Content-Length: ".filesize($directory_location . '/' . $zipname));
ob_end_flush();
readfile($directory_location . '/' . $zipname);
ob_end_clean();
Even i don't know how to debug it, so that i get the weak point from my codes.
So since you are using sessions:
An “open” session blocks other scripts from accessing the session while your download script is running.
session_write_close before streaming the file content to the client fixes that. Just call it after you are done with checking whatever you need to check in the session, and before the time-consuming part of the script begins – that will release the lock on the session, and other scripts that are called while the download script is running can access the session again.
I was able to zip a folder and download the zip file using PHP code in "chrome" and "FF" browsers. But when I try that in IE8 it shows the downloaded zip file as a file of unknown type. I will have to open it with rar application specifically but I am looking to make this zip file download directly as .zip file in IE8 as it is happening in the other browsers. The following are the headers I used for it:
header("Pragma: public");
header("Pragma: no-cache");
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/zip");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($archive_file_name));
header("Content-Disposition: attachment; filename=$archive_file_name");
readfile("$archive_file_name");
unlink($archive_file_name);
Can someone please let me know where I need to correct the code? Thanks
header("Content-Disposition: attachment; filename=$archive_file_name");
Make sure $archive_file_name is just a name not a whole path, also it needs to be url encoded and should probably have a ".zip" extension at the end.
header("Content-type: application/zip");
Why is the t in "type" lower cased?
You are overwriting some headers, some are needed just for IE, some for other browsers, IE is more picky.
Look at this link:
http://www.boutell.com/newfaq/creating/forcedownload.html