I've been working with this code all day but for some reason files won't get deleted after timer has run out.$zip_file_name is the file that I'd like to see gone after 30secs
$the_folder = '../test';
$zip_file_name = ("PHOENIX_fullbackup_$now.zip");
$download_file= true;
//$delete_file_after_download= true; doesnt work!!
class FlxZipArchive extends ZipArchive {
/** Add a Dir with Files and Subdirs to the archive;;;;; #param string $location Real Location;;;; #param string $name Name in Archive;;; #author Nicolas Heimann;;;; #access private **/
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
} // EO addDir;
/** Add Files & Dirs to archive;;;; #param string $location Real Location; #param string $name Name in Archive;;;;;; #author Nicolas Heimann
* #access private **/
private function addDirDo($location, $name) {
$name .= '/';
$location .= '/';
// Read all Files in Dir
$dir = opendir ($location);
while ($file = readdir($dir))
{
if ($file == '.' || $file == '..') continue;
// Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
} // EO addDirDo();
}
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if($res === TRUE)
{
$za->addDir($the_folder, basename($the_folder));
$za->close();
}
else { echo 'Could not create a zip archive';}
if ($download_file)
{
ob_get_clean();
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/zip");
header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
readfile($zip_file_name);
if ($handle = opendir ($zip_file_name)) {
while (false !== ($file = readdir($handle))) {
if (filectime($file)< (time()-30)) { // timer -30secs
unlink($file);
}
}
}
}
?>
Any help is greatly appreciated.thanks
you could delete zip file (if it already exists) prior to generating the zip file - so that old files are deleted first before new ones are generated:
E.g. (snippet of your code)
<?php
// Delete previous zip files
$old_files = glob($the_folder .'/*.zip');
foreach ($old_files as $old_file) {
unlink($old_file);
}
// Usage
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if ($res === TRUE)
{
// Add Files to Zip
$za->addDir($the_folder, basename($the_folder));
$za->close();
// Download If enabled
if ($download_file)
{
ob_get_clean();
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/zip");
header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
readfile($zip_file_name);
}
}
else
{
echo 'Could not create a zip archive';
}
?>
Related
I am using the following code in php to zip a folder and download all its files:
<?php
$the_folder = '/path/to/folder/to/be/zipped';
$zip_file_name = '/path/to/zip/archive.zip';
$download_file= true;
$delete_file_after_download= true;
class FlxZipArchive extends ZipArchive {
/** Add a Dir with Files and Subdirs to the archive;;;;; #param string $location Real Location;;;; #param string $name Name in Archive;;; #author Nicolas Heimann;;;; #access private **/
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
} // EO addDir;
/** Add Files & Dirs to archive;;;; #param string $location Real Location; #param string $name Name in Archive;;;;;; #author Nicolas Heimann
* #access private **/
private function addDirDo($location, $name) {
$name .= '/';
$location .= '/';
// Read all Files in Dir
$dir = opendir ($location);
while ($file = readdir($dir))
{
if ($file == '.' || $file == '..') continue;
// Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
} // EO addDirDo();
}
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
$basename = pathinfo($zip_file_name, PATHINFO_BASENAME);
if($res === TRUE)
{
$za->addDir($the_folder,basename($the_folder));
$za->close();
}
else { echo 'Could not create a zip archive';}
if ($download_file)
{
ob_get_clean();
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/zip");
header("Content-Disposition: attachment; filename=\"$basename\"" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
readfile($zip_file_name);
}
?>
Where should the condition be added to skip hidden files and folders? I am using the following function to check if the file/folder is hidden or not:
function is_hidden_file($path) {
$dir = "\"".$path."\"";
$attr = trim(shell_exec("FOR %A IN (".$dir.") DO #ECHO %~aA"));
if($attr[3] === 'h')
return true;
return false;
}
Hi only one detail here.
If you want to delete the file after downloaded need to updated the last part of code with this:
if ($download_file){
ob_get_clean();
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/zip");
header("Content-Disposition: attachment; filename=".basename($zip_file_name) . ";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
if($delete_file_after_download){
ob_clean();
flush();
if (readfile($zip_file_name))
unlink($zip_file_name);
}else{
readfile($zip_file_name);
}
}
This is the solution I found out so as to skip Actual hidden files and other files starting with '.' and '~' from being zipped and downloaded. It is working fine with windows 7, window 8 and windows 8.1.
<?php
$the_folder = '/path/to/folder/to/be/zipped';
$zip_file_name = '/path/to/archive.zip';
$download_file= true;
$delete_file_after_download= true;
class FlxZipArchive extends ZipArchive {
/** Add a Dir with Files and Subdirs to the archive;;;;; #param string $location Real Location;;;; #param string $name Name in Archive;;; #author Nicolas Heimann;;;; #access private **/
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
} // EO addDir;
/** Add Files & Dirs to archive;;;; #param string $location Real Location; #param string $name Name in Archive;;;;;; #author Nicolas Heimann
* #access private **/
private function addDirDo($location, $name) {
$name .= '/';
$location .= '/';
// Read all Files in Dir
$dir = opendir ($location);
while ($file = readdir($dir))
{
/************Putting the condition here restricts hidden files and files starting with '.' and '~'...***********************/
if ($file == '.' || $file == '..' || $file[0]=='.' || $file[0]=='~' || is_hidden_file($location.$file)) continue;
// Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
} // EO addDirDo();
}
function is_hidden_file($fn) {
$dir = "\"".$fn."\"";
$attr = trim(shell_exec("FOR %A IN (".$dir.") DO #ECHO %~aA"));
if($attr[3] === 'h')
return true;
return false;
}
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
$basename = pathinfo($zip_file_name, PATHINFO_BASENAME);
if($res === TRUE)
{
$za->addDir($the_folder,basename($the_folder));
$za->close();
}
else { echo 'Could not create a zip archive';}
if ($download_file){
ob_get_clean();
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/zip");
header("Content-Disposition: attachment; filename=".basename($zip_file_name) . ";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
if($delete_file_after_download){
ob_clean();
flush();
if (readfile($zip_file_name))
unlink($zip_file_name);
}else{
readfile($zip_file_name);
}
}
?>
I've got a project about showing all files of root directory in a list and the user can browse through files-directories, search a file by name,with the button downloadAll he can download all files at the current list.To make the downloadAll button work I use downloadAll.php code which by getting the current directory can create a zip file and auto download it. It works fine in windows. I'm trying to run the same project at linux but in that case it downloads an empty zipped file. Also when I try to open it this message appears:
Archive: /tmp/archived_name.zip
[/tmp/archived_name.zip]
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
zipinfo: cannot find zipfile directory in one of /tmp/archived_name.zip or
/tmp/archived_name.zip.zip, and cannot find /tmp/archived_name.zip.ZIP, period.
downloadZip.php:
<?php
if ((isset($_GET['currentdirectory'])) && ($_GET['currentdirectory']!="")){
$the_folder = $_GET['currentdirectory'];
}
else {
$the_folder = "/var/www/my_project";
}
$the_folder1 = $the_folder;
$zip_file_name = 'archived_name.zip';
$download_file= true;
class FlxZipArchive extends ZipArchive {
public function addDir($location, $name) {
$this->addEmptyDir(str_replace("./","",$name));
$this->addDirDo($location, str_replace("./","",$name));
}
private function addDirDo($location, $name) {
$name .= '/';
$location .= '/';
$dir = opendir ($location);
while ($file = readdir($dir))
{
if ($file == '.' || $file == '..') continue;
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, str_replace("./","",$name) . $file);
}
}
}
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if($res === TRUE)
{
$za->addDir($the_folder1, basename($the_folder1));
$za->close();
}
else { echo 'Could not create a zip archive';}
if ($download_file)
{
ob_get_clean();
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/zip");
header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
readfile($zip_file_name);
}
unlink("archived_name.zip");
?>
You likely have an error that is happening elsewhere. Try setting $download_file to false and set error_reporting(E_ALL) to see if there are any errors. If that doesn't work, try viewing the web server error logs. For apache they default to /var/log/apache2/error.log.
<?php
$zip = new ZipArchive;
$download = 'download.zip';
$zip->open($download, ZipArchive::CREATE);
foreach ("d://Photo_Album/shweta/*.jpg" as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename = $download");
header('Content-Length: ' . filesize($download));
header("Location: $download");
?>
This is my code but its not running when the path is taken. But its not taking in php. If anyone knows plz help on this
try this
<?php
ob_start();
ini_set('memory_limit','2048M');
set_time_limit(0);
$sourcefolder = "d:/Photo_Album/shweta/" ; // Default: "./"
$zipfilename = "download.zip"; // Default: "myarchive.zip"
$timeout = 7000 ; // Default: 5000
$the_folder = $sourcefolder;
$zip_file_name =$zipfilename;
$download_file= true;
//$delete_file_after_download= true; doesnt work!!
class FlxZipArchive extends ZipArchive {
/** Add a Dir with Files and Subdirs to the archive;;;;; #param string $location Real Location;;;; #param string $name Name in Archive;;; #author Nicolas Heimann;;;; #access private **/
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
} // EO addDir;
/** Add Files & Dirs to archive;;;; #param string $location Real Location; #param string $name Name in Archive;;;;;; #author Nicolas Heimann
* #access private **/
private function addDirDo($location, $name) {
$name .= '/';
$location .= '/';
// Read all Files in Dir
$dir = opendir ($location);
while ($file = readdir($dir))
{
if ($file == '.' || $file == '..') continue;
// Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
} // EO addDirDo();
}
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if($res === TRUE)
{
$za->addDir($the_folder, basename($the_folder));
$za->close();
}
else { echo 'Could not create a zip archive';}
if ($download_file)
{
ob_get_clean();
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/zip");
header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($zip_file_name));
readfile($zip_file_name);
}
?>
You can use the DirectoryIterator
$dirName = 'D:/Photo_Album/shweta';
$dir = new DirectoryIterator($dirName);
foreach ($dir as $fileInfo) {
if ($fileInfo->isFile() && $fileInfo->getExtension() == 'jpg') {
$path = $dirName . '/' . $fileInfo->getFilename();
$zip->addFile($path);
}
}
ok then try this
<?php
$zip = new ZipArchive;
$download = 'download.zip';
$zip->open($download, ZipArchive::OVERWRITE);
$zip->addGlob('d:/Photo_Album/shweta/*.jpg', 0, array('add_path' => 'images/', 'remove_all_path' => true));
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename = $download");
header('Content-Length: ' . filesize($download));
header("Location: $download");
Following is my code to download zip file using PHP.
function create_zip($files, $file_name, $overwrite = false) {
foreach ($files as $imglink) {
$img = file_get_contents($imglink);
$destination_path = $_SERVER['DOCUMENT_ROOT'] . 'demoproject/downloads/' . time() . '.jpg';
file_put_contents($destination_path, $img);
$imgFiles[] = $destination_path;
}
if (file_exists($file_name) && !$overwrite) {
return false;
}
$valid_files = array();
if (is_array($imgFiles)) {
foreach ($imgFiles as $file) {
$valid_files[] = $file;
}
}
if (count($valid_files)) {
$zip = new ZipArchive();
if ($zip->open($file_name, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
echo "Sorry ZIP creation failed at this time";
}
foreach ($valid_files as $file) {
$zip->addFile($file, pathinfo($file, PATHINFO_BASENAME));
}
$count = $zip->numFiles;
$resultArr = array();
$resultArr['count'] = $count;
$resultArr['destination'] = $file_name;
$filename = $file_name;
$filepath = $_SERVER['DOCUMENT_ROOT'] . 'demoproject/';
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: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($filepath . $filename));
ob_end_flush();
#readfile($filepath . $filename);
} else {
return false;
}
}
Here the $filename and $filepath containing the name and path of zip file respectively.
Ex:
echo $filepath.$filename;
Output : D:/wamp/www/demoproject/1357198557.zip
The issue is it displaying download window but showing the folder size 0 byte. I am using windows7. See image below :
Can you try adding this, Make sure you got proper filesize
$mm_type="application/octet-stream";
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($fullpath)) );
I am trying to read and get the size of the zip file in PHP.
Following is my code:
function create_zip($files, $file_name, $overwrite = false) {
foreach ($files as $imglink) {
$img = file_get_contents($imglink);
$destination_path = $_SERVER['DOCUMENT_ROOT'] . 'demoproject/downloads/' . time() . '.jpg';
file_put_contents($destination_path, $img);
$imgFiles[] = $destination_path;
}
if (file_exists($file_name) && !$overwrite) {
return false;
}
$valid_files = array();
if (is_array($imgFiles)) {
foreach ($imgFiles as $file) {
$valid_files[] = $file;
}
}
if (count($valid_files)) {
$zip = new ZipArchive();
if ($zip->open($file_name, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
echo "Sorry ZIP creation failed at this time";
}
foreach ($valid_files as $file) {
$zip->addFile($file, pathinfo($file, PATHINFO_BASENAME));
}
$count = $zip->numFiles;
$resultArr = array();
$resultArr['count'] = $count;
$resultArr['destination'] = $file_name;
$filename = $file_name;
$filepath = $_SERVER['DOCUMENT_ROOT'] . 'demoproject/';
$fileSize = filesize($filepath . $filename) / 1024;
echo 'size of the file is : ' . $fileSize . ' kb';
exit;
// $size = 0;
// $resource = zip_open($filepath . $filename);
// while ($dir_resource = zip_read($resource)) {
// $size += zip_entry_filesize($dir_resource);
// }
header("Pragma: no-cache");
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: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . zip_entry_filesize($filepath . $filename));
if (#readfile($filepath . $filename) === false) {
header('http://localhost/demoproject/fbindex.php');
return 500;
} else {
header('Location:http://localhost/demoproject/fbindex.php');
return 200;
}
} else {
return false;
}
}
$files : contain the array of images and
$file_name : contain name of file : that is : $file_name = time() . ".zip";
In the above code $filename contains the actual zip file and $filepath contains the location of the zip file.
The issue is it's reading the file but always showing its size 0 instead of actual file size.
This is I am doing :
echo $size;
O/P : 0
Where am I going wrong? Need Help. Thanks
try this -
$fileSize = filesize($filepath . $filename)/1024;
echo 'size of the file is : '.$fileSize.' kb';
Shouldn't you close ZIP file just after adding files to it?
$zip->close();
Try clearstatcache() just before calling filesize().
Closing your zip file before all this is also a great idea.
I know I'm a bit late on this, but who knows?