I have Excel spreadsheets stored in a MySQL table longblob field. I need to retrieve this data and then stream it to the user as a downloadable file, preferably without writing it to disk first.
Possible?
Edit - Eh, just figured it out... Posted in answer below.
function getfile($blockid)
{
global $msa_db;
$sql = "select filename, filedata from blocks where blockid = '$blockid'";
$query = mysql_query($sql, $msa_db);
$result['filename'] = mysql_result($query,0,0);
$result['filedata'] = mysql_result($query,0,1);
return $result;
}
function download($fileinfo)
{
$file = base64_decode($fileinfo['filedata']);
header("Cache-Control: no-cache private");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.$fileinfo['filename']);
header("Content-Type: application/vnd.ms-excel");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. strlen($file));
echo $file;
exit;
}
$fileinfo = getfile($blockid);
download($fileinfo);
Just got the solution
http://www.codeproject.com/Articles/831185/CREATE-DOWNLOAD-ZIP-FILE-USING-PHP
$data = base64_decode($data);
header("Cache-Control: no-cache private");
header("Content-Description: File Transfer");
header("Content-disposition: attachment; filename='".$identifier.".zip'");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. strlen($data));
header("Pragma: no-cache");
header("Expires: 0");
ob_clean();
flush();
echo $data;
Related
<body>
<?php
$zip = new ZipArchive;
if ($zip->open(getcwd() .'/read.zip', ZipArchive::CREATE) === TRUE) {
$zip->addFile(getcwd() . '/read.txt','/newname.txt');
$zip->close();
$file = getcwd() . '/read.zip';
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"read.zip\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile($file);
echo 'ok';
} else {
echo 'failed';
}
?>
</body>
I have following code and want to download zip file automatically that is being produced by this code when we run this page.
u need to add header for the client about file downloading. just see the manual example: http://php.net/manual/en/function.header.php#example-5315
<?php
$file = getcwd() . '/read.zip';
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"read.zip\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
ob_end_flush();
#readfile($file);
I have the following code in an attempt to have it so a specific file on the server is downloaded:
$file = 'upload/Order.txt';
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
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-Disposition: attachment; filename="basename.$file);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
All that happens is it causes the browser to download a file, in my case "Order.txt", but its blank, it is just creating this file out of no where. It doesn't do anything with the "upload/" path portion of $file.
Thanks for any help, I find headers really confusing, why I can't just have "download ($file)" is beyond me.
Edit: I'll struggle over a problem for days, finally decide to ask here then immediately after fix it my self.
I changed the location of the file to be in the same as the PHP script, I'd still like to know how to make it work with them being separate however. I also added:
readfile($file);
If I alter with of these 2 changes it doesn't work.
Also slim lined it, new code:
$file = 'Order.txt';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
you should print content of file to stdout after headers:
$file = 'upload/Order.txt';
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
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-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
echo $contents;
Try change this line:
header("Content-Disposition: attachment; filename='".basename($file)."';");
I want to read an excel file with php and send its content to the client. What I really want to do is by changing the content type and other html headers force browser to download the file instead of showing its content. Everything is ok and user can download the file but when he tries to open it contents are not displayed in right format and shape. The thing which comes to my mind is that I should not send response in unicode string and have to do it in binary format which excel can know it. This is my current code :
$filename = $_GET['file'];
$ext = $_GET['type'];
$filename .= '.' . $ext;
$path = "../generatedReports/" . $filename;
header('Content-type: application/vnd.ms-excel;');
header('Content-Disposition: attachment; filename=' . $filename);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Description: File Transfer");
header("Accept-Ranges: bytes");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($path));
$size = filesize($path);
$f = fopen($path, 'r');
$content = fread($f, $size);
echo $content;
Any solution?
Please try with the below code,
echo unicode2utf8(hexdec("00CE")); // Result: Î
// Or the function that will recognize U+ in front of string, and will skip it to show the character
function unicodeCodePointToChar($str) {
if (substr($str,0,2) != "U+") return $str;
$str = substr($str,2); // Skip U+
return unicode_to_utf8(array(hexdec($str)));
}
echo unicodeCodePointToChar("U+00CE"); // Result: Î
my problem was solved by this answer. I've changed my code to this :
$filename = $_GET['file'];
$ext = $_GET['type'];
$filename .= '.' . $ext;
$path = "../generatedReports/" . $filename;
header('Content-type: application/vnd.ms-excel;');
header('Content-Disposition: attachment; filename=' . $filename);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Description: File Transfer");
header("Accept-Ranges: bytes");
header("Content-Transfer-Encoding: binary");
ob_clean(); // discard any data in the output buffer (if possible)
flush(); // flush headers (if pos
readfile($path);
and everything got nice.
$path = BASE_URL."/pdf/";
$filename= $path.basename($_GET['download_file']);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment;
filename='.basename($filename));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);
exit;
This code works but I'm getting Error in reading pdf file when opening the downloaded pdf. In the above code I get the file from the location http://localhost//eec//pdf/CV_Prabin Mishra.pdf
BASE_URL probably has a slash at the end, so you don't need the extra one:
$path = BASE_URL."pdf/";
I don't see where you ever set the value of $filename, you set the location of the PDF in $fullPath, but then use $filename to read it out. I think the code should be
$path = BASE_URL."/pdf/";
$fullPath = $path.basename($_GET['download_file']);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment;
filename='.basename($fullPath));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($fullPath));
readfile($fullPath);
exit;
am integrating force download using php. when i download the flv files and open in video players its shows error.
$Filename = '/* file name */';
header('Content-Description: File Transfer');
header("Content-type: video/flv");
header("Cache-Control: no-cache");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename=test.flv' );
header("Pragma: no-cache");
header('Content-length: '.filesize($Filename));
$hFPi = fopen ("$Filename", "rb");
while (!feof($hFPi))
{
$sBuf = fread ($hFPi, filesize($Filename) );
echo $sBuf;
}
fclose ($hFPi);
Please help..
$Filename = '/* file name */';
$path = "../../upload/";
$file = $path.$filename;
savedata1();
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/flv");
header("Content-Transfer-Encoding: binary");
readfile($file);
this will help u. Try it in ur code and u will find ur answer...
This code working great to download mp4, flv, mpeg file from my end.
Here:
$file_name = The Downloaded video file name,
$file_path = Base Path of the original file in the server.
header('Pragma: public'); // required
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Type: video/mpeg");
header("Content-Transfer-Encoding: binary");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0', false);
header('Cache-Control: private', false); // required for certain browsers
ob_clean();
flush();
readfile($file_path);
exit;
Try
$Filename = 'test.flv';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: video/flv");
header("Content-Disposition: attachment; filename=".basename($filename).";" );
header("Content-Transfer-Encoding: binary");
readfile("$Filename ");
This is what I do to force a download of a file (for me it's ZIP but it should work for any file type though). Keep in mind you'll have to tweak it as I store my db information differently than you probably do.
header("Content Description: File Transfer");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must revalidate");
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename(".".$filepath)."");
header("Content-Length: " . filesize(".".$filepath));
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
readfile(".".$filepath);