how to delete zip file after download complete php - php

am trying to create a zip file by php (ZipArrchive),then after user download complete i want to remove the temp created zip file , i tried to use unlink() but logically it will not work because i did not know when the users`s downloading complete , so , any help ?
i`ve tried this code :
if(file_exists('test.zip')){
//Set Headers:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime('test.zip')) . ' GMT');
header('Content-Type: application/force-download');
header('Content-Disposition: inline; filename="test.zip"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize('test.zip'));
header('Connection: close');
readfile('test.zip');
exit();
}
if(file_exists('test.zip')){
unlink('test.zip');
}

Related

php - download a cookie together with pdf

I need to download a cookie together with PDF. I added setcookie to following function but it doesn't work. What's wrong? Thanks
protected function send_download($file) {
if (file_exists($file)) {
set_time_limit(0);
header('Connection: Keep-Alive');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
setcookie("fileDownloadToken", "fileDownloadToken", time()+360); // 10 minutes
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);
}
}
Resolved :)
It was visible only as response cookie. It was necessary to set path and domain.
setcookie("fileDownloadToken", $fileDownloadToken, time()+360, '/', $this->template->domain); // 10 minutes

Save Mysql data in xls using php

I want to export data in xls format using PHP. But instead of download information, they are displayed in my web page.
//connection to database
//building of all information -> ($righe)
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: esporta_utenti.xls");
print $righe;
exit();
$righe is my tuple
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime); // file mime type
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
header('Connection: close');
readfile($file_name); // push it out
exit();
This code was originally Published here http://davidwalsh.name/php-force-download . And can be referred for a detailed explanation of the Code.

Can't download a word file

Can you tell me what is wrong with my headers? My download is not effective. I use PHPWord library but I don't think it's a problem
<?php
require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$wordText = utf8_encode($_REQUEST['TEXT']);
$section->addText($wordText);
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
//$objWriter->save('helloWorld.docx');
$path = 'tmp/kikou2.docx';
$objWriter->save($path);
//download code
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' .$path);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Length: ' . filesize($objWriter));
readfile($objWriter);
unlink($objWriter); // deletes the temporary file
exit;
?>
Thanks
Recommends headers for an OfficeOpenXML .docx file
// Redirect output to a client’s web browser (.docx)
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="kikou2.docx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
paying particular attention to the Content-Type heading
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="'.basename($objWriter).'"'); //<<< Note the " " surrounding the file name
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($objWriter));
Add two headers:
header("Content-Type: application/force-download");
header("Content-Type: application/download");
$document->save($url.'temp/Form Letters1.docx');
$path = $url.'temp/Form Letters1.docx';
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="Form Letters1.docx"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);

not supported or damaged pdf/doc file when downloading in php

I am downloading PDF/DOC files using php
This is my html code :
<a title="Download" target="_new" href="includes/pdf_server.php?file=test.pdf">Test PDF</a>
This is my php code in pdf_server.php file
<?php
$file = $_GET["file"];
if (file_exists("../PDF/".$file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . urlencode(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("../PDF/".$file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
PDF is the folder where I have my test.pdf file . When I click on link to download file. Browser shows file to download with its size (1.4 mb) but when download finished and I open the file it shows error either file damaged or not supported file. Then I check it's properties it shows 0 bytes.
Please help
Try this-
<?php
$file_name = $_GET["file"];
if (file_exists("../PDF/".$file_name)) {
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ("../PDF/".$file_name)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.'application/pdf');
header('Content-Disposition: attachment; filename="'.basename("../PDF/".$file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
header('Connection: close');
readfile("../PDF/".$file_name); // push it out
exit();
}
?>

PHP force header download not working on zip files

I have a document download site where I track files downloaded.
My problem is getting zip files to download through the script I am using. Every other file gets forced to download apart from zip files. When a zip file is pushed it just goes to the download.php page where the script is with no file being pushed out.
ob_start();
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;
Here is a code snippet that works for me:
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="'.basename($file_url).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize($file_url));
} else {
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="'.basename($file_url).'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize($file_url));
}
readfile($file_url);
Had similar problem, this quotes helped that time:
header('Content-Disposition: attachment; filename="'.basename($file).'"');
I was not using the base_name for the content disposition and length, I had the full path instead. Using base_name worked for me.

Categories