ballouta Posted June 14, 2009 Share Posted June 14, 2009 Hello I am using this code to read a protected directory (username&password) contents called (protect). <?php require_once("admin/global.inc.php"); // increase script timeout value ini_set('max_execution_time', 300); //Generate a new flag $random = (rand(000000,999999)); $date = date("y-m-d"); // create object $zip = new ZipArchive(); // open archive if ($zip->open("$date-$random.zip", ZIPARCHIVE::CREATE) !== TRUE) { die ("Could not open archive"); } // initialize an iterator // pass it the directory to be processed $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("protect/")); //check question #2 // iterate over the directory // add each file found to the archive foreach ($iterator as $key=>$value) { if ($key != 'protect/.htaccess') { $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); $query="INSERT INTO `archives_logs` (`id`, `file`, `flag`, `date`) VALUES (NULL, '$key', '$random', '$date')"; $query_result = mysql_query ($query); } } // close and save archive $zip->close(); echo "Archive created successfully."; ?> If I place my code file in a location differerent than the protected directory location, i have to change the path of the directory to be compressed which is fine, BUT the problem is that all directories in the path are included in the Zip Archive. So if open the compressed file i get: www/username/public_html/etc... Here is the directories strcuture: www/protect/(files to be compressed here) www/compress_code.php (here is my current code file) The path that I wish to place my code file in is: www/protect/admin/files/compress_code.php Q1) How do I keep my code file in the last mentioned location WITHOUT including the path in my ZipArchive file? Q2) When my code is in the same location of the directory to be compressed, and when i open the compressed file i see, protect/(the files). Can I add only the content of protect directory in the Zip Archive without inclduing the directory itself? Many thanks Quote Link to comment Share on other sites More sharing options...
jacksonmj Posted June 14, 2009 Share Posted June 14, 2009 bool ZipArchive::addFile ( string $filename [, string $localname ] ) Adds a file to a ZIP archive from a given path Parameters filename - The path to the file to add. localname - local name inside ZIP archive. So replace $zip->addFile(.....) with the following, which will remove everything up to and including the first instance of "protect" in the zip file paths. $realFileName = realpath($key); $zipFileName = substr($realFileName,strpos($realFileName,"protect")+7); $zip->addFile($realFileName,$zipFileName) or die ("ERROR: Could not add file: $key"); This will let you solve both problems. Quote Link to comment Share on other sites More sharing options...
ballouta Posted June 14, 2009 Author Share Posted June 14, 2009 thanks jacksonmj everything is great but the files in the zip archive all begin with a / how i can remove it? Quote Link to comment Share on other sites More sharing options...
jacksonmj Posted June 14, 2009 Share Posted June 14, 2009 $zipFileName = substr($realFileName,strpos($realFileName,"protect")+7); Change the +7 above to +8 Quote Link to comment Share on other sites More sharing options...
ballouta Posted June 14, 2009 Author Share Posted June 14, 2009 Many thanks, the files names are correct now. I have another type of problem, I edited the same code to copy the files to another directory, but i am getting this error: Warning: copy(/home/simwsim/public_html/submitted/980995) [function.copy]: failed to open stream: Is a directory in /home/simwsim/public_html/admin/files/index2.php on line 42 failed to copy 123456789123456.ask... <?php require_once("../global.inc.php"); // increase script timeout value ini_set('max_execution_time', 300); //Generate a new flag $random = (rand(000000,999999)); $date = date("y-m-d"); //create a directory to copy the compressed files to mkdir("/home/simwsim/public_html/submitted/$random", 0777); // create object $zip = new ZipArchive(); // open archive if ($zip->open("$date-$random.zip", ZIPARCHIVE::CREATE) !== TRUE) { die ("Could not open archive"); } // initialize an iterator // pass it the directory to be processed $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("/home/simwsim/public_html/protect/")); // iterate over the directory // add each file found to the archive foreach ($iterator as $key=>$value) { if ($key != '/home/simwsim/public_html/protect/.htaccess') { $remove = array("/home/simwsim/public_html/protect/"); $replace = array(""); $file = str_replace($remove, $replace, $key); $realFileName = realpath($key); $zipFileName = substr($realFileName,strpos($realFileName,"protect")+; $zip->addFile($realFileName,$zipFileName) or die ("ERROR: Could not add file: $key"); //Move the file to the submitted directory if (!copy("/home/simwsim/public_html/protect/$file", "/home/simwsim/public_html/submitted/".$random)) { echo "failed to copy $file...\n"; } } else {} }//foreach // close and save archive $zip->close(); echo "Archive created successfully."; ?> Note that the submitted directory already exists. where is the problem? Quote Link to comment Share on other sites More sharing options...
jacksonmj Posted June 14, 2009 Share Posted June 14, 2009 You need to specify the destination filename as well as the directory. copy("/home/simwsim/public_html/protect/$file", "/home/simwsim/public_html/submitted/".$random."/".$file) Quote Link to comment Share on other sites More sharing options...
ballouta Posted June 14, 2009 Author Share Posted June 14, 2009 you are genius i really thank you and appreciate your help. Good luck Quote Link to comment 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.