I wanted to list a directory in PHP. At the same time, I wanted to make sure only files are listed – not subdirectories or dot directories.
More specifically, I only wanted to include files with the ending .zip.
Two tools come to the rescue here:
scandir() to list the directory and give us an array
and the super handy pathinfo() to check a file extension
Here’s how I did it
// directory we want to scan
// list the contents
Call the function with the directory of your choice – give it a full path like /var/your/directory. Next scandir() creates an array of files and directories which we iterate over with a foreach loop. I’m also formatting the output as an unordered list, hence the presence of some HTML elements in the echo statements.
The first thing we’ll do in the loop is to have a look at each file’s extension. If it is in fact “zip” then we’d like to list it. Other elements won’t show up.
I’ve just realised how (relatively) easy it is to make PHP create a ZIP Archive from a directory and its sub directories.
Previously I had relied on the Linux Shell Command zip or tar to do this which is a convenient one liner. I had assumed that in PHP we had to go through each directory and add the files manually – until I’ve discovered the RecursiveIterator class variants.
In this example I’m defining a root path and a file name for my archive (change at will), then iterate through each file in each directory (and if thats’ a directory too, we’ll go one level deeper). The result of which is an array of files. We’ll get their full paths and add them to the ZIP archive.
echo'<p>There was a problem writing the ZIP archive.</p>';
echo'<p>Successfully created the ZIP Archive!</p>';
The above will produce a ZIP file which will contain references to the absolute full paths of each file. That’s great if you’d like to unzip those files later in exactly the same location – but it’s not so cool if you’re just interested in the content and the idea of 11 sub folders strikes you as “less funny”.
Help is at hand, thanks to the way we can add files to the archive – namely by creating a new local filename. Here’s an example of how this works on Linux systems:
// let's iterate and create a new local file name
No more references to absolute full paths in your ZIP file.
Choose the full version without ZEND Server (not necessary as we’re using MAMP). Unpack the download and put it somewhere safe. I’m adding mine to my Documents directory. I’ll also rename my folder to something like “ZendFramework” without the version number.
To access it from anywhere on our machine we’ll create an alias named “zf”. zf is a shell script provided by the framework that we’ll need throughout our development journey with ZEND. Let’s to this in a Terminal session:
Replace the path with your own. Notice the call to /bin/zf.sh which is the “real” shell script. Our alias has just made this universal and accessible without having to mess with our shell path.
Before we execute a shell command from PHP it’s a good idea to test if the server will respond to it. We can do this by making use of the empty() function.
The following example consists of a helper function you can call before executing the command in question. Then we call it with the shell command we intend to use, before executing the command for real. We’re using ‘uname -a’ here as an example that will generate output and takes a parameter: