Hi All, I'm sorta stuck on this one. Hopefully some of you folks can help me out...

My question in a nutshell: Is there a way to have PHP extract all of the files from an archive without preserving the directory structure?

Extra Info:
A site I'm working on allows users to upload pictures. I've just added support for Zip files, so that PHP extracts the images from the Zip, moves them to the appropriate directory, and catalogs them in the MySQL database. To do this, the names of the files are put into an array.

The problem is that if the Zip file contains any sub-directories, the name of the sub-directory is written to the array and the files contained within are skipped. I understand that this is because PHP is seeing the sub-directory as a file and not a directory.

Recommended Answers

All 2 Replies


By the sounds of your post, you have obviously got the unzipping part of your zip file upload process working.

When iterating through the unzipped directory, why not do it recursively, checking each item within the directory to see whether it is a file or directory? Using the function is_dir would achieve this.

If you encounter a sub-directory, you could move it's content into the root of the current directory. One thing to bear in mind here would be file and directory name clashes.

Alternatively, you could put the onus on the user, and disallow nested directories. I.e. upload the zip file, iterate through each item checking whether it is a file or directory, and if a directory is found, inform the user that their upload contained nested directories.

Just some thoughts to start with...


Yeah, the extraction works perfectly- just too perfectly, sorta. I thought of adding to my array-making function some code to check for directories and such, but I was hopeful that the easier solution would be to work with a "flat" directory structure (ie no hierarchy) by telling having the extraction discard the structure.

So, was I wrong in my hopes that this would be possible?

If it isn't possible, I'll try working with my array function and using is_dir with it, but I would definitely prefer making the extraction do the heavy lifting.

PS- Good call on the name clashes- I will bear that in mind if it comes down to messing with that function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.