PHP: force an image download from above directory root? - php

I'm attempting to force a download of an image that is in a directory above my website root. The download happens ok, and the correct filename is saved. However, the end-file is not a valid image and will not open or display properly. Here's my code:
$photograph = new ViewPhotograph($photograph_id);
$photograph->setPhotographVars();
$file = $photograph->getPath('small');
$filename = '1.jpg';
header('Content-Description: File Transfer');
header('Content-Type: image/jpg');
header('Content-Disposition: attachment; filename=' . $filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;

do a ob_start before calling ob_clean()
once something is outputed by using echo or similar, you can't get rid of this, except, when you start a buffer before

Perhaps there's a PHP warning corrupting the stream.
Comment out the headers and see if you see a warning. If you do, fix it. (note that you shouldn't have display_errors turned on production servers).

Related

PHP cant download image with readfile

This is my code:
$file = 'test.jpg';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
test.jpg is a file on my system. I can open it and nothing is wrong with it. The download gets started but when i try to open the downloaded image, it says that it cant be opened. Im testing it on Chrome. Does someone have a solution? Thanks a lot.
Edit: the downloaded .jpg is only 23kb where as the file i want is 135kb.
Try to set header
header('Content-Type: image/jpeg');
ob_clean();
exit;
try replacing readfile with echo file_get_contents($file)

PHP File Download Issue - Can't Read

I have the following code for downloading files automatically
at the click of a submit button, everything seems to work fine;
the file downloads in the rigth format, right size, right name, but when I
want to open it, I get an error, the file cannot be read, what could be the
problem?
$file=mysql_fetch_assoc($sel);
$file=$file['downloadlink'];
header('Content-Type: "application/octet-stream"');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment; filename=\"".basename($file)."\"");
readfile($file);
you could try tweaking this function from the readfile() comments:
function DownloadFile($file) { // $file = include path
if(file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
I have an addition for this. If the file size is very big, it'll download empty file which we cannot open at all. That is not a problem with 'readfile' function itself. The problem is reading large files into memory. So, for preventing that kind of issues, we have to use 'ob_end_flush()' immediately before to the 'readfile' function for turning off output buffer.
Hope this tip will save someones time. :)

PHP Downloading File(s) outside webroot

I save many documents outside the webroot.
I want to click a link, that opens a new window (target="_blank"), and force download the file that's found.
Here's what I've got so far, but my results show gobble-de-gook in the browser popup, rather than forcing the download to the desktop:
function download($filelocation){
$filename = basename($filelocation);
if (file_exists($filelocation)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filelocation));
ob_clean();
flush();
readfile($filelocation);
exit;
}
}
In the new browser window I simply call that download() function with a specific path the the file.
It's definitely finding the file, but now I'm just wondering what I'm missing with header() to force the file through the browser.
Missing this:
header("Content-Type: application/force-download");

PHP - What are the correct headers for downloading file?

I have a piece of code that allow users download file from server (document such as docs,docx,pdf etc).
Users can download files but it has some errors like the files were broken. For example, a MS Word file after download need to recovery to read content.
I wonder that if there is any mistake in this code (or problem when uploading?).
$size_of_file = filesize($download_path);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $file_name);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size_of_file);
//read file from physical path
readfile($download_path);
Did you try like this ?
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
I found the root of the problem, I hav some extra spaces after php close tag. Thank you guys.

File Download Not Working

When I download the original zip it works fine, but when I download it using the below headers and stuff it doesn't work. I know it's better to take this route and tell the browser how to handle the file rather than leave it up to the browser, but I can't get this to work, so I'm tempted to use a header() forward.
$path = $this->tru->config->get('root.path').'/Digital Version of Book for Web.zip';
set_time_limit(0);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-Type: application/zip');
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="NewFileName.zip"');
header('Content-Length: ' . filesize($path));
$f = fopen($path, 'rb');
fpassthru($f);
fclose($f);
Edit:
Sorry, what I mean by it doesn't work is that the file downloads in a zip format (all 9.3 MB) but I'm unable to unpackage the zip because it's invalid.
Take a look into the ZIP file using Notepad or another text editor. Check whether there is a PHP error message screwing up the file on the first few lines. It could be a "headers already sent" message or the set_time_limit() call throwing an error due to the script being in safe mode.
Try using readfile(). An example is provided in the PHP Manual.
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}

Categories