Jump to content

PHP Zip help


herghost

Recommended Posts

Hi all, I am trying to create a zip from a folder using php.

 

My script works if I replace the variable $username with a constant, but with the variable I get nothing at all, no errors, no files. I have error reporting on as well.

 

// Config Vars 
$username = "user";
$sourcefolder = "uploads/" . $username . ""           ; // Default: "./" 
$zipfilename  = "uploads/" . $username . ""; // Default: "myarchive.zip"
$timeout      = 5000           ; // Default: 5000

// instantate an iterator (before creating the zip archive, just
// in case the zip file is created inside the source folder)
// and traverse the directory to get the file list.
$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);

// set script timeout value 
ini_set('max_execution_time', $timeout);

// instantate object
$zip = new ZipArchive();

// create and open the archive 
if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) {
    die ("Could not open archive");
}

// add each file in the file list to the archive
foreach ($filelist as $key=>$value) {
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}

// close the archive
$zip->close();
echo "Archive ". $zipfilename . " created successfully.";

// And provide download link ?>
<a href="http:<?php echo $zipfilename;?>" target="_blank">
Download <?php echo $zipfilename?></a> 

 

Whats going on?!

 

Many thanks

Dave

Link to comment
https://forums.phpfreaks.com/topic/254579-php-zip-help/
Share on other sites

Fixed

 

//make zip

// Config Vars 

$sourcefolder = "uploads/".$username.""; // Default: "./" 
$zipfilename  = "uploads/".$username.".zip"; // Default: "myarchive.zip"
$timeout      = 5000; // Default: 5000

// instantate an iterator (before creating the zip archive, just
// in case the zip file is created inside the source folder)
// and traverse the directory to get the file list.
$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);

// set script timeout value 
ini_set('max_execution_time', $timeout);

// instantate object
$zip = new ZipArchive();

// create and open the archive 
if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) {
    die ("Could not open archive");
}

// add each file in the file list to the archive
foreach ($filelist as $key=>$value) {
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}

// close the archive
$zip->close();
echo "<br>Archive ". $zipfilename . " created successfully.";

// And provide download link ?>
<br /><a href="http:<?php echo $zipfilename;?>" target="_blank">
Download <?php echo $zipfilename?></a>

Link to comment
https://forums.phpfreaks.com/topic/254579-php-zip-help/#findComment-1305460
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.