PHP header() Can not open xlsx file after download - php

I use header() to download an xlsx file from given url. The file is downloaded but I can't not open it. It shows error
Below is my code
$url = "http://example.com/attachment/file.xlsx"
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Disposition: attachment; filename=Test.xlsx');
readfile($url);
exit();

Randy, your question looks weird. The URL in the serve response is way different than the one in your code.
Before commencing the download - sending the headers, do a is_file() or other check on the URL and only start the download if the file exists.
I suspect you are trying fopen on URL, not local file and the URL may be either incorrect or on server not allowing fopen on URLs.
Sample:
$url = "http://example.com/attachment/file.xlsx";
if (!fopen($url,'r')) exit('File/URL not accessible');
else fclose($url);
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename=Test.xlsx');
readfile($url);
exit();

Related

Download PDF from Binary SQL SERVER With PHP

I need help with this script. I'm trying to get this binary file stored in a sql server DB . The main problem is that , each time im trying to show it in my browser or to download it, the file is corrupted . Heres my code:
$binary = $row['PDF_FILE_STORED'];
file_put_contents('my.pdf', $binary);
header('Content-type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-Length: ".filesize($binary));
header("Content-Disposition: attachment;filename=my.pdf");
ob_clean();
flush();
echo $binary;
Is there a problem with the encoding aspect? I just got this warning in my brower's console : "Resource interpreted as Document but transferred with MIME type application/pdf" . Any advices ?
To just download the pdf file, you don't need to save it locally on the server. You don't need to send content-length as that should be done automatically and I would skip the content-transfer-encoding as well.
If there was no output yet, you can also skip dealing with the output buffer.
Try this:
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=my.pdf');
echo $row['PDF_FILE_STORED'];

Force PHP to download MP3 file from URL

I have mp3 file url and I want to make it downloadable for user.
I'm using below code but this is not working.
<?php
$file = 'http://r1---sn-2uja-pncz.googlevideo.com/videoplayback?pl=17&ip=39.33.149.149&ms=au&mt=1460124725&mv=m&id=o-ALL1b-5qDY-vJciSMtIjNoeHW6NSl0a67lvMj_IDTJ_V&mm=31&mn=sn-2uja-pncz&keepalive=yes&clen=3694396&pcm2cms=yes&sparams=clen,dur,gir,id,initcwndbps,ip,ipbits,itag,keepalive,lmt,mime,mm,mn,ms,mv,pcm2cms,pl,source,upn,expire&ipbits=0&initcwndbps=163750&mime=audio/webm&lmt=1404826626593256&source=youtube&dur=290.350&gir=yes&key=yt6&signature=07AF8F194980DA82DA85AC08FFCC99CA8E459336.38D33A3B46AD95087B779B646C8139A85EE55555&fexp=9405969,9416126,9416891,9420452,9422596,9423662,9426926,9427902,9428398,9429294,9429854,9431270,9432651,9433088,9433186,9433425,9433703&sver=3&upn=j6a9XvntKKY&expire=1460146538&itag=171';
// URL is not path of mp3 file but exact mp3 file
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>
Any suggestion why this is not working.
UPDATE
I try below code and it give forbidden 403 access.
header('Content-type: application/mp3');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
These codes are working for .mp3 files not not direct url like this $file because there is not .mp3 but url which return mp3 file formate.
mp3 is playing well in this url, Now how can I download it.

PHP Download - File emty [duplicate]

The end goal is for the user to download a .csv file. Right now I'm just testing trying to download a simple text file: test.txt. The only thing in this file is the word "test".
Here is the HTML code for files_to_download.php
Test file: <a href='test.php?file=test.txt'>Test.txt</a>
Code for test.php:
if(!(empty($_GET["file"])))
{
$file_name = $_GET["file"];
$path = "path/to/file";
$fullPath = $path . $file_name;
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: inline; attachment; filename=\"$file_name\"");
header("Content-Type: text/plain");
header("Content-Transfer-Encoding: binary");
readfile($fullPath);
}
I've tried variations of the headers above, adding more and removing others. The above seem to be the most common recommended on this site.
I've also tried changing
header("Content-Type: text/plain");
to
header("Content-Type: text/csv");
and get same results: empty .txt or .csv file.
The files are not empty when I open them directly (file browser) from the server. I've checked the permissions of the files and they're both 644, so the entire world can at least read the files. The directory is 777.
Is there a configuration on the Apache server I need to specify that may not be or am I missing something above.
Thanks for looking!
In most cases the path is wrong
Read the text file, then echo the text out after your header() calls.
Here's how I have my csv download set up:
//downloads an export of the user DB
$csv = User::exportUsers();
header('Content-disposition: attachment; filename=userdb.csv');
header('Content-type: text/csv');
echo $csv;
Where exportUsers() creates the csv data. You can easily just replace $csv with the contents of your text file, then echo it out.
And as far as your text file, you can use file_get_contents() to get the contents of your file into a string. Then echo that string.
Try setting the content length of the file:
header('Content-Length: ' . filesize($file));
Also, please have this in mind: file inclusion
In my case, the path was correct. And the download-forcing was working on windows, but not mac.
I figured out after few tests that the header Content-Length was failing. I was using the function filesize on a full url, like :
$url_my_file = "http://my-website.com/folders/file.ext";
header('Content-Length: '.(filesize($url_my_file)));
I replace it by
$url_my_file = "http://my-website.com/folders/file.ext";
$headers = get_headers($url_my_file, 1);
header('Content-Length: '.($headers['Content-Length']));
And ... It's working now :)

