PHP's syntax for reading and writing files is not easy to grasp at first sight. CI's file helper contains a few useful functions, which act as a wrapper for PHP's own file handling operations. Start off as always by loading the helper:
Then life gets a lot simpler. For instance, to write to a file, all you need to know is:
• The location of your file.
• The mode in which you want to open the file. Modes are defined in the PHP manual (see the page on 'fopen'). They include 'r' for read, 'w' for write (write to the file, overwriting data already there), and 'a' for append (write to the file, adding on to existing data). In each case, adding a '+', say 'a+', opens the file for both read and write operations. 'a' and 'w', but not 'r' or 'r+', also create the file, if one is not already there.
Then you use these three pieces of information as parameters to the write_file() function:
This is simpler and more intuitive than PHP's two-step code:
Once again, the CI code adds a little extra: it automatically locks the file before writing and unlocks it afterwards. The helper returns 'FALSE' if the file operation doesn't take place, so you can use it to report success or failure. You have to specify a title for your file, but if you don't specify a filepath, it is placed in the web root folder for your site, where your main index.php file is.
Of course, any folder in which you create or write to a file, must have I
write permissions set. Remember also that if you are running on I
a Windows system you have to use forward slashes — /— to describe I
your filepath. I
In our application, we can combine this helper with the database utility class. This allows us to create, back up, repair, and optimize databases and tables, though only on MySQL and MySQLi databases. Mix it in with the file helper, and you create a neat backup routine.
The above code writes the latest version of our database to a file on the server. Reading a file back again is equally simple:
There's also a function that returns an array of all files and/or folders in a given directory:
although, if you use it in a directory with many files, you may find that PHP times out before it can list them all. You can use this in a simple piece of code to check that the files or folders actually in a folder are what you expect. Start by using the CI function to find the files actually present, and a reference array of files you expect to find, then use array_diff() to compare them. Given two arrays, array_diff() tells you what values are in the first that are not in the other, so you have to use it twice, putting each array first.
//list files actually found
$files_there = get_filenames('e:/rootfolder/system/application/
// list files we expected
$files_expected = array('start.php', 'index.php'); // any found that we didn't expect?
$difference = array_diff($files_there, $files_expected);
// any expected that we didn't find?
$difference = array_diff($files_expected, $files_there);
Lastly, but far too horrible even to think about, there is the delete_files() function. This deletes all the files within any directory you specify, so that:
would delete everything in mydirectory. If you add the optional parameter true, as in:
it will also delete all subfolders in that directory Use with great care: just imagine what:
Was this article helpful?