Hello.
I am trying to dynamically create an archive, save it, and output the results so that a browser and download the zip file. At the exact point where I call the close() method, an "Internal Server Error" occurs with nothing in the logs.
Here is the script:
I am trying to dynamically create an archive, save it, and output the results so that a browser and download the zip file. At the exact point where I call the close() method, an "Internal Server Error" occurs with nothing in the logs.
Here is the script:
Code:
<?php
/**
* Mukei Source Code Packager
*
* This script packages the latest version of the Mukei system into a
* Zip file for developers to start downloading. Hopefully they don't
* use the source in a production environment yet!
*
* @package Packager
* @version 0.1
*/
// Zlib Not Installed?
if ( !extension_loaded( "zlib" ) )
{
// Report problem.
trigger_error( "Zlib must be installed in order to create the " .
"packages.",
E_USER_ERROR );
}
// Zip functions missing?
if ( !function_exists( "zip_open" ) )
{
// Report problem.
trigger_error( "Zip function, zip_open, does not exist even though " .
"zlib is loaded.",
E_USER_ERROR );
}
// ZipArchive class missing?
if ( !class_exists( "ZipArchive" ) )
{
// Report problem.
trigger_error( "ZipArchive class is missing even though zlib is " .
"loaded.",
E_USER_ERROR );
}
// Make global.
global $sep;
// Get file path seperator.
$sep = ( substr( strtolower( PHP_OS ), 0, 3 ) === "win" )
? "\\"
: "/";
// Configure root directory.
$root = $sep . make_path( array(
'path', 'to', 'directory',
) );
// Get current directory.
$current = dirname( __FILE__ ) . $sep;
// Make destination path.
$to = $current . "latest.zip";
// Get directories.
$latest = scandir( $root );
// Remove extras.
array_shift( $latest );
array_shift( $latest );
// Get altest.
$latest = make_path( array(
$root, $latest[ count( $latest ) - 1 ],
) ) . $sep;
// Get contents.
$contents = scandir( $latest );
// Remove extras.
array_shift( $contents );
array_shift( $contents );
// Create an archive.
$archive = new ZipArchive ( );
// Escape root.
$preg_root = "/^" . preg_quote( $latest, "/" ) . "/";
// Create zip archive.
if ( $archive->open( $to, ZipArchive::CREATE ) === true )
{
// Initialize iterator.
$iterator = new RecursiveIteratorIterator (
new RecursiveDirectoryIterator ( $latest ) );
// Walk through contents.
foreach ( $iterator as $key => $value )
{
// Get relative path.
$relative = preg_replace( $preg_root,
"",
$key );
// Add file.
if ( $archive->addFile( realpath( $key ),
"files/$relative" ) !== true )
{
// Close archive.
$archive->close( );
// Report fatal error.
trigger_error( "Unable to add file, $key.",
E_USER_ERROR );
}
}
// Close archive.
if ( $archive->close( ) )
{
// Have host?
if ( !empty( $_SERVER['HTTP_HOST'] ) )
{
// Archive exists?
if ( file_exists( $to ) )
{
// Send download headers.
header( "Content-type: application/x-zip-compressed" );
header( "Content-disposition: attachment; filename=\"" .
"files-" . date( "Ymd-His" ) . ".zip\"" );
// Open Zip file.
if ( $file = fopen( $to, "r" ) )
{
// Print as read.
while ( $data = fread( $file, 1024 ) )
print $data;
// Close Zip file.
fclose( $file );
// Quit.
exit( );
}
}
}
}
}
// Still here?
trigger_error( "Unable to generate latest Zip file.",
E_USER_ERROR );
/**
* Make Path
*
* @param array $parts Path parts.
* @return string
*/
function make_path ( $parts )
{
// Get global.
global $sep;
return( join( $sep, $parts ) );
}
?>