Jason28 Posted September 16, 2011 Share Posted September 16, 2011 Hello, I never dealt with such a feature before but I found a small script online that I am editing that works just fine. The problem is that it includes all of the directories in the path of my site in the zip. So if the path of my webhost is: /home/bob/files/ the zip will create a folder such as /home (inside that folder) /bob (inside that folder) /files (then the zipped files I wanted). I just want the zip files and I did store them out of the www/public_html directory to prevent people from downloading the files directly from the web but is there no way to make it not include all of those directories in the zip and only show the files? If so I will post the code that I have. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/247245-simple-question-about-zipping-files-using-php/ Share on other sites More sharing options...
btherl Posted September 16, 2011 Share Posted September 16, 2011 Please show the code. Quote Link to comment https://forums.phpfreaks.com/topic/247245-simple-question-about-zipping-files-using-php/#findComment-1269788 Share on other sites More sharing options...
Jason28 Posted September 16, 2011 Author Share Posted September 16, 2011 Thanks for the quick reply. I realized this problem doesn't happen if the file is located in the www or public_html directory, only when the directory is in a non accessible from the web directory. The following code is the original that works when the file is in the public folder. If you create a directory before the public folder and use the path to it, you will see that it creates multiple folders: <?php /* creates a compressed zip file */ function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false if(file_exists($destination) && !$overwrite) { return false; } //vars $valid_files = array(); //if files were passed in... if(is_array($files)) { //cycle through each file foreach($files as $file) { //make sure the file exists if(file_exists($file)) { $valid_files[] = $file; } } } //if we have good files... if(count($valid_files)) { //create the archive $zip = new ZipArchive(); if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); } //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; //close the zip -- done! $zip->close(); //check to make sure the file exists return file_exists($destination); } else { return false; } } // sample: $files_to_zip = array( 'test.txt', 'index.html' ); $full_zip_path = '/home/bob/public_html/test/'; // trailing slash $zipped_name = 'my-test.zip'; $final_destination = $full_zip_path . $zipped_name; //if true, good; if false, zip creation failed $result = create_zip($files_to_zip, $final_destination); if ( !$result ) { echo 'error'; } else { echo 'zip was successful'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/247245-simple-question-about-zipping-files-using-php/#findComment-1269790 Share on other sites More sharing options...
btherl Posted September 19, 2011 Share Posted September 19, 2011 Ok, can I clarify what the problem you're having is - is it that the files you add to the zip archive keep their directory inside the zip file, and instead you want them to be just files only? If that's the case I would try this: $zip->addFile($file,basename($file)); Based on the documentation here: http://www.php.net/manual/en/function.ziparchive-addfile.php Quote Link to comment https://forums.phpfreaks.com/topic/247245-simple-question-about-zipping-files-using-php/#findComment-1270532 Share on other sites More sharing options...
Jason28 Posted September 19, 2011 Author Share Posted September 19, 2011 Thanks but what part of that code do I replace with the one you provided? Quote Link to comment https://forums.phpfreaks.com/topic/247245-simple-question-about-zipping-files-using-php/#findComment-1270535 Share on other sites More sharing options...
jcbones Posted September 19, 2011 Share Posted September 19, 2011 I would guess: //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); } Quote Link to comment https://forums.phpfreaks.com/topic/247245-simple-question-about-zipping-files-using-php/#findComment-1270536 Share on other sites More sharing options...
Jason28 Posted September 19, 2011 Author Share Posted September 19, 2011 But doesn't that add multiple files into one zip? Quote Link to comment https://forums.phpfreaks.com/topic/247245-simple-question-about-zipping-files-using-php/#findComment-1270537 Share on other sites More sharing options...
btherl Posted September 20, 2011 Share Posted September 20, 2011 I thought you wanted to put multiple files into one zip? And that the problem is that when you open the zip to get the files back, it creates directories as well? If you use this code it should not create the directories when you open the zip file: //add the files foreach($valid_files as $file) { $zip->addFile($file, basename($file)); } Quote Link to comment https://forums.phpfreaks.com/topic/247245-simple-question-about-zipping-files-using-php/#findComment-1270841 Share on other sites More sharing options...
Jason28 Posted September 20, 2011 Author Share Posted September 20, 2011 Excellent, works perfectly I had no idea just adding basename() would be the solution. Quote Link to comment https://forums.phpfreaks.com/topic/247245-simple-question-about-zipping-files-using-php/#findComment-1270911 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.