PHP File handling
PHP includes a lot of built-in functions for handling files and directories. You can read, write, delete, and get lots of information on files thru the use of these functions. Note that before you start working with files, you have to make sure that you have the right permissions that will allow you to manipulate them. So while you’re trying to figure out why you can’t delete a file, it may be because the server doesn’t allow you to do that.
The creation and removal of files are completed with two functions: “touch()” and “unlink()”. “touch()” searches if a file exists, and if it doesn’t, it creates one, according to the specified filename; “unlink()” is used to remove the file passed on as an argument:
touch(“newinfo.txt”); //create a new file, if it doesn’t already exist unlink(“oldinfo.txt”); //delete a file
Now that you know how to create a file, you should learn how to access it. This is done using the “fopen()” function, with requires a string containing the file path and a string containing the mode in which the file is about to be opened, which tells “fopen()” to open a file for reading, writing, or both. The complete list of the available modes can be found in the PHP documentation. “fopen()” return an integer, also known as a file pointer, which should be assigned to a variable. This will be used later on to work with the opened file. If a file cannot be open for whatever reason, “fopen()” returns FALSE. After you’re done with the file, you should remember to close it with “fclose()”, passing as an argument the file pointer.
$oldfp = fopen(“oldinfo.txt”, “r”); //opens a file for reading if(!$fp = fopen(“newinfo.txt”, “w”)) //tries to open a file for writing die(“Error opening file!”); //terminates execution if it cannot open the file fclose($oldfp);
Reading from files is done with “fgets()”, which reads data from a file until it reaches a new line “n”, an EOF (end-of-file), or a specified length, in this order. So if you specify 4096 bytes but “fgets()” first reaches a new line, it stops further reading. You should know that after each reading or writing to a file, PHP automatically increments an index which holds the current position in the file. So if you have just opened a file, and then read 100 bytes, the next time you will want to read something else using the same file pointer, PHP will continue from where the 101st byte. The “fgetc()” is similar to “fgets()”, except that it returns only a single character from a file every time it is called. Because a character is always 1 byte in size, “fgetc()” doesn’t require a length argument.
$text = fgets($fp, 2000); //reads 2000 bytes at most $chr = fgetc($fp); //reads 1 byte only
While you can read lines using “fgets()”, you need a way of telling when you have reached the end-of-file, so you won’t continue reading without any results. This is accomplished with the “feof()” functions, which returns “TRUE” or “FALSE”, weather the position of a file pointer is at the end of it. You now have enough information to read a file line by line:
$filename = "test.txt"; $fp = fopen($filename, "r") or die("Couldn’t open $filename"); while(!feof($fp)) { $line = fgets($fp); print "$line
"; } fclose($fp);
While “fgets()” works well around text files, you may sometimes want more functionality for your script. The “fread()” functions returns the specified amount of data from a file, and doesn’t take into consideration the end-of-line “n”, like “fgets()”. “fread()” also starts from the current file position, but if you’re not happy with it, you can always change it using the “fseek()” function.
fseek($fp, 100); //moves the file pointer to the 100th byte print(fread($fp, 15)); //outputs 15 bytes
Any writing to a file is done with the use of “fwrite()” or “fputs()” functions, which both use a file pointer and a string to perform the writing:
fwrite($fp, “Hello from PHP!”); fputs($fp, “Hello again from PHP!”);
So far, these functions were great with a single user. However, on public web-sites, with many users accessing your scripts at the same time, your files could quickly become corrupt. PHP offers the “flock()” function, which will lock a file and won’t allow other processes to write or read the file while the current process is running. “flock()” requires a file pointer and an integer to do this:
flock($fp1, 1); //shared lock – allows read, doesn’t allow write flock($fp2, 2); //exclusive lock – doesn’t allow neither read, nor write flock($fp3, 3); //release lock – releases a shared or exclusive lock
There are a lot of other functions that you can use with files like: testing functions – “file_exists()”, “is_file()”, “is_dir()”, “is_readable()”, “is_writeable()”, “is_executable()”; functions that return information on files: “filesize()”, “fileatime()”, “filemtime()”, “filectime()”. You can figure out what the function does by just reading its name; more information about them can be found in the PHP documentation.
If you have found my website useful, please consider buying me a coffee below 😉