Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP ZipArchive Creating entire folder path in ZIP Archive

Posted on 2009-05-13
2
Medium Priority
?
572 Views
Last Modified: 2012-06-22
I just wrote a little zip function into my file uploader for my forum to create an archive from the files that have been uploaded during the session. The files locations are different than the scripts that executes the zip function. it works fine but the zip file contains the entire folder structure of my server from /home/ onwards as i have used absolute paths in the function. I do not want the files put in any folders, just at the root of the zip. could anyone help with this? Thank you, first time using the zip function, just recompiled to get it going.

my code:

// $_CONFIG_DATA['absolute_path']  is server path
// $_CONFIG_DATA['path_to_upload'] is url path
 
function createzipfile($_FILE_DATA, $_CONFIG_DATA, $_POST_DATA)
           {
 
$zip = new ZipArchive();
 
$zipname = $_CONFIG_DATA['absolute_path'] . $_CONFIG_DATA['upload_id'] . ".zip";
$zipurl = $_CONFIG_DATA['path_to_upload'] . $_CONFIG_DATA['upload_id'] . ".zip";
 
 
if ($zip->open($zipname, ZIPARCHIVE::CREATE) !== TRUE) {
    die ("Could not open archive");
}
 
foreach($_FILE_DATA as $slot => $value){
		$file_slot = $_FILE_DATA[$slot]->getFileInfo('slot');
		$file_name = $_FILE_DATA[$slot]->getFileInfo('name');
                $fpath = $_CONFIG_DATA['absolute_path'] . $file_name;
 
                $zip->addFile($fpath) or die ("ERROR: Could not add file: $fpath");
}
    
// close and save archive
$zip->close();

Open in new window

0
Comment
Question by:aiwazz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
Graceful_Penguin earned 2000 total points
ID: 24382396
Try this. I added the second optional parameter to the addFile() function to specify the name of the file in the zip file without a path.
// $_CONFIG_DATA['absolute_path']  is server path
// $_CONFIG_DATA['path_to_upload'] is url path
 
function createzipfile($_FILE_DATA, $_CONFIG_DATA, $_POST_DATA)
           {
 
$zip = new ZipArchive();
 
$zipname = $_CONFIG_DATA['absolute_path'] . $_CONFIG_DATA['upload_id'] . ".zip";
$zipurl = $_CONFIG_DATA['path_to_upload'] . $_CONFIG_DATA['upload_id'] . ".zip";
 
 
if ($zip->open($zipname, ZIPARCHIVE::CREATE) !== TRUE) {
    die ("Could not open archive");
}
 
foreach($_FILE_DATA as $slot => $value){
		$file_slot = $_FILE_DATA[$slot]->getFileInfo('slot');
		$file_name = $_FILE_DATA[$slot]->getFileInfo('name');
                $fpath = $_CONFIG_DATA['absolute_path'] . $file_name;
 
                $zip->addFile($fpath, $file_name) or die ("ERROR: Could not add file: $fpath");
}
    
// close and save archive
$zip->close();

Open in new window

0
 

Author Closing Comment

by:aiwazz
ID: 31581332
Thank you for this! I should've read further at php.net, twas late. Much appreciated!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question