How to File Handling in PHP ?


File Handling in PHP :–

  1. Open File – fopen()
  2. Close File – fclose()
  3. Read File – fread()
  4. Write to File – fwrite()
  5. Upload File

1.Open File -fopen() :– 

Files are Opened in PHP using the “fopen()” Command. The Command takes Two parameters . The First Parameter Contain the Name of File be Opened & the Second parameter specifies in which made the file should be opened.


[$ fp = fopen(“my file .txt”,”r”);]
Example =>
< ?php
$ myfile = fopen (“directory.txt”,”r”);
fread( $ myfile , file size(“directory.txt”));

2. Close File – fclose() :–  

The fclose() function is used to close an open file the fclose() requires the name of file (or a variable that holds the name ) we want to close.
Example =>
< ? php
$myfile = fopen (“directory.txt”,”r”);
//Some code to be executed……
fclose . ($myfile);

3.Read File -fread() :–

The fread() function reads from an open file first parameter of fread() contains the name of the file to read from the second. parameter specifies the maximum number of bytes to read.The following PHP code read the “directory.txt”file to the end.
fread ($myfile ,file size(“directory.txt”));

4.Write to File -fwrite() :–

The fwrite() function is used to write a file the first parameter of fwrite() contains the name of the files to write to and the second parameter is the string to be written.
      The example below writes a couple of names into a new file called “newfile.txt”.
Example =>
$myfile = fopen(“newfile.txt”,”w”);
$txt = “aaaaa\n”;
fwrite ($ myfile , $txt);
$txt = “aaaaa\n”;
fwrite($myfile , $ txt);
fclose ($myfile);

5.Upload File:– 

A php script can be used with a  HTML from to allow users to upload file to the server initially files are loaded  into a temporary directory & then  located to a target destination by a PHP script information in the PHP page describes the temporary directory that is used for file uploads _tmp-dir & the maximum Permitted size of files that can be upload is started as upload_max_file size.
Creating an upload Form 
<Title> File Uploading Form </Title>
<h3> File Upload</h3>
select a file the upload:<br/>
<form action = “/php/file-uploader.php” method =”post” enctype =”Multipart form_data”>
<input type = “file name = “file size = “50”>
<input type = “submit value =” upload file”/>
Output :–   File upload :– Select a file to upload

NOTE :– 

Thanks for visit our site .If it has been helpful for you, then definitely share it with your friends and if you are not getting any topic, then please let us know by commenting below, we will add it ASAP.

we always try to make the best notes available to you. If you want notes for other subject or topic, then you can commenting below.

Leave a Comment