herghost Posted January 8, 2012 Share Posted January 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254579-php-zip-help/ Share on other sites More sharing options...
herghost Posted January 8, 2012 Author Share Posted January 8, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/254579-php-zip-help/#findComment-1305460 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.