What is a File?
A file is simply a resource for storing information on a computer.
Files are usually used to store information such as;
- Configuration settings of a program
- Simple data such as contact names against the phone numbers.
- Images, Pictures, Photos, etc.
Opening a File
The fopen() function is used to open files in PHP.The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened:
1 2 3 4 5 6 7 8 9 |
<html> <body> <?php $file=fopen("welcome.txt","r"); ?> </body> </html> |
Different Types Of File MODE
The file may be opened in one of the following modes:
Modes | Description |
---|---|
w | Write only. Opens and clears the contents of file; or creates a new file if it doesn’t exist |
w+ | Read/Write. Opens and clears the contents of file; or creates a new file if it doesn’t exist |
r | Read only. Starts at the beginning of the file |
r+ | Read/Write. Starts at the beginning of the file |
a | Append. Opens and writes to the end of the file or creates a new file if it doesn’t exist |
a+ | Read/Append. Preserves file content by writing to the end of the file |
x | Write only. Creates a new file. Returns FALSE and an error if file already exists |
x+ | Read/Write. Creates a new file. Returns FALSE and an error if file already exists |
Note: If the fopen() function is unable to open the specified file, it returns 0 (false).
Example
The following example generates a message if the fopen() function is unable to open the specified file:
1 2 3 4 5 6 7 |
<html> <body> <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); ?> </body> </html> |
Closing a File
The fclose() function is used to close an open file:
1 2 3 4 5 |
<?php $file = fopen("test.txt","r"); //some code to be executed fclose($file); ?> |
PHP Read File
PHP provides various functions to read data from file. There are different functions that allow you to read all file data, read data line by line and read data character by character.
The available PHP file read functions are given below.
- fread()
- fgets()
- fgetc()
Read File – fread()
The PHP fread() function is used to read data of the file. It requires two arguments: file resource and file size.
Syntax
1 |
string fread (resource $handle , int $length ) |
$handle represents file pointer that is created by fopen() function.
$length represents length of byte to be read.
1 2 3 4 5 6 7 8 |
<?php $filename = "c:\\file1.txt"; $fp = fopen($filename, "r");//open file in read mode $contents = fread($fp, filesize($filename));//read file echo "<pre>$contents</pre>";//printing data of file fclose($fp);//close file ?> |
Reading a File Line by Line
The fgets() function is used to read a single line from a file.
Note: After a call to this function the file pointer has moved to the next line.
Example
The example below reads a file line by line, until the end of file is reached
1 2 3 4 5 6 7 8 |
<?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "<br />"; } fclose($file); ?> |
Reading a File Character by Character
The fgetc() function is used to read a single character from a file.
Note: After a call to this function the file pointer moves to the next character.
Example
1 2 3 4 5 6 7 |
<?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); while (!feof($file)) { echo fgetc($file); } fclose($file); ?> |
PHP Write File
PHP fwrite() and fputs() functions are used to write data into file. To write data into file, you need to use w, r+, w+, x, x+, c or c+ mode.
PHP Write File – fwrite()
The PHP fwrite() function is used to write content of the string into file.
1 2 3 4 5 6 7 |
<?php $fp = fopen('data.txt', 'w');//opens file in write-only mode fwrite($fp, 'welcome '); fwrite($fp, 'to php file write'); fclose($fp); echo "File written successfully"; ?> |
Output: data.txt
1 |
welcome to php file write |
PHP file upload
allow users to upload files from a form can be very useful. Look at the following HTML form for uploading files:
1 2 3 4 5 6 7 8 9 10 |
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> |
Description of above form contents
· enctype: is the attribute that will define that the file to be uploaded is being uploaded using HTML form.
· action: This attribute is used to define the reference of PHP file, that is responsible to handle code for uploading PHP file.
· method:This attribute is used to define the way to transferring mode of contents to the PHP file. It can be GET and POST.
· input type: Using this attribute we can define the maximum size of file in bytes, that can be uploaded.
· input name: This attribute is used to define the name of uploaded file.