PHP tutorial: file handling


File in PHP

In this tutorial, you learn to do file handling in PHP. PHP provides you a lot of functions that can be used to read and write data to text and binary files, and do other file handling tasks.

Before data can be read or write to a text file or binary file, you must open it by using the fopen function. The fopen function takes two arguments: file path and file mode. The file path is a string that represents the path of the file to be open. The file mode can be read, write, or append mode. The table below summarizes the file modes.

File Mode Description
r opens a text file for reading.
w opens a text file for writing. It creates the file if it does not exist.
a opends a text file for appending. It creates the file if it does not exist.
r+ opens a text file for reading and writing. You will get error if the file does not exist.
w+ opens a text file for writing and reading. It creates the file if it does not exist.
a+ opends a text file for appending and reading. It creates the file if it does not exist.
rb opens a binary file for reading.
wb opens a binary file for writing.
ab opens a binary file for appending.
r+b opens a binary file for reading and writing. You will get error if the file does not exist.
w+b opens a binary file for writing and reading. It creates the file if it does not exist.
a+b opends a binary file for appending and reading. It creates the file if it does not exist.

 

This is an example of using the fopen function to open a text file called myfile.txt. The fopen function returns a file pointer if the file is successfully opened or created. Otherise it returns false. So you have to test it first before reading content of the file or writing data to the file.

$textfile= @fopen('./myfile.txt','r+');

if(!$textfile){

echo "Could not open the file.";

}

else{

//code to read or write data to the file goes here

}

Reading content of a text file

To read data from a text file, you can use the fgets or fread function. The fgets function is able to read content of the text file line by line. The fread function can be used to read a number of bytes from the file. The feof function is used to check whether the end of the file is reached.

<?php

$textfile= @fopen('./myfile.txt','r+');

if(!$textfile){

echo "Could not open the file.";

}

else{

//read file line by line
while(!feof($textfile)){
  $line = fgets($textfile);
  echo $line."<br />";
}

}

//close the file

fclose($textfile);

?>

This is an example of using the fread function to read a number of bytes from the file. The filesize function returns the number of bytes of a file.

<?php

$textfile= @fopen('./myfile.txt','r+');

if(!$textfile){

echo "Could not open the file.";

}

else{

$content=fread($textfile,filesize("./myfile.txt"));
echo $content;
fclose($textfile);

}

?>

Writing data to a text file

You can write data to text file by using the fputs or fwrite function. These functions take two arguments: file handler and the text to write to the file.

<?php

$textfile= @fopen('./myfile.txt','w+');

if(!$textfile){

echo "Could not open the file.";

}

else{

$text="This is a sentence.\r\n";
fwrite($textfile,$text);
//fputs($textfile,$text);
fclose($textfile);

}

?>

Besides reading data from and writing data to a text file, you can read data from and write data to a binary file. The fgets and fread functions can also be used to read content of a binary file. Similarly, the fputs and fwrite functions can also be used to write content to a binary file. The example code below is a function that can be called to copy data from a source image file to a destination image file.

<?php

function readAndWriteImage($sfile,$dfile){
 $sfilep=@fopen($sfile,"rb");
 $dfilep=@fopen($dfile,"wb");
 if(!$sfile){
  echo "Error in reading the source file<br/>";
}
else{
 while(!feof($sfilep)){
  //$byte=fread($sfilep,1000);
  //fwrite($dfilep,$byte);
  $byte=fgets($sfilep,1000);
  fputs($dfilep,$byte);
 }
}
 fclose($dfilep);
 fclose($sfilep);
}

readAndWriteImage("./gmaptr.png","./gmapcopy20.png");

?>

Delete, rename, and copy files

In php, you can delete a file by using the unlink function. The function takes one arguemnt: the file path. The example code below deletes a file called gmaptr.jpg from the current working directory.

unlink("./gmaptr.jpg");

Changing the name of file can be done by using the rename function. This function takes two arguments: source file and destination file paths.

rename("./gmaptr.png","./gmaptr.jpg");

The copy function is used to copy content of a source file to a destination file. It also takes two arguments: source file and destination file paths. Here is an example.

copy("./gmaptr.jpg","./gmaptr.png");

Listing contents of a directory

Sometimes, you might want to get the files and directories in a directory. In PHP, you can accomplish this task by using the scandir function. You will need to supply the path of the directory to this function. The scandir function returns an array that contains the names of files and directories in that directory. You can use the is_file function to check whether an item of the directory is a file.

function getDireContent($sdi){
$list=scandir($sdi);
foreach($list as $value)
{


if(is_file($value))
  echo "file:".$value."<br />";
else
  echo "dir:".$value."<br/>";
 }
}

?>



Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.