Forcing php to download a file inside zip - php

I have a file image.php that dynamically views an image inside a Zip folder.
So image.php?location=./folder/Hi.zip&file=image.jpg views the image.
I have another file downloadfile.php that forces download of file specified in the parametes.
downloadfile.php?location=.folder/&file=image.jpg* downloads the image*
What I need is to download the dynamically generated image (by image.php) using downloadfile.php.
downloadfile.php
<?php
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
$file=$_GET['file'];$ext=$_GET['ext'];$location=$_GET['location'];
header('Content-disposition: attachment; filename="'.$file.'"');
header('Content-type:"'.$ext.'"');
readfile("$location"."$file");
?>
image.php
<?php
function showimage($zip_file,$file_name)
{
$z=new ZipArchive();
if($z->open($zip_file)!=true){
echo "File not found";
return false;
}
$stat=$z->statName($file_name);
$fp=$z->getStream($file_name);
if(!$fp){
echo "Could not load image";
return false;
}
header('Content-Type:image/'.$_GET['type']);
header('Content-Length:'. $stat['size']);
fpassthru($fp);
return true;
}
showimage($_GET['zip'],$_GET['file']);
?>
Any suggestions??

In order to force browser to download an image file you can use the code from image.php
and just change the header
from
header('Content-Type:image/'.$_GET['type']);
header('Content-Length:'. $stat['size']);
to
header("Content-Transfer-Encoding: binary");
header('Content-Description: File Transfer');
header('Content-Type:image/'.$_GET['type']);
header('Content-Length: ' . $stat['size']);
header('Content-Disposition: attachment; filename=' . $file_name);
this will force user to download instead of display an image.

Related

why does an empty pdf file created in the same folder when i click download every time?

i was trying to force download pdf files on my website. The file was downloaded successfully but an empty file is created in the same folder with a random name when download is clicked. i'm having multiple null files in the folder.
download.php :
<?php
$file = $_GET['file'];
header('Content-type: application/pdf');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="'.$file.'"');
?>
Assuming you store your pdf in database.
//you need to decode
$decoded = base64_decode($downloaded->pdf);
//name of pdf
$filename =date('Ymdhis').'.pdf';
$size=strlen($decoded);
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.($filename).'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header('Content-Length:'.$size);
echo($decoded);
exit;
Please note, if you send some headers before it will not working.
Make sure you create properly your pdf file.
$decoded = base64_decode($downloaded->pdf);
$filename =date('Ymdhis').'.pdf';
//if file doesn't exists
if(!file_exists($filename)){
//we create file in the location
$fp=fopen($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$dir_name.''.$filename,'a+');
//We write in
fwrite($fp, $decoded);
}

PHP Coding for downloading the image

In the website page contains many images with downloading options. If I click the download button it automatically downloaded on user system and it shows on browser downloadable page. I have PHP code like
$image = file_get_contents('http://website.com/images/logo.png');
file_put_contents('C:/Users/ASUS/Downloads/image.jpg', $image);
Above coding is working fine. But I need to provide the path name for image to save. In user side we don`t know the path.
I need the PHP code to use the browser download location and download images need to show the browser downloads.
not possible to store the image in particular user location due to security issues .you don't force user .you have to suggest him to store particular location .and also you don't know the what file system there in user system.and also downloading path can be setting up by user anywhere so your not able to get that.
$filename = '/images/'.basename($_POST['text']);
file_put_contents($filename, $content);
you have to save/download the image somewhere on your web serwer and next send the file to user using header function, for example:
$file = 'path_to_image';
if (file_exists($file)) {
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);
exit;
}
else {
echo "file not exists";
}
manual
`<?php
$filename ='http://website.com/images/logo.png';
$size = #getimagesize($filename);
$fp = #fopen($filename, "rb");
if ($size && $fp)
{
header("Content-type: {$size['mime']}");
header("Content-Length: " . filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
fpassthru($fp);
exit;
}
header("HTTP/1.0 404 Not Found");
?>`

Php or Apache? Content of zip file is diplayed instead of downloading zip file

I tried to write a php-script to create and download a zip file. When I tested the script on my localhost, the download works, but when it's uploaded to the server, things go wrong: instead of downloading the zip file, the content of the file is displayed in the browser.
Can somebody point me in the right direction?
The code
$zip = new ZipArchive();
$zip->open("$maand/zipfile.zip", ZipArchive::OVERWRITE);
$zip->addFile("$maand/ant/a_nb.txt", 'ant.txt');
$zip->addFile("$maand/lim/l_nb.txt", 'lim.txt');
$zip->addFile("$maand/oos/o_nb.txt", 'oos.txt');
$zip->addFile("$maand/vla/v_nb.txt", 'vla.txt');
$zip->addFile("$maand/wes/w_nb.txt", 'wes.txt');
$zip->close();
$filename = "zipfile.zip";
$filepath = "$maand/";
// headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
#readfile($filepath.$filename);
you missing ob_start()
example:
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $download . '"');
ob_start();
$str = '';
if(file_exists($file) === true){
$str = file_get_contents($file);
}
ob_end_clean();
echo $str;

Php / Codeigniter File Download

I'm trying to download file in php. I used both codeigniter download helper and pure php code readfile. I can get the contents of file in browser's Network but I can not download it.
When I tried the readfile function in an independent php file it worked.
Here is my code block for dowload file.
// codeigniter
$this->load->helper('download');
$data = file_get_contents(base_url("/images/logo.png"));
force_download($decodedFileInfo->fileName, $data);
// php readfile
$file = "./images/logo.png"; //. $decodedFileInfo->fileName;
if (file_exists($file)) {
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);
exit;
}else{
var_dump("hst");die;
}
Thank you..
Try Saving the file name and image name in variables and echo them on the screen.
I tested this functions works perfectly:
$this->load->dbutil();
//Calling a store procedure
$sp ="exec secondProc '5 Aug 2013'";
$query = $sqlsrvr->query($sp);//->result();
$jawad = $query->result_array();
//pass it to db utility function
$new_report = $this->dbutil->csv_from_result($query);
//Downloading the file
$this->load->helper('download');
force_download('csv_file.csv', $new_report);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$name}");
header("Expires: 0");
header("Pragma: public");

Problem naming the download file

I have some php code which is suppose to download a zip file...this code is called from a file called download_file.php (very original, i know)
The relevant function is as follows :
function download_clientfile() {
if ($this->download_file === FALSE) {
$this->log->log(sprintf("The download file was not generated."), PEAR_LOG_CRIT);
return;
}
$fsize = filesize($this->download_file);
$path_parts = pathinfo($this->download_file);
$ext = strtolower($path_parts["extension"]);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; file='.$path_parts['basename']);
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($this->download_file));
ob_clean();
flush();
readfile($this->download_file);
exit;
}
Problem : The file downloaded is the zip file that is expected, however its downloaded with the name 'download_file.php'
Any suggestions on how I can get it to download by the name of the zip file.
Thanks
Use filename instead of file:
header('Content-Disposition: attachment; filename='.$path_parts['basename']);
Also, you may encounter browsers which use the extension anyway. In that case, you can call the script like this:
download_file.php?file=myfile.zip
or:
download_file.php/myfile.zip
This way, the URL ends in .zip, which tricks the browser into thinking it is downloading a ZIP file.
Try changing file= to filename=
header('Content-Disposition: attachment; filename='.$path_parts['basename']);
use
header("Content-Disposition: attachment; filename=\"$filename\"");

Categories