PHP File System

PHP File System

This tutorial deals with the opening , closing, reading and writing of file. There are dedicated function for performing these operations in PHP. In order to open a file, fopen() can be used. Calling this function typically requires two parameters namely filename and the mode in which the programmer wishes to open the file. The different modes available in PHP are as follows-

ModeFunction
rThis mode opens the file for only reading. The file pointer points to the first character of the file.
r++This mode opens the file for reading as well as writing. The file pointer points to the first character of the file.
wthis mode opens the file for only writing. The file pointer points to the beginning of the file. Moreover, the file length is truncated to zero. In case the file doesn’t exist, then the system tries to create a file.
w++This mode opens the file for only writing and reading. The file pointer points to the beginning of the file. Moreover, the file length is truncated to zero. In case the file doesn’t exist, then the system tries to create a file.
AThis mode opens the file for only writing. The file pointer points to the end of the file. In case the file doesn’t exist, then the system tries to create a file.
a++This mode opens the file for only writing and reading. The file pointer points to the end of the file. In case the file doesn’t exist, then the system tries to create a file.

If the requested file is successfully opened, the file pointer for the file is returned. On the other hand, if the file opening operation fails, FALSE is the returned to the calling function, As a rule, be sure to close any file that you open. The function used for this purpose is fclose(), which has been discussed below.

Once a file is open, one of the operations that you will perform is the file reading. This is typically done using fread(), which requires two arguments namely, file pointer and the file length in bytes. If the size of the file is unknown, you can use filesize() function to determine the same and only requires the filename as argument. In order to read a file file, the steps that you will need to follow include-

  • Opening the file using fopen()
  • Determining the size of file using filesize()
  • Reading the contents of the file using fread()
  • Closing the file using fclose()

Same code for demonstrating the different steps involved in the process has been provided below for your reference.

<html>
<head>
<title>PHP File System</title>
</head>
<body>
<?php
$file_name="test.txt";
$file1=fopen($file_namem,"r");
if($file1==false){
echo "File cannot open";
exit();
}
$file_size=filesize($file_name);
$file_text=fread($file1,$file_size);
fclose($file1);
echo "Size of File: $file_size bytes";
echo "<pre>$file_text</pre>";
?>
</body>
</html>

You can run this code to see the output it generates. If the file is not present, the program shall give an error message else it shall provide you desired output.

The other function that you can expect to perform with file is writing. It is possible to rewrite a file or append the same on need basis. The function used for this purpose is fwrite(). which requires two arguments namely file pointer to file and the string that needs to be written to the file.

Besides this, this function also expects a third argument, which is the length of data that is to be written. Typically, if you provide this argument, writing to the file will stop as soon as the number of bytes specified in data-size is reached.