Rectangle 27 1

PHP Zip file download issue?


function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
header('Cache-Control: public');  
header('Content-Description: File Transfer');  
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename="'.$filename.'"');
if(!file_exists($fileName)) {
   die('Zip file not found');
}
else {
   $file_content = file_get_contents($fileName);  // To download the ZIP file
   header('Cache-Control: public');  
   header('Content-Description: File Transfer');  
   header('Content-type: application/force-download');
   header('Content-Disposition: attachment; filename="'.$filename.'"');
   echo $file_content;
}
exit;

I also have edited my response, maybe using cURL instead of readfile() could be of help. I'm not 100% sure but you could give it a try.

I have edited my post with the response headers from Firebug. Still it doesn't work.

I've used your original code, as well as those included in my answer and both work. Only difference is my PHP version (which I doubt is the reason of the problem) and my platform: Server:Apache/2.4.6 (Unix) PHP/5.5.1. I'm wondering how is your export button calling this function? It's indeed pretty weird though, in theory everything you have should just "work".

It's still the same, I will be trying to see if it is related to zip files only by sending csv files.

Well the csv doesn't work as well, checked in all browsers its the same problem.

You could also try using cURL instead of readfile(), something like:

Note
Rectangle 27 1

PHP Zip file download issue?


function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
header('Cache-Control: public');  
header('Content-Description: File Transfer');  
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename="'.$filename.'"');
if(!file_exists($fileName)) {
   die('Zip file not found');
}
else {
   $file_content = file_get_contents($fileName);  // To download the ZIP file
   header('Cache-Control: public');  
   header('Content-Description: File Transfer');  
   header('Content-type: application/force-download');
   header('Content-Disposition: attachment; filename="'.$filename.'"');
   echo $file_content;
}
exit;

I also have edited my response, maybe using cURL instead of readfile() could be of help. I'm not 100% sure but you could give it a try.

I have edited my post with the response headers from Firebug. Still it doesn't work.

I've used your original code, as well as those included in my answer and both work. Only difference is my PHP version (which I doubt is the reason of the problem) and my platform: Server:Apache/2.4.6 (Unix) PHP/5.5.1. I'm wondering how is your export button calling this function? It's indeed pretty weird though, in theory everything you have should just "work".

It's still the same, I will be trying to see if it is related to zip files only by sending csv files.

Well the csv doesn't work as well, checked in all browsers its the same problem.

You could also try using cURL instead of readfile(), something like:

Note