M (Minor edits)
M (Minor corrections)
Writing to a file is done by using the [http://php.net/fwrite() ''fwrite()''] function in conjunction with [http://php.net/fopen() ''fopen()''] and [http://php.net/fclose() ''fclose()'']. As you can see, there aren't as many options for writing to a file as there are for reading from one. However, PHP 5 introduces the function [http://php.net/file_put_contents ''file_put_contents()''] that simplifies the writing process somewhat. This function will be discussed later in the PHP 5 section, as it is fairly self-explanatory and does not require discussion here.
The extra options for writing don't come from the amount of functions, but from the modes available for opening the file. There are three different modes you can supply to the [http://php.net/fopen ''fopen()''] function, if you wish to write to a file. One mode, 'w', wipes the entire contents of the file so anything you then write to the file will fully replace what was there before. The second mode, 'a' appends stuff to the file so anything you write to the file will appear just after the original contents of the file. The final mode 'x' only works for non-existent files. All three writing modes will attempt to create the file, if it doesn't exist whereas the 'r' mode will not.
Error checking is important for any sort of programming, but when working with files in PHP it is especially important. This need for error checking arises mainly from the filesystem the files are on. The majority of webservers today are Unix-based and so, if you are using PHP to develop web-applications, you have to account for file permissions. In some cases PHP may not have permission to read the file and so, if you've written code to read a particular file, it will result in an ugly error. More likely is that PHP doesn't have permission to write to a file and that will again result in ugly errors. Also, the file's existence is (somewhat obviously) important. When attempting to read a file, you must make sure the file exists first. On the other side of that, if you're attempting to create and then write to a file using the 'x' mode then you must make sure the file ''doesn't'' exist first.
In short, when writing code to work with files, always assume the worst. Assume the file doesn't exist and you don't have permission to read/write to it. In most cases this means you have to tell the
There are two main ways of error checking. The first is by using the '[http://uk.php.net/manual/en/language.operators.errorcontrol.php @]' operator to suppress any errors when working with the file and then checking, if the result is '''false''' or not. The second method involves using more functions like [http://php.net/file_exists ''file_exists()''], [http://php.net/is_readable ''is_readable()''] and [http://php.net/is_writeable ''is_writeable()''].
|As you can see, the '@' operator is used mainly when working with the fopen() function. It can be used in other cases, but is generally less efficient.
// But might want to create the file instead
$handle = @ fopen($
echo 'PHP does not have permission to create a file in the current directory.';
Line-endings were mentioned briefly in the final example in the 'Reading' section of this chapter and it is important to be aware of them when working with files. When reading from a text file it is important to know what types of line-endings that file contains. 'Line-endings' are special characters that try to tell a program to display a new line. For example, Notepad will only move a piece of text to a new line, if it finds "\r\n" just before the new line (it will also display new lines, if you put word wrap on).
If someone writes a text file on a Windows system, the chances are that each line will end with "\r\n". Similarly, if they write the file on a Classic Macintosh (Mac OS 9 and under) system, each line will probably end with "\r". Finally, if they write the file on a Unix-based system (Mac OS X and GNU/Linux), each line will probably end with "\n".
Most of the time though, it is just sufficient to be aware of their existence within the text so you can adjust your script to cope properly.
So far, all of the text seen in this chapter has been assumed to be encoded in some form of plaintext encoding such as UTF-8 or ASCII. Files do not have to be in this format however and in fact there exist a huge number of formats that
Since about PHP 4.3, this is no longer necessary as PHP will automatically detect, if it needs to open the file as a text file or a binary file and so you can still follow most of the examples shown here.
Working with binary data is a lot different to working with plaintext strings and characters and involves many more functions that are beyond the scope of this chapter
Serialization is a technique used by programmers to preserve their working data in a format that can later be restored to its previous form. In simple cases this means converting a normal variable such as an array into a string and then storing it somewhere. That data can then be unserialized and the programmer will be able to work with the array once again.
There is a whole chapter devoted to [[Programming:PHP/Serialization|Serialization]] in this book as it is a useful technique to know how to use effectively. It is mentioned here as one of the primary uses of serialization
In PHP, serialization is very easy to perform through use of the [http://php.net/serialize ''serialize()''] and [http://php.net/unserialize ''unserialize()''] functions. Here follows an example of serialization used in conjunction with file functions.