I'm trying to download a zip file without revealing the URL to the user. The file should only be accessible by authorized users.
Currently I'm doing this:
// check authentication and request method etc...
// ...
$file = '../../filename.zip';
$filesize = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: private');
header('Content-Length: ' . $filesize);
ob_clean();
flush();
echo readfile($file);
die();
And I can see the file is downloaded somehow in the Chrome network tab (it shows in the Response tab) and ~3MB are transferred (which is the filesize), but the file isn't downloaded in the browser.
I'm doing a simple GET request, btw.
Thanks for your help,
Tobias
EDIT: Okay, apparently I also had an error on the client side and now the file is being downloaded in the browser, but it's only a 1KB zip containing Response with status: 200 OK for URL: xxx.
My request looks like this:
let headers = new Headers({'Accept': 'application/zip'});
let options = new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob });
this.authHttp.post(environment.apiUrl + 'rewards/download/', data, options)
.map(res => new Blob([res],{ type: 'application/zip' }))
.catch((error:any) => Observable.throw(error.json().error || 'Server error'))
.subscribe(
data => {
//console.log(data);
window.location.href = window.URL.createObjectURL(data);
},
error => {
console.log("Error downloading file.");
},
() => console.log("Download completed."));
EDIT2: Okay, got it. Apparently I had to use .map(res => new Blob([res.json()],{ type: 'application/zip' })) in the request. Also now I'm using FileSaver.js instead of window.location, but I don't think that affected the problem.
i think you just need to redirect ur method to your physique zip file.
i mean you need just to add :
header("Location: $url_physique_of_ur_zip_file");
before
ob_clean();
flush();
and replace $url_physique_of_ur_zip_file by your physique url of your file
i wish u understand me because this solution work for me
Okay, got it. Apparently I had to use .map(res => new Blob([res.json()],{ type: 'application/zip' })) in the request. Also now I'm using FileSaver.js instead of window.location, but I don't think that affected the problem.
Related
I have to trigger a download of a zip file ( The Zip file is inside my data folder).
For this i am using the code,
$file = 'D:\php7\htdocs\Project\trunk\api\data\file.zip';
header('Content-Description: File Transfer');
header('Content-type: application/zip');
header('Content-disposition: attachment; filename=' . basename($file) );
readfile($file);`
This is working in core php as i expected. But when i am using the same code in the Zend prints a content like below,
PKYsVJ)~�� study.xlsPKYsVJs�����+
tutorial-point-Export.xlsPKYsVJn��� 8��Zabc.xlsP
In between the content i can see the name of all files in the zip. But it is not getting downloaded.
After i realised that this is not working i started searching about it and Found some solution from stack over flow
Try 1: Adding different header element and ob functions in every random lines
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_start();
ob_clean();
flush();
All these are tried from different stack overflow Question and answers and have the same result
Try 2:PHP is reading file instead of downloading . This question do not have any accepted answer (He was asking about the core php but i have the same issue with zend only) . I tried all of this but it was not working.
Try 3:Changing the .htaccess . After that i thought it was a problem with my .htaccess and found this answer for changing the .htaccess file.
<FilesMatch "\.(?i:zip)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
This also given me the same result.
Try 4:Using download functions in Zend . I have tried the all the zend functions in the answer of this question. But given me an empty output even the file was not read.
Try 5: Remove all the unwanted spaces before and after the php tag as per the answer
Is there any other way to trigger a download in ZF2 framework?
EDIT
Below is my exact function. This is GET(API) function,
public function getList(){
try{
//here i am getting the zip file name.
$exportFile = $this->getRequest()->getQuery('exportid','');
$file = 'D:\php7\htdocs\Project\trunk\api\data\\' . $exportFile . '.zip';
header('Content-Description: File Transfer');
header('Content-type: application/zip');
header('Content-disposition: attachment; filename=' . basename($file) );
readfile($file);
return new JsonModel(["status"=>"Success"]);
} catch(\Exception $e){
return new JsonModel(["status"=>"Failed"]);
}
}
There are two problems here:
your browser trying to open the file, instead of downloading it.
also, it is not opening the file correctly.
Both point to a Content-Type error. Verify that the Content-Type being received by the browser is correct (instead of being rewritten as, say, text/html).
If it is, change it to application/x-download. This might not work in Internet Explorer, which performs some aggressive Content-Type sniffing. You might try adding a nosniff directive.
Additionally, after a readfile (and you might be forced to return the file's contents instead of readfile()'ing - i.e., return file_get_contents($filename);), you should stop all output with return null;. ZIP file directory is at the very end, so if you attach a JSON message there, you risk the browser neither downloading the file, nor displaying it correctly.
As a last resort, you can go nuclear and do everything yourself. Extremely non-elegant, and all frameworks ought to provide an alternative, but just in case...
// Stop *all* buffering
while (ob_get_level()) {
ob_end_clean();
}
// Set headers using PHP functions instead of Response
header('Content-Type: application/x-download');
header('X-Content-Type-Options: nosniff');
header('Content-Length: ' . filesize($filename));
header('Content-Disposition: attachment; filename="whatever.zip"');
die(readfile($filename));
It's possible that some creative use of atexit handlers or destructor hooks might mess up even this last option, but I feel it's unlikely.
Based on this SO answer, you can try the following modification to your function.
public function getList(){
try{
//here i am getting the zip file name.
$exportFile = $this->getRequest()->getQuery('exportid','');
$file = 'D:\php7\htdocs\Project\trunk\api\data\\' . $exportFile . '.zip';
if (file_exists($file)) {
$response = new \Zend\Http\Response\Stream();
$response->setStream(fopen($file, 'r'));
$response->setStatusCode(200);
$response->setStreamName(basename($file));
$headers = new \Zend\Http\Headers();
$headers->addHeaders(array(
'Content-Description' => 'File Transfer',
'Content-Disposition' => 'attachment; filename="' . basename($file) .'"',
'Content-Type' => 'application/zip',
'Content-Length' => filesize($file)
));
$response->setHeaders($headers);
return $response;
//return new JsonModel(["status"=>"Success"]);
} else {
return new JsonModel(["status"=>"Failed. No such file in \"".$file."\""]);
}
} catch(\Exception $e){
return new JsonModel(["status"=>"Failed"]);
}
}
This worked for me!
ob_clean(); // Clear any previously written headers in the output buffer
$filepath = "some_file.zip";
$content_type = 'application/octet_stream';
$filetype = filetype($filepath);
$filename =$filepath;
if($filetype=='application/zip')
{
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$fp = #fopen($filepath, 'rb');
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: '.$content_type);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize(trim($filepath)));
}
else
{
header('Content-Type: '.$content_type);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize(trim($filepath)));
}
fpassthru($fp);
fclose($fp);
}
If you correct the capitalisation of the headers does it work? ie use Content-Disposition and Content-Type over Content-disposition and Content-type respectively?
Regardless, as standard debugging technique I would suggest using your browser dev tools to inspect the requests that are being made (inc headers) and comparing that to what ends up in your serverside code, and what is in the server side response and what ends up in the client. I would also validate this using a private-session (Incognito mode in Chrome etc) or a fresh profile / VM install just to eliminate anything else.
Also, why not use xsendfile and delegate the responsibility of sending the file to the web server so you aren't incurring the responsibility in your PHP code? You can do this with appropriate server configuration (sometimes through .htaccess, but in this day and age surely you have complete control anyway) and then simply setting the X-Sendfile header as per the example on the above link:
header("X-Sendfile: $path_to_somefile");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$somefile\"");
Because you are return JsonModel so your output will be a json with your message instead of buffering for downloading.
Edit: I notice that you was missing Content-Transfer-Encoding: Binary, tested on my os x - php5.6 env.
You should try this
public function getList(){
try{
//here i am getting the zip file name.
$exportFile = $this->getRequest()->getQuery('exportid','');
$file = 'D:\php7\htdocs\Project\trunk\api\data\\' . $exportFile . '.zip';
header('Content-Description: File Transfer');
header('Content-type: application/zip');
header('Content-disposition: attachment; filename=' . basename($file));
header("Content-Transfer-Encoding: Binary");
header("Content-length: " . filesize($file));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$file");
} catch(\Exception $e){
return new JsonModel(["status"=>"Failed"]);
}
}
Just remove your JSonModel on response.
You can try this for downloading the file instead of readfile();
Server side -
file_put_contents("file.zip", fopen("http://someurl/file.zip", 'r'));
Client side -
<button>download file</button>
download file
I already found many questions here in SO about the theme, but any of them helps me with a specific problem.
I need to send a ajax request to PHP and then download a PDF file.
This is my Javascript
$('body').on("click",'.download-file', function(e) {
e.stopPropagation();
var id = $(this).attr('data-id');
$.ajax({
url: 'app/myPage/download',// The framework will redirect it to index.php and then to myPage.class and method dowload
data: id,
type: 'POST',
success: function (return) {
console.log(return);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error... " + textStatus + " " + errorThrown);
}
});
});
Here is my PHP file:
class myPage{
public function download()
{
$id = $_POST['id'];
$filePath = $this->methodToGetFile($id);
$name = end(explode('/',$filePath));
$fp = fopen($filePath, 'rb');
header("Cache-Control: ");
header("Pragma: ");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($filePath));
header("Content-Disposition: attachment; filename='".$name."';");
header("Content-Transfer-Encoding: binary\n");
fpassthru($fp);
exit;
}
}
With this approach, I have the PDF "printed" in the console like
%PDF-1.5%����1 0 obj<</Type/Catalog/Pages 2 0 R/Lang(pt-BR) /StructTreeRoot 8 0 R/MarkInfo<</Marked true>>>> ....
So, I'd like to know what should I do to open this file and download it.
In the linked questions I see something about window.location, but I don't know how can I use it.
I've already tried change the PHP headers to try force the download, but no success with it. In these cases I just receive null in the javascript nothing happens. Here are the modified headers:
header('Content-Type: application/pdf');//tried with this option
//header('Content-Type: application/octet-stream');// also tried with this other option
header('Content-Disposition: attachment; filename=' . $name);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
Are there some good approach to do this?
i don't think this approach to downloading a pdf will work. Javascript is sending the request, and php is sending the response. You want the response to go directly to the browser, not to javascript. You should change the download link to go directly to the download location. No ajax / javascript needed. Like this:
download
I am trying to download a CSV file with PHP but I can not make it to get the dialog box.
I have changed the headers and used the readfile() function as shown here
This is my code:
$nodeId = 'something';
$filename = "/var/www/dkar/ruralBroadband/ruralApp/rural/csvExports/$nodeId.csv";
header('Content-type: text/csv');
header('Content-disposition: attachment; filename ="report.csv"');
readfile($filename);
** EDIT **
Tried also with this as suggested:
header('Content-Disposition: attachment; filename="report.csv"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize('report.csv'));
When I am checking the response in Firebug I can see the whole CSV file returned. Also the headers change into:
Content-Type text/csv
Content-disposition attachment; filename ="report.csv"
So I can not figure out why I dont get the dialog box to save the file.
Add these headers:
header('Content-Disposition: inline; filename="report.csv"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));
After all I didn't use the above method and I did that, which works fine and its more straight forward:
var ajaxurl = 'exportNodeData.php', // script to run
data = {nodeId:nodeId}; // data to pass
$.post(ajaxurl, data, function (response) {
document.location.href = 'https://www.ruralauditor.gr/csvExports/'+nodeId+'.zip';
});
This is the ajax request, which I execute in order to create the CSV file (which is also zipped). I then use this line:
document.location.href = 'https://www.ruralauditor.gr/csvExports/'+nodeId+'.zip';
in order to force the download of the file.
I'm using phpWord to create a word document on the fly in a php script that is called using XMLHttpRequest. I'm trapping the response to the request, and then attempting to prompt the user to download or open the file. My phpWord code creates the file OK (I can open the file on the server), and the browser prompts the user to open or save the file, but the file that is downloaded is corrupted somehow. It gives an error like "We're sorry. We can't open results.docx because we found a problem with its contents".
On the server side I have this code:
...
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document' );
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
flush();
readfile($filename);
On the client side I have this code:
function getDOC()
{
url='services/doc_search_results_service.php/';
var req = null;
var postParms = '';
req = new XMLHttpRequest
if ( req )
{
req.open( 'POST', url, true );
req.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
req.setRequestHeader( "Content-length", postParms.length );
req.setRequestHeader( "Connection", "close" );
req.onreadystatechange = function()
{
if ( req.readyState == 4 && req.status == 200 )
{
downloadDOC( "results.docx", req.responseText );
}
}
req.send( postParms );
}
}
function downloadDOC(filename, text)
{
var pom = document.createElement('a');
pom.setAttribute('href', 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom);
}
Would love to know if anyone can spot where I'm going wrong. I'm not an expert in php nor javascript, so any help would be gratefully received. FWIW I think it may be to do with headers. I'm also guessing that it may well be a client side problem as the file is create OK on the server side.
Thanks in advance. :-)
Thanks to #Musa for the suggestion to use 'GET' rather than 'POST' which worked perfectly.
As #Musa suggested, the client-side javascript in this example should be:
window.location = 'services/doc_search_results_service.php/';
Which replaces all the javascript quoted in my original question.
Note - I'm not sure what the correct answer would have been if it had not been possible to use GET for some reason.
I have a js script that saves an image as .jpg in a specific folder on the server:
$data = substr($_POST['imageData'], strpos($_POST['imageData'], ",") + 1);
$decodedData = base64_decode($data);
$fp = fopen("imgdownload/cc.jpg", 'wb');
fwrite($fp, $decodedData);
fclose($fp);
The next step would be for the user to save it on his/her disk, the best would be to open a dialog box "save as" and choose the name and location, but just forcing download to a set location would be a dream.
I've tried:
$file = 'imgdownload/cc.jpg';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: image/jpg');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
And other things without success. What am I doing wrong and how to do it right?
EDIT - this is my JS:
savePicture: function() {
$(this.el).find('canvas').attr('id', 'myCanvas');
var data = document.getElementById("myCanvas").toDataURL();
$.post("api/process.php", {
imageData: data
}, function(data) {
//window.location = data;
});
},
You cannot trigger a download in response to a background AJAX request. You'll have to direct the main browser to the URL where the download occurs. E.g., in your AJAX callback:
window.location = '/download.php';
That means you'll need to store the file server-side in the AJAX upload request to then have it available for download in the separate following request to download.php somewhere.