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 :)
Related
I'm learning PHP so this is the question for education purposes. Since I can't find an answer in tutorials I use, would be nice from you to make it clear for me.
So, imagine we have a file "text.txt" and content is:
"Hello World!"
The following PHP script:
<?php
echo readfile("text.txt");
?>
Will output "Hello World!12" - I can't think of any cases when such an output can be useful, but I found that if I don't want to see the file length at the end, I've to omit "echo":
<?php
readfile("text.txt");
?>
The output will be "Hello World!". This is a way better, but manual says: "Returns the number of bytes read from the file.", so my question is - How am I supposed to get the file length using the readfile() function? According to my logic it "returns" the file content but I feel like I didn't get something right. Please help me to figure this out.
So you want to read the size of a file using readfile()? Sure, but this function also outputs the file. No biggie, we have something we can use in this situation: output buffering.
<?php
ob_start();
$length = readfile("text.txt");
// the content of the file isn't lost as well, and you can manipulate it
$content = ob_get_clean();
echo $length;
?>
readfile is not used to get file size or file content the way you write. It is typically used to send a file to the client. For example, suppose that you have created a pdf file in your web application after the client submit a form or clicked some link. Sometimes you can direct them to the file directly, but sometimes you dont want that for some reasons(security etc.). This way you can do this:
How it is ment to be used.
$filepath = "../files/test.pdf";
header("Content-Description: File Transfer");
header("Content-Type: application/pdf; charset=UTF-8");
header("Content-Disposition: inline; filename='test.pdf'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($filepath));
readfile($filepath);
exit;
An example for which you may use it.
$filepath = "../files/test.pdf";
ob_start();
$filesize = readfile($filepath);
$content = ob_get_clean();
header("Content-Description: File Transfer");
header("Content-Type: application/pdf; charset=UTF-8");
header("Content-Disposition: inline; filename='test.pdf'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $filesize );
echo $content;
exit;
So, here you output the file content in addition to the correct headers so that the browser will identify it as a pdf file and open it.
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 :)
I have a code that save the data of a form into a TXT file and it seems like Internet Explorer can't read the file once it is closed.
The file is successfully saved in my folder and when I load it from the FTP I can see my values, but when I do readfile() on the "submit", the donwloadable file is empty from data.
Here is my code :
$fp = fopen("plan_de_concepts/". $nom_du_fichier, "w");
$savestring = "$sujet=*|*=$concept1=*|*=$concept2=*|*=$concept3=*|*=$c1mc1=*|*=$c1mc2=*|*=$c1mc3=*|*=$c1mc4=*|*=$c1mc5=*|*=$c2mc1=*|*=$c2mc2=*|*=$c2mc3=*|*=$c2mc4=*|*=$c2mc5=*|*=$c3mc1=*|*=$c3mc2=*|*=$c3mc4=*|*=$c3mc5=*|*=$c4mc1=*|*=$c4mc2=*|*=$c4mc3=*|*=$c4mc5=*|*=$c5mc1=*|*=$c5mc2=*|*=$c5mc3=*|*=$c5mc3=*|*=$c5mc4=*|*=$c5mc5=*|*=$c6mc1=*|*=$c6mc2=*|*=$c6mc3=*|*=$c6mc4=*|*=$c6mc5";
fwrite($fp, $savestring);
fclose($fp);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize($nom_du_fichier));
header("Content-Disposition: attachment; filename=". $nom_du_fichier);
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile('plan_de_concepts/'.$nom_du_fichier);
Works great on Firefox and Chrome but IE return empty.. Is there any ways I could force to donwload the "uploaded" version of the file?
You use filesize($nom_du_fichier) and readfile('plan_de_concepts/'.$nom_du_fichier) (note the folder-prefix). I expect only one of the files exists.
It looks like IE is actually using the Content-Length-header where FF is silently ignoring it.
My Content-length was different of the file exact path. Internet Explorer could not find the file to read it.
It should be noted that in the example:
header('Content-Length: ' . filesize($file));
$file should really be the full path to the file. Otherwise content
length will not always be set, often resulting in the dreaded "0 byte
file" problem.
Source : PHP readfile returns zero length file
I want to allow a user to download a pdf file, the download code is below....for some odd reason even though the file is being downloaded I get an error saying that the file has been damaged on the server...Could someone help me and point out where I am making my mistake.
<php
$name = $_POST["name_first"];
$mail = $_POST['email'];
$number = $_POST['phone_number'];
$email_message = "first name: {$name} email is {$mail} number is {$number} ";
mail('fanaa#gmail.com', 'Form Response', $email_message);
if ($mail == "" OR $name == "" OR $number == "")
{
echo "Enter valid details ";
}
else
{
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="tokina.pdf"');
readfile('docs/tokina.pdf');
}
?>
I used this code to download pdfs:
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octetstream');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
readfile("$file");
}
This should be fine, and make sure there are no spaces or return characters (don't escape php at all is the best solution).
If you find your still having problems, open the corrupted file with notepad (there may be a php error warning inside).
Hope this helps!
Remove the headers and look at the page, do you see any error messages? If PHP outputs anything else than the actual PDF source, the file will appear to be corrupted.
header('Content-type: application/pdf');
enable PHP extension php_gettext and you are done.
try taking out the double quotes in
header('Content-type: "application/octet-stream"');
so it becomes
header('Content-type: application/octet-stream');
Maybe your content-type is not correct. try this one:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
Your PDF file tokina.pdf is either not uploaded or not in the same directory as the PHP file. That's why it's saving as "tokina.pdf.htm" - it's loading the HTML for a 404 page instead. That is why your browser/PDF viewer thinks the file is "corrupted" - because its extension is PDF but its contents are not.
Make sure the file is uploaded, and if it is, make sure readfile is pointing to the correct path. If it's not in the same folder, use a relative/absolute path, for example:
readfile('docs/tokina.pdf');
And yes, the content type should be application/pdf
Using this script
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filenamepath));
readfile($filenamepath);
I had the same problem. Comparing the original file and the downloaded file with a hexadecimal editor like UltraEdit, I found some characters at the beginning of the corrupted file.
The problem was that after ?> marking end of PHP code there were line terminators several times in my code.
Remove all the line terminators after ?> and read also the forum article Downloaded Files are corrupt - Common Problem. That worked for me.
I hope that can help you.
I use
$download_path = your path (where to look for the files)
set_time_limit(0);
$file_url = $download_path . $data['link'];
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file_url). '"');
//then to read the file
readfile($file_url);
this usually works for me
I have a PHP file that generates xls files using the module found at http://pear.php.net/package/Spreadsheet_Excel_Writer/
I can create the sample document just fine and when I open it, it looks fine.
My next step it to turn it into a downloadable link. To do that, I did this:
$mimeType = "application/vnd.ms-excel";
$file_name = "test.xls";
$file_path = "/tmp/".$file_name;
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-Type: application/' . $mimeType);
header('Content-Length: '.$size);
header("Content-Disposition: attachment;filename=$file_name ");
header("Content-Transfer-Encoding: binary ");
// open the file in binary read-only mode
// display the error messages if the file canĀ“t be opened
$file = & fopen($file_path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
When I download the file however, it contains a lot of garbage data both in Excel and OpenOffice. The diff says that then binary file in the /tmp folder and the downloaded file are different from each other. I'm guessing that it has something to do with the headers or with fpassthru but I haven't had much luck with debugging the issue.
Any ideas on what the problem is?
The multiple Content-Type headers are uncessary. You're essentially saying that the file is a muffin and a pizza and a ford taurus all at the same time. All you need is the application/octet-stream version, unless you want to serve up the exact mime type.
As well, is there any reason you're trying to turn the file handle returned by fopen() into a reference?
Try something simpler:
<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment;filename=$file_name");
readfile("/tmp/test.xls");
exit();
?>
and see if that does any better.
Just make sure that you don't send ANYTHING out to the browser BEFORE the actual file content gets send.
It might just be some php 'error' or even 'notice' that Spreadsheet_Excel_Writer is producing and you don't even see. Or it might be a closing '?>' tag thats followed by s simple space or newline.
I had a similar error where the file that was generated inside the web folders were working. However the delivery using header('...') gave me corrupt files. This was due to a single space at the end of one php file after the closing '?>' tag.
I am using the same library and I just discovered that the files in the library itself are creating the whitespace.
Solution: In the following files remove the whitespace at the end of the file, or remove the ?> closing tag at the end.
Files to edit (all files in the Spreadsheet_Excel_Writer package):
Writer.php
Workbook.php
Worksheet.php
PPS.php
Parser.php
OLE.php
Parser.php
File.php
BIFFWriter.php
Validator.php
Root.php
Add the following code at the top of the page where the excel file is generated
ob_clean();
This would clear all the gibberish data.Also check for any echo statements.If echo statements are present, remove them. The data should always present in format specified by excel package.