I have got the script working sort of, I have check all the way through it now populates the array with jpg only , it creates a zip file and also starts the download, my problem now it that when you open the zip file there is only 1 photo in it but in the directory (and array) there are 27.
this is doing my head in now any help would be great, please see my code bellow
Code:
<?php
function zip_files($files_to_add = array(), $save_to = '', $overwrite_existing = true)
{
//if the file already exists and overwrite is
//set to false then return false and get out
if(file_exists($save_to) && !$overwrite_existing)
{
return false;
}
//now on to the action
$files = array();
//check for an array
if(is_array($files_to_add))
{
//loop through each file in the array
foreach($files_to_add as $valid_file)
{
//if the file exists then add to the file array
if(file_exists($valid_file))
{
$files[] = $valid_file;
}
//check the count of files
if(count($files))
{
//create a new zip archive object
$zip_archive = new ZipArchive();
//open zip archive for modifying and check for a value
//of true with the modes specified
if($zip_archive->open($save_to, $overwrite_existing ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) != true)
{
return false;
}
//loop through the file array
//adding each file to the new zip archive
foreach($files as $file)
{
$zip_archive->addFile($file, $valid_file);
}
//close the zip archive
$zip_archive->close();
//finally make sure the new zip file exists
return((file_exists($save_to)) ? true : false);
}
}
}
}
function stf_get_files( $directory, $filter = array( "*" ) ){
$results = array(); // Result array
$filter = (array) $filter; // Cast to array if string given
// Open directory
$handler = opendir( $directory );
// Loop through files
while ( $file = readdir($handler) ) {
// Jump over directories.
if( is_dir( $file ) )
continue;
// Prepare file extension
$extension = end( explode( ".", $file ) ); // Eg. "jpg"
// If extension fits add it to array
if ( $file != "." && $file != ".." && ( in_array( $extension, $filter ) || in_array( "*", $filter ) ) ) {
$results[] = $file;
}
}
// Close handler
closedir($handler);
// Return
return $results;
}
$dir = ".";
$my_files = stf_get_files($dir, array( "JPG", "php", "GIF"));
$zip = zip_files($my_files,'ThePhotos.zip');
if(!$zip)
{
echo('There was a problem creating the zip file');
}
else
{
//BUILD THE FILE INFORMATION
$file = "ThePhotos.zip";
//CREATE/OUTPUT THE HEADER
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment; filename=\"".basename($file)."\"");
readfile($file);
}
?>
Bookmarks