File Handling in PHP


Share

Powerful as it is, MySQL is not the only (or necessarily the best) way to store all data on a web server.
Sometimes it can be quicker and more convenient to directly access files on the hard disk.
Then we can use file handling functions. There are various file handling function in PHP.

File Handling Functions

1. Opening a file
2. Reading a file
3. Writing a file
4. Closing a file

Checking Whether a File Exists

To determine whether a file already exists, you can use the file_exists function, which returns either TRUE or FALSE and is used like this: if (file_exists("testfile.txt")) echo "File exists";

Creating a File

At this point testfile.txt doesn’t exist, so let’s create it and write a few lines to it.

<?php //testfile.php
$fh = fopen("testfile.txt", 'w') or die("Failed to create file"); 
fwrite($fh, "This is a first file") or die("Could not write to file"); 
fclose($fh);
echo "File 'testfile.txt' written successfully";
?>

This simple example shows the sequence that all file handling takes:
1. Always start by opening the file. This is done through a call to fopen.
2. Then you can call other functions; here we write to the file (fwrite), but you can also read from an existing file (fread or fgets) and do other things.
3. Finish by closing the file (fclose). Although the program does this for you when it ends, you should clean up yourself by closing the file when you're finished.

Reading from Files

Reading function used to read the exiting file.

<?php
$fh = fopen("testfile.txt", 'r') or 
           die("File does not exist or you lack permission to open it"); 
$line = fgets($fh);
fclose($fh);
echo $line; 
?>

Or you can retrieve multiple lines or portions of lines through the fread function, as in below example.

<?php
$fh = fopen("testfile.txt", 'r') or
       die("File does not exist or you lack permission to open it");
$text = fread($fh, 3);
fclose($fh); 
echo $text; 
?>

Copying Files

PHP copy function used to copy the one file text in other file. We can use copy funtion in this task.

<?php  // copyfile.php
copy('testfile.txt', 'testfile2.txt') or die("Could not copy file");
echo "File successfully copied to 'testfile2.txt'";
?>

Moving a File

To move a file, rename it with the rename function, as in below example:

<?php    // movefile.php
if (!rename('testfile2.txt', 'testfile2.new'))
     echo "Could not rename file";
else echo "File successfully renamed to 'testfile2.new'"; 
?>

Deleting a File

Deleting a file is just a matter of using the unlink function to remove it from the filesystem, as in below example:

<?php    // deletingfile.php
if (!unlink('testfile2.new')) echo "Could not delete file";
else echo "File 'testfile2.new' successfully deleted";
?>

Updating Files

Often you will want to add more data to a saved file, which you can do in many ways. You can use one of the append write modes, or you can simply open a file for reading and writing with one of the other modes that supports writing, and move the file pointer to the place within the file that you wish to write to.

<?php    // updatingfile.php
$fh = fopen("testfile.txt", 'r+') or die("Failed to open file");
$text = fgets($fh);
fseek($fh, 0, SEEK_END); 
fseek($fh, 0, SEEK_END); fwrite($fh, "$text") or die("Could not write to file");
fclose($fh); echo "File 'testfile.txt' successfully updated";
?>