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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.