Using PHP’s scandir() function to find files in a directory
There are a number of ways to find the files in a directory with PHP. I’ve covered opendir() readdir() and closedir() and glob() in previous posts and now look at scandir().
PHP is a powerful open source server-sided scripting language that can be used on a wide variety of operating systems and webservers, including Apache on UNIX and IIS on Windows.
PHP contains numerous extensions which enable you to dynamically create images and flash files, parse xml, connect to remote servers using pop, imap, ftp, http and raw sockets, natively access many database servers including PostgreSql, MySQL and Microsoft SQL Server, compress and decompress files, lookup domain names and and much more.
PHP has an excellent manual and online documentation which covers every function for every extension available.
There are a number of ways to find the files in a directory with PHP. I’ve covered opendir() readdir() and closedir() and glob() in previous posts and now look at scandir().
I have been in the process of moving my websites from a dedicated machine running Gentoo Linux to a VPS (Virtual Private Server) running CentOS 5. One of the ecommerce websites uses an old install of osCommerce which expects register globals and register_long_arrays on, otherwise things won’t work very well.
While testing the site out on the new server, I couldn’t work out why some of the form posts were working and some weren’t. For example, on some pages I could add items to the shopping cart but on others I couldn’t. I also couldn’t click the form button to proceed to th checkout or update my basket: whenever I tried it kept showing me the same basket page again.
I’ve finally set up an RSS feed for this site and one of the things I needed to sort out was the use of URLs and image tags. All the anchor tags in this site are relative, and so are all the image sources. This isn’t going to work in an RSS feed because it would mean none of the links would work, and the images would all be broken. Fortunately this is easy to fix with the use of PHP’s string replacement functions.
There are several PHP "magic constants" (or "magical contants") which can be useful for a variety of reasons. These magic constants aren’t actually constants at all, but effectively behave like them, although the values change depending on the context.
I run a number of web servers with PHP and MySQL and have a PHP command line script that runs on a daily basis to back up MySQL databases using the mysqldump command. It would be possible to do this using a simple bash script as well, and I know I used to use a bash script in the past but for some reason switched it to PHP at some stage.
It is possible to trigger an error in PHP. This can be useful for debugging purposes, or if there is some condition which occurs in your PHP script which requires a PHP notice, warning or full error which causes the script to halt execution, and which would not be generated by PHP itself.