The following function will rename all of the image files in a directory to be sequential. The parameters are the path of the directory that the files are in and the name of a function that will be used to sort the array of files through the PHP usort() function.
After following the last post on deleting files and directories in Phing you might now be wondering if you can create things as well. The answer is yes.
To create a directory with Phing you need to use the mkdir element. The dir attribute is used to give the name of the directory to be created. The following example will create a directory called myProject_build in the current working directory (ie. where you are running phing from).
Although using Phing is mainly about copying files, you might also need to delete directories and files using the delete element. Remember that the default behavior of copy command copies files only if the source files are different from the destination files. A prudent approach might be to delete the build directory and then recreate it, ready for Phing to copy files into.
Sometimes it is necessary to turn off PHP parsing for a directory. You might want to give away some source code and therefore don't want to parse that code when the user tries to download it. You could just rename the file to have the extension .phps, which is a PHP source file, but this is an alternate method.
To turn off PHP parsing in a directory just create a .htaccess file with the following content.
php_flag engine off
You can expand on this by adding the following:
I have been asked a couple of times recently if PHP has a function that will move a file, so I thought I would put the solution here.
PHP has a function called rename() which can be used to rename a file, but because of the way it works it can be used to move files. Let's say that you wanted to rename a file called test.txt to test_back.txt, this can be accomplished by doing the following.
The normal way of looping through a directory is to get a handle on the directory, then go through each item, making sure that the file is readable and is not "." or ".." before doing something with the file. Because this is done a lot the DirectoryIterator() object was created in PHP5 to simplify this process.
The constructor of the DirectoryIterator() object takes a single parameter, this is the path of the directory that is to be iterated over.
Writing regular expressions can sometimes be a real pain, especially if you are not used to them. Rather than trying for yourself to make a regular expression you might want to think about looking for regular expressions that other people have made. Rather than reinventing the wheel to prove you can do something,using free third party regular expressions can save you a lot of time.
Use the following function to list the contents of one or more nested directories.
For many sites the blog is only part of the site, not the main reason for the site existing. In these cases the blog is kept in the directory /blog or similar. Occasionally (for what ever reason) it might be necessary to change the directory. To that end here is a small walk through of the steps you need to take in order to do this.