IDIOT DEVELOPER

File Handling in PHP

File handling is one of the task that every web developer must know about. Web basically deals with data, and sometime we need files to store data. So in this tutorial we will learn about File handling in PHP, and explore various other things related to it. We will explore basic of reading a file to some more advance concepts like file permission, etc.

 

In this tutorial, we are going to deal with:

1. Opening a file.

2. Reading content of a file.

3. Writing content to a file.

4. Appending content to a file.

5. Closing the file.

 

Opening a File

The PHP fopen() function is used to open a file. It requires two argument, first the file name with its correct location and second the mode in which the file needs to be open.

 


$filename = '/var/yourdirctory/yourfile.txt';
$file_mode = 'r';

$fp = fopen($filename, $file_mode);

 

A file can be opened in the following modes:

 

1. : Open file in read-only mode. It places the file pointer at the beginning of the file.

 

2. r+  : Open file in read and write only. It also places the file pointer at the beginning of the file.

 

3. w  : Open the file in write mode. It places the file pointer at the beginning of the file and it clears the content of the file also. If the doesn’t exist then it attempts creates the file.

 

4. w+  : Open the file in read and write mode only. It places the file pointer at the beginning of the file. If the doesn’t exist then it attempts creates the file.

 

5. a  :  Open the file in write mode only. It places the file pointer at the end of the file. If the doesn’t exist then it attempts creates the file.

 

6. a+  :  Open the file in read and write mode only. But it places the file pointer at the end of the file. If the doesn’t exist then it attempts creates the file

 

NOTE: In the above cases if a file fails to open then fopen will return false. Otherwise it will returns the file pointer.

 

7. x  :  Create and Open files in write mode only. Creates a new file and returns FALSE and an error if file already exists.

 

8. x+  :  Create and Open files in write mode only. Creates a new file and returns FALSE and an error if file already exists.

 

These are some of the basic modes. For more visit PHP.net – fopen modes.

 

NOTE: If file exists on the location and your file pointer i.e. $fp returns false then it might be possible that your apache user does not have permission to open that file. Also it might be possible that your apache user has permission to read only but you are trying to open in write mode.

 

Reading a File

After you opened the file using the fopen() and you have a file pointer. After then you can use fread() function to read the content of the file. This function requires two argument first the file pointer, and second the number of bytes.

The size of the file (no. of bytes) can be find using the filesize() function. it requires only one argument that is name of the file.

 


$path = 'file.txt';
$fp = fopen($path, 'r');

if(!$fp){
  echo 'Error: Cannot open the file.';
}else{
  $filesize = filesize($path);
  $file_content = fread($fp, $filesize);
  echo $file_content;
}

fclose($fp);	

 

Writing a File

A new file can be written using the fwrite() function in PHP. This function requires only three arguments. First the file pointer. Second the data that needs to written into the file. Third is an optional argument, it specify the length of the data to be written . If the third argument is included then the writing would stops when the specified length is reached.

 


$path = 'file.txt';
$fp = fopen($path, 'w');

if(!$fp){
  echo 'Error: Cannot open the file.';
}else{
  $data = "This is a file written in PHP.";
  fwrite($fp, $data);
}

fclose($fp);	

 

Appending to a File

Appending data to a file is same as writing data to a file. The main difference is that in appending we write and add data at the end the file.

 


$path = 'file.txt';
$fp = fopen($path, 'w');

if(!$fp){
  echo 'Error: Cannot open the file.';
}else{
  $data = "Appending text to the file.";
  fwrite($fp, $data);
}

fclose($fp);	

 

Closing a file

After making a changes to the opened file it is important to close the file. We can close a file using the fclose() function in PHP. The fclose() function requires a file pointer as its argument and then returns true when the closure succeeds or false if it fails.

 

Leave a Reply

Your email address will not be published. Required fields are marked *