PHP – Manipulating Files
June 16, 2009 by: Allen SanfordNow I know there are a hundred and one tutorials on how to manipulate files with PHP, but I have to write this tutorial for no other reason than to practice writing. You see I aspire to perhaps write a book or two about programming on the web and well, I am going to need the practice before I actually write the books. So here we go. For those of you who don’t know already my writings are of the southern variety, and I usually don’t try to sound all refined and sophisticated, I usually leave out the unneeded technical mumbo jumbo and stick with just the useful stuff, and sometimes things come out of no where in my writings.
If you plan on doing any kinda of programming for the web, and you do it long enough you are going to run across the need to manipulate files. The creators of PHP were kind enough to give us all kinds of file manipulation routines, some are even a security risk so be careful. Not only are some of these function a security risk but you can tear things up in the system core files or configuration files, for simplicity lets just say you can do a lot of damage, OK. Alright so here is my disclaimer speech, one you mess it up it an’t my fault, two make sure apache is using the www-data user this will help minimize any damage should things go wrong, and three backups are your friend, ok enough onward we go.
Well I guess first things first, if you want to be able to manipulate files I guess you have to have a file to manipulate. Enter fopen now the fopen function needs two parameters, one the filename and two the mode in which to operate which can be anyone of the following:
| mode | Description |
|---|---|
| ‘r’ | Open for reading only; place the file pointer at the beginning of the file. |
| ‘r+’ | Open for reading and writing; place the file pointer at the beginning of the file. |
| ‘w’ | Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. |
| ‘w+’ | Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. |
| ‘a’ | Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. |
| ‘a+’ | Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. |
| ‘x’ | Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. |
| ‘x+’ | Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. |
Now here is an example:
1 2 3 | $filename = "testFile.txt"; $handle = fopen($filename, 'w') or die("Danger, failed to open file!"); fclose($handle); |
Kinda easy uh?
So now we have an empty file so I think we need to put some text in it, I opt for Lorem Ipsum text. Enter fwrite function stage left;
1 2 3 4 5 | $filename = "testFile.txt"; $handle = fopen($filename, 'w') or die("Danger, failed to open file!"); $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac orci metus."; fwrite($handle, $string); fclose($handle); |
Man I don’t anyone can understand this stuff it is so complicated
, those PHP guys were nice to us when they named this stuff. There is also a function called file_put_contents that does what the above code does worth reading up on in the documentation.
OK, so now we can open a file, we can write to a file, how about reading a file. Serious note here there are many ways to read a file in PHP depending on what it is you want to do with it there is fgets, fgetss, fgetc, fgetcsv, file_get_contents so read up in the documentation after you finish up here.
1 2 3 4 5 | $filename = "testFile.txt"; $handle = fopen($filename, 'w') or die("Danger, failed to open file!"); $oneLine = fgets($handle); echo $oneLine; fclose($handle); |
The above you guessed it reads one line from the file. Implement a loop here and you could read the whole file.
Now comes the most unintuitive part of this tutorial, deleting a file.
1 2 | $filename = "testFile.txt"; unlink($filename); |
Who would have thought unlink? Just to clarify before you can delete it it the handle must be closed.
One last example before you go I mentioned file_gets_contents and file_puts_contents do here is an example of that. Notice the lack of fopen and fclose here.
1 2 3 4 5 | $filename = "testFile.txt"; $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac orci metus."; file_put_contents($filename, $string); $contents = file_get_contents($filename); echo $contents; // Will echo the entire contents of that file. |
Well, there you have it my stab at a file manipulation tutorial in PHP. Sure hope my writing gets better with time. Have a Good’n!












Hey, nice post, really well written. You should post more about this.