Fri, 17 May 2024
NetArt Media     Pay 2, Get 3!    


File System Functions in PHP

By: Anton, NetArt Media
Wed, 1 August 2018
File System Functions in PHP

Manipulating system files with PHP is quite in important process when it comes to web development. The ability to find, edit, copy and back up relevant directory files is something people really can't live without. This article is essentially a continuation of the file reading article I posted last week.

The first example I offer here utilizes the 'file_exists' functions, checking to see if a file does indeed exist and if so, returns a message stating so with a link to the file.


'reading_files.php' is the file I used on my development server last week when I introduced file reading. After defining the variable '$file', I checked to see if it existed using a conditional statement, replaced the underscore with an empty space, removed '.php' and used it to represent a name for the file. This wasn't necessary, however, I do like to use several functions together in order to show what they can do in unison.

I then created a hyperlink the end user can take advantage of if they choose to visit the file that has been searched for. Here is the result in my browser below.

The next example is quite simple, but could come in very handy if you're ever looking for a particular line from a file.


Notice that once again I check to see if the file exists. This is recommended practice, as sometimes files have been deleted out of necessity or even out of mistake. Which will force PHP to spit back an error.

As covered in the article last week, the 'file' function will automatically create an array using each line of a defined file. By using the offset parameter, in this case set at '0', we can locate the first line of 'file.txt'. The result returned to me is shown below.

Link 1

The final example for today's article creates a backup of 'file.txt'. Backing files up cannot be overstated. Every one of us has run into situations where something went wrong and have lost important files. Who wants to re-write them? I certainly don't. Again, this is one of the many reasons I like coding with PHP so much. The ability to back files up simply by loading a page is priceless, in my opinion. In this case, I use the '.bak' extension to create the backup file.


I simply replaced '.txt' with '.bak' for this particular process. This will work with any relevant file extension of course. The result returned in my browser is shown below.

file.txt was successfully copied into file.bak

Keep in mind that I am doing this on a Windows server, so some things may need to be amended for Linux and other operating systems. Also, be careful with these functions. One wrong bit of code and you could end up with a major headache. Normally, functions like these would be used following an HTML form submission. In that case, verifying and validating the information before processing it could prevent any resulting disasters.

I hope you may find this article helpful and thanks for stopping by.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox