Jump to content

Simple question about zipping files using PHP


Jason28

Recommended Posts

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 :)

Link to comment
Share on other sites

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';
}



?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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));
	}

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.