pdf php download produces empty file

<?php
header('Content-disposition: attachment; filename=Booking.pdf');
header('Content-type: application/pdf');
readfile('http://mysite.com/Booking.pdf');
?>
why is the Booking.pdf file downloaded empty!??
mac and windows both say:
The file “Booking.pdf” could not be opened because it is empty.
checked google and stackoverflow, can't find relative info... has anyone experienced this before?
ps: I only found this forum post:'The online issue is a bit off topic I think, but is generally due to loading the PDF to a server in the ASCII mode of FTP rather than binary. That creates a corrupt file. Be sure to turn on binary transmission', but this is not true in this case as i can display the same pdf file in an iframe and it is not blank/empty.
You need to change
readfile('http://mysite.com/Booking.pdf');
To
readfile(__DIR__ . '/Booking.pdf');
Example
$file = __DIR__ . '/test.pdf' ;
header('Content-disposition: attachment; filename=Booking.pdf');
header('Content-type: application/pdf');
header("Content-length: ".filesize($file));
readfile($file);

Force download file with PHP giving empty file

The end goal is for the user to download a .csv file. Right now I'm just testing trying to download a simple text file: test.txt. The only thing in this file is the word "test".
Here is the HTML code for files_to_download.php
Test file: <a href='test.php?file=test.txt'>Test.txt</a>
Code for test.php:
if(!(empty($_GET["file"])))
{
$file_name = $_GET["file"];
$path = "path/to/file";
$fullPath = $path . $file_name;
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: inline; attachment; filename=\"$file_name\"");
header("Content-Type: text/plain");
header("Content-Transfer-Encoding: binary");
readfile($fullPath);
}
I've tried variations of the headers above, adding more and removing others. The above seem to be the most common recommended on this site.
I've also tried changing
header("Content-Type: text/plain");
to
header("Content-Type: text/csv");
and get same results: empty .txt or .csv file.
The files are not empty when I open them directly (file browser) from the server. I've checked the permissions of the files and they're both 644, so the entire world can at least read the files. The directory is 777.
Is there a configuration on the Apache server I need to specify that may not be or am I missing something above.
Thanks for looking!
In most cases the path is wrong
Read the text file, then echo the text out after your header() calls.
Here's how I have my csv download set up:
//downloads an export of the user DB
$csv = User::exportUsers();
header('Content-disposition: attachment; filename=userdb.csv');
header('Content-type: text/csv');
echo $csv;
Where exportUsers() creates the csv data. You can easily just replace $csv with the contents of your text file, then echo it out.
And as far as your text file, you can use file_get_contents() to get the contents of your file into a string. Then echo that string.
Try setting the content length of the file:
header('Content-Length: ' . filesize($file));
Also, please have this in mind: file inclusion
In my case, the path was correct. And the download-forcing was working on windows, but not mac.
I figured out after few tests that the header Content-Length was failing. I was using the function filesize on a full url, like :
$url_my_file = "http://my-website.com/folders/file.ext";
header('Content-Length: '.(filesize($url_my_file)));
I replace it by
$url_my_file = "http://my-website.com/folders/file.ext";
$headers = get_headers($url_my_file, 1);
header('Content-Length: '.($headers['Content-Length']));
And ... It's working now :)

Categories