The binary data is simply the actual file, or rather the important contents of that file, just without file name.
$base64 = /* some base64 encoded data fetched from somewhere */;
$binary = base64_decode($base64);
And there you have the file data/contents of the file in the $binary variable. From here, it depends on what you want to do. You can write the data to a file, and you get an "actual" PDF file:
file_put_contents('my.pdf', $binary);
You can spit the data out to the browser with an appropriate header, and the user will receive something that looks like a PDF file to him:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $binary;
it generates a pdf file but it not opening..it show error occur.
//below is the php file
<?php
$base64 = "/here binary code/";
$binary = base64_decode($base64);
file_put_contents('my.pdf',$binary);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $binary;
?>
then how to compare downloaded file with original file.
I think your problem is that you don't have proper data in $base64 variable. You're simply making PDF response, but content of that response is not PDF file.
(You can try to read content from some real PDF, fill this data to $binary, and it should work.)
Related
I am using fputcsv function to export data as CSV file but it is displaying all data in browser instead of downloading this as CSV file. Here is my code
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=export.csv');
$output = fopen('php://output', 'w');
fputcsv($output, $column_headings);
fclose($output);
Data is displaying correctly in browser but I want to download this as export.csv file.I do not know what is wrong with this. Any suggestion regarding this will be appreciated.
You cannot control how the browser handles the text/csv content type, however, you can tell the browser it should treat the data as binary.
header('Content-Type: application/octet-stream');
I added ob_start(); at the start of my file and now I can export files.
I'm trying to output data returned by an MS SQL query to an Excel or CSV file with PHP.
I've used the script in this answer and can output the file OK. Without the header lines (at the bottom of my code) it saves in my server's folder structure rather than outputs as a download to the browser.
If I add the header lines, it ouputs to a CSV file but writes the page's HTML to the file rather than the extract from the database! Am I missing a setting somewhere? I tried running the code on a page with no HTML in it (PHP and SQL code only), but it still happens.
// Give the file a suitable name:
$FileName= $PartNumber.".csv";
$fp = fopen($FileName, 'w');
// Connect to MS SQL server; the actual database is chosen in the form
// ConnSQL defined in inc/dbconn/config.php
ConnSQL($idDatabase);
// the query is a biggie; here it is:
require 'inc_sql.php';
// run it through the SQL server
$rstBOM = sqlsrv_query($GLOBALS['ConnSQL'], $sqlBOM);
while ($export= sqlsrv_fetch_array($rstBOM, SQLSRV_FETCH_ASSOC)) {
if (!isset($headings))
{
$headings = array_keys($export);
fputcsv($fp, $headings, ',', '"');
}
fputcsv($fp, $export, ',', '"');
}
// force download csv - exports HTML to CSV!
header("Content-type: application/force-download");
header('Content-Disposition: inline; filename="'.$FileName.'"');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ". filesize($FileName));
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="'.$FileName.'"');
fclose($fp);
Any ideas where I'm going wrong please?
You need to output your csv file to the browser simply by putting
readfile($FileName);
At the end of your code after the fclose($fp); function.
Otherwise, browser receives the headers for files, but no content in sent from your PHP code.
You could also generate your csv file on the fly and just echo $csvFileContents; instead. This would prevent server from creating and writing data to file, which could lead to security breaches.
Good luck!
We are receiving file data from a third party API call that returns the content type in one variable and the file contents in another. The file contents are "garbage code." For example:
$doc->fileContents = "A�[�j쎪A����Kb����m��= ....";
$doc->fileType = "application/msword";
I need to be able to provide my users a clickable method for downloading the file, but simply writing the contents to a file and forcing a download with that content type simply opens a file containing all that garbage... not the real file contents.
EDIT: Per Patrick Q's request, I did it the standard fwrite way:
$fp = fopen($filename, 'w'); // filename is generated elsewhere
fwrite($fp, $doc->fileContents);
fclose($fp);
So the question is, how do I create a real, usable file out of this garbage?
The data looks like binary file data. There's no need to write it to a file first, just make sure you set the Content-Transfer-Encoding header to binary when you send it to the browser for downloading.
I'm assuming you already have a way of mapping $doc->fileType to a filename with the proper extension, I'll just hardwire .doc here since your example is an application/msword document.
// your logic here to determine the filename sent to the browser
$filename = "file.doc";
// set the minimum appropriate HTTP headers
header('Content-Type: ' . $doc->fileType);
header('Content-Length: ' . strlen($doc->fileContents));
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary'); // this is an important one
// send the file and stop
echo $doc->fileContents;
exit;
If you are writing it to file first, make sure you open it in binary mode:
$fp = fopen($filename, 'wb');
fwrite($fp, $doc->fileContents);
fclose($fp);
I'm trying to force download a pdf file that I'm generating. I don't need the pdf file to be actually saved on the server.
So when I generate my pdf file, I get the file content. I then encode it with base64. Now the problem is that I need to force download it. I've looked all over the web, but I haven't found any search results that tells me how to do this without the file actually being placed on the site.
I've tried the following code:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
header("Content-Length: ".filesize($pdffile));
readfile(base64_decode($pdffile));
But, it's giving me a corrupt pdf file, (1 kb). The actual file should be around 50kb.
Any ideas, as to what I can try?
readfile trying to output content from file, but you have only data string. Try this instead:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
echo base64_decode($pdffile);
I also suggest rename $pdffile to $pdfcontent for even better clarification.
I have a php file which converts the form data to csv format and then it should get downloaded automatically to the user's local download folder.
$time = time();
$filename = 'exceldownloads/myreport_'.$time.'.csv';
$file = fopen($filename,'w');
fputcsv($file,$rowexcel);
The above code works fine and stores the csv file in the specified folder in server. But my requirement is to download it to a local folder. I have seen many solutions to the above problem, but they are working only if we know the local destination folder. However, My requirement is to make it downloadable to the end-user local download's folder (whose download location Im unaware of). Is there anyway to get it downloaded on to the end user system without specifically mentioning the destination path.
You can export the output of your web page as an attachment, which will be shown as a download to the user. You can do this by outputting appropriate headers right before you make any output to the user.
Here's an example, that creates a download of a CSV file called foo.csv:
header("Content-type: text/csv");
header("Content-Disposition: attachment;Filename=foo.csv");
After outputting the headers, you just output all of the file's data to the page content.
*Edit: * Here's a working snippet, as requested:
header("Content-type: text/csv");
header("Content-Disposition: attachment;Filename=foo.csv");
echo implode(";", $rowexcel) . "\r\n"; // you should expand this accordingly
alternatively, here is another snippet, based on your code:
$filename = 'myreport'.time().'.csv';
$f = fopen($filename,'w');
fputcsv($f,$rowexcel);
header('Content-type: application/csv');
header('Content-Disposition: attachment;filename="'.$filename.'"');
readfile($filename);
If you are not getting any download, make sure that you don't output anything before the header() calls. Also, make sure that you don't have any UTF8 BOM bytes at the beginning of your PHP file, as these can be misinterpreted for output