File handling is need for any web application so FILE HANDLING is an important part of PHP language. Some task to be done file needs to be processed. PHP have many function handling any kind of normal files. (Liketxt, docx, csv, jpg etc)
Handle a file need to know below topics
i. File create
ii. File open
iii. File read
iv. File write
v. File modify
vi. File close
vii. File remove
i. File Create
Create any kind file through PHP use fopen() function.fopen() function mainly use to open a file in different mode.
File modes are r for read, w for write and a for append. In this function require two arguments. First argument is file name and second argument is file mode value. It is always open a file with specific mode but if the file is not present then create a blank file with the file name.
touch() function also create new file. Only one argument is required the file name or file location where want to create.
Example
1 2 3 4 5 | <!--?php fopen('sample.txt','w'); touch('new text file.txt'); echo"File is creating in the same directory where the program is executed!"; ?--> |
Output
File is creating in the same directory where the program is executed!
ii. File Open
fopen() function is used to open a file with a specific mode.
Example
1 2 3 4 | <!--?php fopen('sample.txt', 'r'); echo"File open in read mode"; ?--> |
Output
File open in read mode.
Note: if file is not present in this location then PHP return a warning. Like, in above example if sample.txt file doesn’t exist in this location then output is “Warning: fopen(sample.txt): failed to open stream: No such file or directory”
iii. File Read
Read any kind of file use fopen() function with r mode(Read mode). There is a another function fread() which is used to store or directly display the opened file contents as a string. fread() function have two argument first the file value (which is open in r mode using fopen()) and second is file size (how much bytes want to read from the file). Want to read all data of the file use filesize() function which return the original file size in bytes. filesize() have only one arguments, valid file name is required.
Example
1 2 3 4 5 | <!--?php $handle = fopen('poem.txt', 'r'); $data = fread($handle,filesize('poem.txt')); echo $data; ?--> |
Output
1 | How do I love thee? Let me count the ways. I love thee to the depth and breadth and height My soul can reach, when feeling out of sight For the ends of Being and ideal Grace I love thee to the level of everyday's Most quiet need, by sun and candle-light. I love thee freely, as men strive for Right; I love thee purely, as they turn from Praise. I love thee with the passion put to use In my old griefs, and with my childhood's faith I love thee with a love I seem to love With my lost saints, - I love thee with the breath, Smiles, tears, of all my life! - and, if God choose, I shall but love thee better after death. |
Another way to read a file:
1 2 3 4 5 6 | <!--?php $handle = fopen('poem.txt', 'r'); while(!feof($handle)){ echo fread($handle,1024); } ?--> |
Explanation
In this example return the same output as the previous one.
feof() function is file end of the file. There is one argument, the file value. Means if the file content is not ended the while loop is continue. Here fread() is only read 1kb data at a time so whenever the file size is to over while loops continue, after complete the reading of the whole file content while loop automatically stop.
Only some data can be read in a large file using fread() function
1 2 3 4 | <!--?php $handle = fopen('poem.txt', 'r'); echofread($handle,100); ?--> |
Output
How do I love thee? Let me count the ways. I love thee to the depth and breadth and height My
Note: Here only 1kb data read from the whole file.
Read online file directly
file_get_contents() is a PHP function which directly read a file from Internet. Only one argument is required in this function the URL value. But it’s also read local server file so without using fopen(), fread() can be read any file data using file_get_contents().
Example
1 2 3 | <!--?php echofile_get_contents('http://textfiles.com/stories/3wishes.txt'); ?--> |
Explanation
Its return the whole data of this URL file in local server.
iv. File Write:
Write any file using PHP the open the file in w mode in fopen(). Write mode. After that use fwrite() function to write content in this file. fwrite() function have two arguments filename where want to write and another the content value which want to write in this file.
Example
1 2 3 4 | <!--?php $handle = fopen('sample.txt', 'w'); fwrite($handle, 'This is a sample file...'); ?--> |
Explanation
After executing the program ‘This is a sample file…’ content is written in the ‘sample.txt’ file. But add another content with previous content is not possible in w mode. If execute the program with new content the only new content value is present in the text file previous data will be lost.
1 2 3 4 | <!--?php $handle = fopen('sample.txt', 'w'); fwrite($handle, 'Add something with old content'); ?--> |
After execute this one ‘Add something with old content’ this value only store in the ‘sample.txt’ file.
‘This is a sample file…’ will be replaced by the current content.
v. File Modify
Update file content then open the file in a mode. Append mode.
Example
1 2 3 4 | <!--?php $handle = fopen('newsample.txt', 'a'); fwrite($handle, 'This is ananother sample file...'); ?--> |
Explanation
After executing the program ‘This is another sample file…’ content is written in the ‘newsample.txt’ file. In append mode can be modified previous content of the file and add new content in this file.
1 2 3 4 | <!--?php $handle = fopen('sample.txt', 'w'); fwrite($handle, 'Add something with old content!'); ?--> |
After execute this one output is ‘This is an another sample file…Add something with old content!’
vi.File Close
After Open a file it must be need to close the file after operation. fclose() is used to close file. Only one argument is there the file value.
Example
1 2 3 4 5 | <!--?php $handle = fopen('sample.txt', 'w'); fwrite($handle, 'This is a sample file...'); fclose($handle); ?--> |
Output remain same.
vii File Remove
Remove or Delete any existing file use unlink() function. There have only one argument. The file name or file location/path is required which want to delete.
Example
1 2 3 | <!--?php unlink('sample.txt'); ?--> |
After execute the program ‘sample.txt’ will be permanently deleted from the folder.
viii Rename File
In PHP language rename any file name use rename(). There have two arguments. First one is the original file name or original file location and second one is new name value of this file with location.
Example
1 2 3 | <!--?php rename ("sample.txt", "newfile.txt"); ?--> |
After execute the program ‘sample.txt’ file name will be changed into ‘newfile.txt’.
ix Copying File
Copy one file content to another file use copy() in PHP. There have two arguments. First one is the copied file name or location and the second one is the file name or file location where to want to copy. Simply copy from source to destination first argument is source file name or location and second is destination file name or location.
Example
1 2 3 4 | <!--?php copy('flower.jpg','copy-flower.jpg'); echo "<IMG SRC='copy-flower.jpg'-->"; ?> |
Output
Usage of file handling
In real life web application there are many usage of file handling. Here is a one example of usage of file handling is downloading. Download online file into PC using PHP.
Example
1 2 3 4 5 6 7 8 9 10 11 | <!--?php $pic = "https://images.pexels.com/photos/36770/flower-lily-lilium-candidum-madonna-lily.jpg"; //Read $data = file_get_contents($pic); //Write $handle = fopen("picture.jpg", "w"); fwrite($handle, $data); fclose($handle); //Display echo 'Done! <IMG SRC = "picture.jpg"-->'; ?> |
Output
Done!
Images Used from Pexels Free stock photos
https://www.pexels.com/photo/white-and-yellow-flower-with-green-stems-36764/
https://www.pexels.com/photo/nature-blue-summer-yellow-36770/
Text file used from
http://textfiles.com/stories/3wishes.txt