i have this code for image download in php... works fine, image downloads but the problem is that it does not open in the place where it gets downloaded, and gives the error " Can't read file header...Unknown file format! "
<?php
$path = $row['img_url'].".jpg";
echo $path;
$filename = $path;
$ctype="application/.jpg";
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>
Change the content type
$ctype="application/.jpg"; //It's a invalid content type
to
$ctype="image/jpeg";
you should not be echoing anything before the header and try using header('Content-Type: image/jpeg');
Try using image/jpeg MIME type instead of application/.jpg
Your content type is wrong. Try to use $ctype="image/jpeg";
Related
I want to implement a button that downloads an image uploaded in a external server.
HTML 5 download attribute does work, but in FireFox and IE10 it opens the image in a new window and the user still have to use the right click to save image as.
Save
I can force the download using PHP if the image is located in my server.
<?php
$file = $_GET['file'];
download_file($file);
function download_file( $fullPath ){
// Must be fresh start
if( headers_sent() )
die('Headers Sent');
// Required for some browsers
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// File Exists?
if( file_exists($fullPath) ){
// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( $fullPath );
} else
die('File Not Found');
}
?>
HTML
Download1
Is there a way to avoid with the HTML5 download attribute the problem to open the image in a new window in FF and IE? -Chrome works great-.
Is there a way to do it with PHP but when the image is located at and external server?
It will be great to do it any way, HTML 5 or PHP. Thanks for any help. Greetings.
Copy it to your server and download from there.
file_put_contents($fullpath,
file_get_contents('https://externalserver/images/image.png'));
header("Pragma: public"); // wtf?
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// omg what have you been reading?
header("Cache-Control: private",false);
// obviously not rfc 2616
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($generatedPath)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( $fullPath );
For the php part. If you have the URL of the image you can use many approach.
An easy one would be a simple call to file_get_content to retrieve the image binary data in a variable .
After you can save it to the disk or do whatever you want with it.
I am facing some problems while generating PDF reports from my application in firefox(ubuntu machine).
my code is:
<?php
$path = "path_to_file" ;
$file = "test.pdf";
header('Content-type: application/pdf');
header("Content-disposition: attachment; filename=$file");
readfile($path);
return new Response("Success");
code is working:
when the PDF-report contains data
when size is more than 1 kb
In windows machine
not working:
when report contains no data or size is in bytes.
It is Generating a binary sting in a new tab in browser instead of generating blank-PDF.
Please help me fixing this issue.Thanks in advance.
Got my answer.This may help others.
<?php
$path = "path_to_file" ;
$file = "test.pdf";
header("Content-disposition: attachment; filename= $file"); //Tell the filename to the browser
header("Content-type: application/pdf");//Get and show report format
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: bytes");
readfile($path); //Read and stream the file
get_curret_user();
I would do it in this way to avoid some problems with browser applications and caching. Give it a try:
<?php
$path = "path_to_file" ;
$file = "test.pdf";
$content = file_get_contents($path);
header("Content-disposition: attachment; filename=\"".$file."\"");
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".strlen($content));
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
echo $content;
die();
EDIT:
If it's really necessary to use the pdf plugin of the browser instead of downloading and opening it immediately with the associated default pdf reader, please replace
header("Content-type: application/force-download");
with
header("Content-type: application/pdf");
A file called "transfer.php" should offer a file for download. Instead of the download, it offers a file with a ".php" of the same file size as the original file. Did I get the header wrong?
Thanks!
<?php
session_start();
$path = $_SESSION['myvar'];
$mm_type = "application/octet-stream";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: ".$mm_type);
header("Content-Length: ".(string)(filesize($path)) );
header("Content-Disposition: attachment; filename=".basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
exit();
While I originally suggested removing the trailing unpaired quote in the disposition header ..
The file name should be enclosed in quotes (thanks Wrikken!). In the posted code the first quote is missing. Using ' for the first string makes the pairing (or lack of) more clear:
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
I'm trying to download a CSV file through the browser. The script is partially working, because so far I managed to display the CSV on screen, but the download is not starting.
Here is what i tried so far:
if(isset($currency)) {
header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=Pricelogs.csv");
ob_clean();
$filename = "/tmp/".uniqid().".csv";
exportCSVFile($filename, $currency, $country, $provider);
readfile($filename);
//unlink("'".$filename."'");
} else {
echo "ERR_USERNAME_PASSWORD";
exit();
}
I had already read all questions of this type from FAQ & also tried but it gets open on browser only,instead of downloading.
I had also used header with single quotes.
I also tried header("Content-Type: text/csv");
Also:
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
PUT your CSV file URL, it will display in browser in place of force browser to download. You can open it in iframe in your site.
http://docs.google.com/viewer?embedded=true&url=www.yoursite.com/filename.csv
I usually just do:
header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=my_csv_filename.csv");
// print CSV lines here
exit();
I can tell you this combination is working for me:
$bom = chr(0xEF) . chr(0xBB) . chr(0xBF);
header("Content-type: application/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
print $bom . $data;
exit;
If I were you, I would first test this plainly (outside of all your buffering), first see that you manage to make this work, and only then test it in your specific set up.
You might try this.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename.csv\";" );
print $content;
For large files you need to get your output buffer started
add : ob_start(); at the start
ob_clean(); at the end of you file
Here is my php script which executes the download:
$link = 'www.example.com';
$url = urlencode($link);
$image = "http://chart.googleapis.com/chart?chs=75x75&cht=qr&chl=$url&choe=UTF-8&chld=L|0";
$filename = file_get_contents("$image");
header("Pragma: public");
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='qr code';");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
echo $filename;
This downloads a file but it only opens in paint not photoshop. As a hint there doesn't appear to be a file type associated with the file (e.g. png or jpg).
I need all the headers above so this works in all browsers.
Thanks.
Perhaps pass a file extension in your filename header.
header("Content-Disposition: attachment; filename='qr_code.jpg';");