January 14th, 2009
Some Useful PHP File And Folder Functions
I have spent the whole day today dealing with files and folder structures with PHP and even though I have dealt with this quite a bit in the past, I still found that I needed to double check some of the functions on PHP.net. So I thought I would create a quick little reference guide on some of the more useful file and folder PHP functions. So, here they are:
dir(’dir_path’)
This is a very common function and what it does is open the directory you pass to it and then it returns an object which you can then use to read all the files and folders in that directory. For more info on it’s implementation check out the dir() function on PHP.net.
mkdir(’dir_path’, permission, recursive)
The mkdir() function is a simple little one which creates a directory on your server. All you do is pass it a path (make sure the parent directory is writable), set the permissions of the directory you are about to create and if you are creating multiple sub-directories, mark the recursive parameter as TRUE and it will create all the sub-directories for you. Check out the mkdir() function on PHP.net for more.
rmdir(’dir_path’)
rmdir() is similar to mkdir but instead of creating a directory, it deletes the directory. You set the path of the directory you want to remove (make sure that it is empty and the permissions are set to be able to delete the folder) and then the function does all the rest. Take a look at PHP.net to learn more about the rmdir() function.
is_dir(’dir_path’)
This function simply checks the path you give it and makes sure that it is a directory. If you pass it a file or a non-existent directory it will return false. If the path does point to a directory, the function will return true. It is a simple function, but can be very useful. You can learn more about is_dir() and its implementation on php.net.
scandir(’dir_path’)
The scandir() function was probably the one I got the most use out of. It is a very cool function which returns an array of files listed in the specified directory. It is extremely useful for checking if a folder is empty or not. If you are using PHP 4, unfortunately it wont work, it is a PHP 5 specific function. Goto the scandir() function page on PHP.net to see how to implement it.
unlink(’file_path’)
This fancy function simply deletes the file that you specify. You pass it the path of the file you want deleted and it takes care of the rest. Again, it is an extremely useful function and I use it extensively. Read more on the unlink() function on PHP.net.
is_file(’file_path’)
And last but not least on my list of useful file and folder functions is is_file(). It is exactly the same as the is_dir function, but checks if the path specified is a file or not. Visit PHP.net to learn more about the is_file() function.

















Thanks for this – just what I was looking for
How to read the entire ms-word dile and also write in php? Plz send asap……OK By Tc