Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 1:52 pm
Display Errors is set to on, bt I see nothing about Error Reporting or a 0 or -1...?? in php.ini
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
error_reporting(-1);Code: Select all
/* The directory you wish to scan for files or create an array in some other manner */
IS THIS THE PROBLEM >>>> $target=__DIR__.'\MyZipArchive.zip';
$id = $_GET['id'];
$query = "SELECT photo, photoprimary FROM products WHERE id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$files_to_zip = explode('|', $row['photo']);
$files_to_zip[] = $row['photoprimary'];
$image_path = __DIR__ . '/images/productphotos/';
// Now we go through each file in our array
foreach ($files_to_zip as $index => $file) {
// and inject that path to the beginning of each file we pulled from the database
$files_to_zip[$index] = $image_path . $file;
}
if( $target ) {
/* Zip the contents: use `true` to overwrite existing zip */
$result=create_zip( $files_to_zip, $target, true );
/* Send the file - zipped! */
if( $result ) call_user_func( 'sendfile', 'MyZipArchive.zip', $target );
}
echo var_dump($target);Code: Select all
<?php
include "dbconn.php";
/* Save as 'zip.php' etc */
/* From David Walsh's site - streamlined */
function create_zip($files = array(),$destination = '',$overwrite = false) {
if(file_exists($destination) && !$overwrite) { return false; }
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) if(file_exists($file)) $valid_files[] = $file;
}
if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) return false;
foreach($valid_files as $file) $zip->addFile($file,$file);
$zip->close();
return file_exists($destination);
}
return false;
}
/* Simple function to send a file */
function sendfile( $filename=NULL, $filepath=NULL ){
if( file_exists( $filepath ) ){
if( !is_file( $filepath ) or connection_status()!=0 ) return FALSE;
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: ".gmdate("D, d M Y H:i:s", mktime( date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Content-Type: application/octet-stream");
header("Content-Length: ".(string)( filesize( $filepath ) ) );
header("Content-Disposition: inline; filename={$filename}");
header("Content-Transfer-Encoding: binary\n");
if( $file = @fopen( $filepath, 'rb' ) ) {
while( !@feof( $file ) and ( connection_status()==0 ) ) {
print( fread( $file, 1024*8 ) );
flush();
}
@fclose( $file );
}
return( ( connection_status()==0 ) and !connection_aborted() );
}
}
/* The directory you wish to scan for files or create an array in some other manner */
$target=__DIR__.'\MyZipArchive.zip';
$id = $_GET['id'];
$query = "SELECT photo, photoprimary FROM products WHERE id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$files_to_zip = explode('|', $row['photo']);
$files_to_zip[] = $row['photoprimary'];
$image_path = __DIR__ . '/images/productphotos/';
// Now we go through each file in our array
foreach ($files_to_zip as $index => $file) {
// and inject that path to the beginning of each file we pulled from the database
$files_to_zip[$index] = $image_path . $file;
}
if( $target ) {
/* Zip the contents: use `true` to overwrite existing zip */
$result=create_zip( $files_to_zip, $target, true );
/* Send the file - zipped! */
if( $result ) call_user_func( 'sendfile', 'MyZipArchive.zip', $target );
}
?>Code: Select all
$result=create_zip( $files_to_zip, $target, true );Code: Select all
if( $target ) {
/* Zip the contents: use `true` to overwrite existing zip */
$result=create_zip( $files_to_zip, $target, true );
echo var_dump($result);
/* Send the file - zipped! */
if( $result ) call_user_func( 'sendfile', 'MyZipArchive.zip', $target );
}Code: Select all
foreach ($files_to_zip as $index => $file) {
// and inject that path to the beginning of each file we pulled from the database
$files_to_zip[$index] = $image_path . $file;
}
echo var_dump($image_path);Code: Select all
$image_path = __DIR__ . '/images/productphotos/';Code: Select all
$image_path = __DIR__ . '\images\productphotos\';Let's take a look at the function, then.simonmlewis wrote:If i place it underSo it's:Code: Select all
$result=create_zip( $files_to_zip, $target, true );I get this... which may give u some clues...Code: Select all
if( $target ) { /* Zip the contents: use `true` to overwrite existing zip */ $result=create_zip( $files_to_zip, $target, true ); echo var_dump($result); /* Send the file - zipped! */ if( $result ) call_user_func( 'sendfile', 'MyZipArchive.zip', $target ); }
[text]bool(false) [/text]
Why would it be false??
Code: Select all
function create_zip($files = array(),$destination = '',$overwrite = false) {
if(file_exists($destination) && !$overwrite) { return false; }
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) if(file_exists($file)) $valid_files[] = $file;
}
if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) return false;
foreach($valid_files as $file) $zip->addFile($file,$file);
$zip->close();
return file_exists($destination);
}
return false;
}Code: Select all
if(file_exists($destination) && !$overwrite) { return false; }Code: Select all
if(is_array($files)) {
foreach($files as $file) if(file_exists($file)) $valid_files[] = $file;
}