PHP has a handy function called filesize() which can help us display how big a file is.
It does this in bytes by default, but with a bit of tinkering we can easily convert this into kilobytes or megabytes.
Let’s first see how this works in bytes:
echo"The size of your file is $filesize bytes.";
Converting bytes into kilobytes works by dividing the value by 1024. PHP is very accurate and will give you 12 decimal digits – perhaps a little overkill. To avoid this we can make use of the round() function and specify how many digits of accuracy we’d like to display:
$filesize=round($filesize/1024,2);// kilobytes with two digits
echo"The size of your file is $filesize KB.";
To display the value in megabytes we’ll simply divide by 1024 twice:
$filesize=round($filesize/1024/1024,1);// megabytes with 1 digit
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: