Eliminate zip file subfolders

Richard Korts
Richard Korts used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Sounds real good, I can probably try it tomorrow.

Thanks, Chris.
Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Chris,

By DIRECTORY_SEPARATOR you mean "/"?

Is DIRECTORY_SEPARATOR a magic predefined constant in php?

Thanks,

Richard
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.
Richard KortsBusiness Owner / Chief Developer

Author

Commented:
OK, get it.

Thanks
Richard KortsBusiness Owner / Chief Developer

Author

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
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Good news Richard. Glad it's working.

Not sure what you mean by the interim folder being empty?
Richard KortsBusiness Owner / Chief Developer

Author

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
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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 !
Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Yes, I agree.

I can explain it all, too busy.

Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial