send zip file to browser / force direct download
<?php
// or however you get the path
$yourfile = "/path/to/some_file.zip";
$file_name = basename($yourfile);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($yourfile));
readfile($yourfile);
exit;
?>
force to download zip from server is not working
you need to change role in .htaccess
add application/octet-stream zip
PHP to download zip file works when called directly but not from web application
The PHP script is sending the actual binary source of the zip file, while the AJAX request is trying to display it as text, that's the garbage you get.
If you really want to keep the AJAX (it would be more simple to just make a <form>
that is sent to download.php
), you could do one two things:
Just print the URL to
$zipFile
in PHP, and then when you get the response redirect to it withwindow.location.href=xhr.responseText;
.Save the response as a zip file. See How to save binary data of zip file in Javascript?
php - How to force download of a file?
You could try something like this:
<?php
// Locate.
$file_name = 'file.avi';
$file_url = 'http://www.myremoteserver.com/' . $file_name;
// Configure.
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
// Actual download.
readfile($file_url);
// Finally, just to be sure that remaining script does not output anything.
exit;
I just tested it and it works for me.Please note that for readfile
to be able to read a remote url, you need to have your fopen_wrappers
enabled.
Create a zip file and download it
Add Content-length
header describing size of zip file in bytes.
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Content-length: " . filesize($archive_file_name));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
Also make sure that there is absolutely no white space before <?
and after ?>
. I see a space here:↓
<?php
$file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf');
Client download of a server generated zip file
I updated my bulkdownload
method to use $window.open(...)
instead of $http.get(...)
:
function bulkdownload(titles){
titles = titles || [];
if ( titles.length > 0 ) {
var url = '/query/bulkdownload?';
var len = titles.length;
for ( var ii = 0; ii < len; ii++ ) {
url = url + 'titles=' + titles[ii];
if ( ii < len-1 ) {
url = url + '&';
}
}
$window.open(url);
}
};
I have only tested this in IE11.
Related Topics
Object Copy Versus Clone in PHP
Facebook Graph API - Friends Using Application
How to Skip Elements in Foreach Loop
PHP Curl: Curlopt_Connecttimeout VS Curlopt_Timeout
PHP Explode Array Then Loop Through Values and Output to Variable
Different Timezone_Types on Datetime Object
PHP - What's the Benefit of Unsetting Variables
Can a PHP File Name (Or a Dir in Its Full Path) Have Utf-8 Characters
Add Some Attribute Values to Woocommerce Variable Product Title from Chosen Variation
How to Use Etags in a PHP File
How to Tag a User in a Photo Using the Facebook Graph API
Flip (Transpose) the Rows and Columns of a 2D Array Without Changing the Number of Columns
Why Use Output Buffering in PHP