Home » Programming » php » PHP File Handing

PHP File Handing

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

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

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

Output

Another way to read a file:

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

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

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

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.

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

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.

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

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

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

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

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

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

Please follow and like us:
error