Image generated by PHP not cached - php

I generate some picture with PHP, and those picture are not cached by the browser.
I ouput some picture through a php file the url looks like this :
http://127.0.0.1:8080/workspace/cps/index.php?act=file&act2=preview_or_icon&template=disable&file_id=341
in my php file i've :
header('Content-Type: image/jpeg');
header('Content-Transfer-Encoding: binary');
//Read File

added this:
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,time()+60*60*24*30));
And fixed!

Related

Facing issue of downloading script in PHP

I am facing a problem of downloading script, whenever download on my project site starts, all links present in the site does not work at all.
Once file gets downloaded completely, link starts working.
I am setting header and using readfile method of php for download the file. I want that file downloading should work simultaneously with users actions on the site.
I am using following code
header("Pragma: public");
header("Expires: 0");
header("Content-Type: application/$ctype");
header("Content-Description: File Transfer");
header("Content-Disposition: inline; filename=$docName");
header('Content-Length: '. filesize($filename));
header("Accept Ranges: bytes");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
readfile("$filename");
Here filename is name of file and ctype is file type.
How to resolve this?
Please let me know if anybody have solution for this.
I'm just guessing that you're missing a proper header, change content type and transfer encoding as you need
$download is the actual file i.e. /var/www/download_images/1.jpg
$filesize is the size of the file
$filename can be any filename that you want presented to the user i.e. homer_simpson.jpg
<?php
if (file_exists($download))
{
header("Cache-Control: must-revalidate");
header("Expires: 0");
header("Pragma: public");
header("Content-Description: File Transfer");
header("Content-Length: ". $filesize .";");
header("Content-Disposition: attachment; filename=" . $filename);
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
readfile($download);
}
?>

PHP file download including the .php file when only pdf is wanted

<?php
$file = $_GET['name'];
$path = './curr/'.$file.'.pdf'; // the file made available for download via this PHP file
$mm_type="application/pdf"; // modify accordingly to the file type of $path, but in most cases no need to do so
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
?>
This is a snippet of code in file.php. I am referring to the file using:
File 1
The intent is that on click of the link, ./curr/First File.pdf should download. I do get a download, but on inspecting, it's the webpage with the pdf embedded in the file. Could anyone assist?
If you want to have just the PDF loaded, the above code is all code to be executed.
Drop all surrounding menus, header or footers. Make sure, that no HTML or any other output besides the PDF from readfile() remains, when calling this link.
Try to change the content type to :
header("Content-Type: application/force-download");

PHP: Forcing a download not working

I have the following code to push a zip file for download.
$filename = "ResourcePack_".time().".zip";
$destination = $basepath."downloads/$filename";
if($this->createdownload($files,$destination,false)){
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$destination").";");
header("Content-Disposition: attachment; filename='$filename'");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
ob_end_flush();
#readfile($destination);
if(file_exists($destination)){
unlink($destination);
}
}
I know the createdownload function is working to generate the zip file just fine because I see the file being created on the server. The problem is file is being written to the browser as a bunch of garbage instead of opening a download stream. Am I missing something in my headers?
EDIT
I was right. My problem is not with the php, but that calling the php file that generates this code via a JQuery $.ajax call is the problem. Using $.ajax automatically sets the Accept-Encoding request header to values incompatible with zip files. So, intead of using $.ajax I just used a simple window.open javascript command to call the same php page and it works just fine with the headers.
try to put a die after the #readfile
and remove the #, to see if you have any other error related with the file reading.
i have some code doing the same thing and this works for me:
header("Pragma: public");
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-Disposition: inline; filename="' . $filename . '"');
header('Content-type: application/zip');
//header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($destination));
readfile($destination);
die();
try passing proper type for that file. I think its fileinfo mime type see http://www.php.net/manual/en/ref.fileinfo.php
header("Content-Type: $file_type");
Also you have semicolon after octet-stream remove it
header("Content-type: application/octet-stream");

PHP Force Download Help

I've been wrestling around with this for awhile now. I am trying to make it so when a user click a link it will force a download. Here's my code so far:
<?php
function Download()
{
$fullpath = $_SERVER['DOCUMENT_ROOT']."front.dwg"; //Full path of document
$filename = "front.dwg"; //Document file nmae
$mm_type="application/octet-stream";
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($fullpath)) );
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary\n");
}
?>
<html>
<body>
Test
</body>
</html>
Your going to need to pull out your function and save it as "download.php"
Then just have a link that goes to it:
<a href="download.php">
Download.php should look like this:
$fullpath = $_SERVER['DOCUMENT_ROOT']."front.dwg"; //Full path of document
$filename = "front.dwg"; //Document file nmae
$mm_type="application/octet-stream";
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($fullpath)) );
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary\n");
PHP is not a client-side function. You can't say onclick="somePHP".
You need a separate PHP script where you will force your download in, and simply link to that script.
In this script, you also need to actually output the file contents.
You need to set the appropriate calls to header first. These have worked for me:
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-disposition: attachment; filename=\"{$filename}\"");
header("Content-Type: {$mime}"); // also works with file extension
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);
die();
You can never FORCE a download. You can change the window location to the download file and it will prompt them, but if you could force downloads, there would be some major security issues.
First, your $fullpath may not contain the data you expect, because you did not insert a forward slash ('/') before the filename. This is correct:
$fullpath = $_SERVER['DOCUMENT_ROOT']."/front.dwg";
You can check the contents of $fullpath using error_log( "Fullpath: $fullpath" ); or echo "Fullpath: $fullpath\n".
Second, you need to make the PHP portion of your code a standalone script. You will not be able to embed PHP function calls in Javascript. All of your PHP executes before the Javascript runs, so you need a way to call the PHP separately.
Third, you should verify that the file exists and is readable using file_exists() and is_readable(). If PHP reports that the file does not exists, you should specify the full path. If it is not readable, you can change the file permissions to allow your PHP script to read it.

Download file from site PHP

I would to know the command in a PHP script to get in output and save a file from my site.
Thanks
See here for a good description of how to force the output of a php script to be a download.
The basics of it are:
// Set headers
header("Cache-Control: public");
header("Content-Description: File Download");
header("Content-Disposition: attachment; filename=" + $filename);
header("Content-Type: application/zip"); // or whatever the mime-type is
// for the file you want to download
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($full_path_to_file);
As an addition (provided by Gordon's comment), see the 1st example on the php documentation here
At the End of the files or used in clicking files, you can add this
$filesh = "check.xls";
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-Disposition: attachment; filename=".basename($filesh));
header("Content-Description: File Transfer");
readfile($filesh);
if you got any header using problem means, top of the file you can add ob_start(); function
If you mean getting output, contents from other site or location, this what you need file_get_contents

Categories