Eliminate zip file subfolders

I have this code (self contained, referenced by a javascript window.location):
<?php
// dnownload_docs.php
$files = explode("~", $_GET['files']);
$nf = count($files);
// find the one from summsp
for ($i = 0; $i < $nf; $i++) {
	if (substr($files[$i],0,10) == "../summsp/") {
		$ln = strlen($files[$i]);
		$fn = substr($files[$i],10,$ln - 10);
		//echo "summary file = " . $fn . "<br>";
		copy($files[$i], "thold/" . $fn);
		break;
	}
}
$files[$i] = "thold/" . $fn;	
//echo "nf = " . $nf . "<br>";
session_start();
$_SESSION['del_summ'] = $fn;
// zip em up
if ($nf > 1) {
	$zip = new ZipArchive();
	$zipname = $_SESSION['vid'] . "zip.zip";
	$path = "zips/" . $zipname;
	$zip->open($path, ZipArchive::CREATE);
	for ($j = 0; $j < $nf; $j++) {
		//echo "file = " . $files[$j] . "<br>";
		$zip->addFile($files[$j]);
	}
	$zip->close();
} else {
	$path = $files[0];
}
// download
	header('Content-type: application/force-download'); 
    header('Content-Transfer-Encoding: Binary'); 
    header('Content-length: ' . filesize($path)); 
    header('Content-disposition: attachment; filename=' . basename($path)); 
    readfile($path);

Open in new window


It works perfectly, EXCEPT the zip file contains two folders, doc_files and thold.

We don't want to assume the users are computer literate enough to get it that they have to click on the subfolders to get the actual files.

Is there any way I can force ZipArchive to force them all into ONE folder with an intuitive name like "documents"?

The attached file is a screen shot of what the users will see now.
zip_download.jpg
Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Hey Richard,

The addFile method of the ZipArchive class takes an optional second argument that defines the name and location inside of the zip.

Also, you may find it a little easier to use a foreach instead of your for loop:

foreach ($files as $file):
    $internalFile = "documents" . DIRECTORY_SEPARATOR . basename($file);
    $zip->addFile( $file, $internalFile );
endforeach;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Sounds real good, I can probably try it tomorrow.

Thanks, Chris.
0
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Chris,

By DIRECTORY_SEPARATOR you mean "/"?

Is DIRECTORY_SEPARATOR a magic predefined constant in php?

Thanks,

Richard
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Chris StanyonWebDevCommented:
Hey Richard,

Yeah ... DIRECTORY_SEPARATOR is a predefined constant and will ensure your code works across all OSes. Windows uses backslashes whereas Linux uses forward slashes. You may get away with hardcoding the slash in yourself, but better safe than sorry - use the constant and it will always be correct, regardless of OS.
0
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
OK, get it.

Thanks
0
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Chris,

Works perfectly, in fact even BETTER than I expected. The interim directory is empty after it is downloaded, which is perfect.

Why is that?

Thanks
0
Chris StanyonWebDevCommented:
Good news Richard. Glad it's working.

Not sure what you mean by the interim folder being empty?
0
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
I create a directory called docs-xxxxx (xxxxx is the unique ID of the visitor) at the site root using php mkdir. Then I use your code to put the docs in there, they download fine.

But afterwards, the directory docs-xxxxx is empty, which, FYI, is perfect, I just wondered why it went away,

Richard
0
Chris StanyonWebDevCommented:
Hmmm. Not sure. In your code above, you're creating a the zip file at this location: "zips/" . $zipname;

After that, all my code does is add the various files to that zipfile. I don't really see anywhere that the docs-xxx comes into it.

But .... as they say .... if it ain't broken, don't fix it !
0
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Yes, I agree.

I can explain it all, too busy.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
zipped files

From novice to tech pro — start learning today.