Friday, September 6, 2019

PHP File Handling


PHP File Handling


Here, we are going to explain file handing in PHP. File is a sequence of data bytes, which is stored in a group. It is used to store any type of information and can change any time.
When we want to work with any file:
- Create, If it does not exists.
- Open, If it is already exists.
For use any option from above, PHP provides some modes to open / create a file. Modes are:
r Read only. Starts file reading from beginning.
r+ Read/Write. Starts file reading or writing from beginning
w Write only. Start write to file from beginning, truncate file to length zero. If file does not exists, it creates
w+ Read/Write. Start read or write from beginning, truncate file to length zero. If file does not exists, it creates
a Append. Write from end of file. If file does not exists, it creates new.
a+ Read/Append. Append from end of file. If file does not exists, it creates new

We use some usefull function related to file system in PHP. Functions are:

fopen It is use to open a file.
fread It is use to read a file.
fsize It is use to calculate file size.
fgets It is use to read a single line from file. File pointer moved to next line after use this function.
fwrite It is use to write in a file
fclose It is use to close a file.
unlink It is use to delete a file from folder


fopen
We use fopen function to open a file. Here, we use two parameters, one is file name and other is file modes.

For Example:
$file_name = 'abc.txt';
$open_action = fopen($file_name, 'w') or die('File does not exists');


fread
We use fread funtion to read a file. Here, we use two parameters, one is file pointer and other is file size.

For Example:
$file_name = 'abc.txt';
$open_action = fopen($file_name, 'r') or die('File does not exists');
$read_data = fread($open_action, filesize($file_name));
echo $read_data;


fgets
We use fgets function to read a file line by line. File pointer moves to next line after read a line.

For Example:
$file_name = 'abc.txt';
$open_action = fopen($file_name, 'r') or die('File does not exists');
while(!feof($open_action)){
echo fgets($open_action). "<br/>";
}

Here, we used feof in above example. It means, file pointer read file till End Of File(EOF). 

fwrite
We use fwrite to write content in a file.

For Example:
$file_name = 'abc.txt';
$open_action = fopen($file_name, 'w') or die('File does not exists');
$content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';
fwrite($open_action, $content);


fclose
We use fclose to close a file.

For Example:
$file_name = 'abc.txt';
$open_action = fopen($file_name, 'w') or die('File does not exists');
fclose($open_action);

unlink
We use unlink to delete an existing file.

Syntax:
unlink(file_name);

For Example:

unlink('abc.txt');


file_exists
We use file_exists to check, file is exists on given path or not.

For Examle:
if(file_exists('abc.txt')){
echo "File does not exists";
}else{
$fpointer = fopen('abc.txt', 'w+');
}

In above example, we are checking and create a new file, if file does not exists.

No comments:

Post a Comment