File handling is used often to read and write the particular file. It is an important part of the web application. In php, you can create a new file, read a particular file, use to upload some on the server and edit files.

readfile() Function

The php readfile() function reads the file and writes the output in the browser.

Example:

[php]<?php
echo readfile("webdictionary.txt");
?>[/php]

Code Explanation:

In the code, the readfile() function tells php to read the file name webdictionary.txt.

The output of the echo statement will look like this.

AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language236

fopen() Function

The fopen() function is more flexibility than readfile() function.

Example:

webdictionary.txt file content

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

Out php code:

[php]<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>[/php]

Code Explanation:

In the php code first, we have created variable $myfile and assigned it to the fopen() function. In the fopon function, we are instructing php script to open the file name “webdictionary.txt” and the “r” text says to open the file to read only. It means the user can’t make any changes to the file.

After the closing bracket of the function, we have added condition called die() if the file unable to open.

On the next line, we ask php to echo the result by using fread() function and specified the above variable and filename to read.

In the end, we have closed the function by using fclose() function.

The above code will generate the following result.

AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language

PHP Modes

1) r – The r mode only reads the file. Pointer start at the beginning of the file.

2) w – It opens the file to write only. It is used to erase the content of the file or create a new file if the file doesn’t already exist in the folder. The pointer of the file starts at the beginning of the file.

3) a – The mode is used to open the file to write only. It preserves the existing data in the file. The file pointer, in this case, starts at the end of the file. It creates a new file automatically if it doesn’t exist in the folder.

4) x – It creates a new file to write only. It will return the false value with an error if the file already exists in the folder.

5) r+ – It opens the file to read or write. The file pointer starts at the beginning of the file.

6) w+ – It opens a file to read and write. It erases the content of the file or creates a completely new file if it doesn’t exist. The file pointer starts at the beginning of the file.

7) a+ – The mode used to open a file for reading or writing. It preserves the data of the file. The pointer starts at the end of the file and creates a new file if it doesn’t exist.

8) x+ – It creates a new file for reading or writing. It shows false value with an error if the file doesn’t exist.

fread() function:

The fread() function is used to read the open file. It will not open the file. For that, you need to use the fopen() function.

Example:

fread($myfile,filesize("webdictionary.txt"));

Code Explanation:

In the above code fread() function is asked to read the file name $myfile. In the next parameter, it is asked to read a specific number of bytes which in this case is the file name “webdictionary.txt”.

fclose() function

The fclose() function is used to close the open file.

Example:

[php]<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed….
fclose($myfile);
?>[/php]

Code explanation:

In the above code we have used fclose() function to close the file name $myfile

fgets() function

The fgets() function is generally used to read a single line from the specified file.

Example:

[php]<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>[/php]

Code Explanation:

In the above code, we have used the echo statement with fgets() function. The function is telling php script to read the single line from the variable name $myfile which is equal to the fopen file name webdictionary.txt. so as per instruction the function reads the file and shows the first line available in the file.

The above code shows result.

AJAX = Asynchronous JavaScript and XML

feof() Function

The feof() function checks if the end of the file has been reached. It is used to check whether the pointer has reached the end of the file.

Example:

[php]<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>[/php]

Code Explanation:

We have used while function to check the end of file has been reached or not. It loops the statement one by one on a single line.

The above code will show the result.

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

fgetc() function

The fgetc() function is used to read a single character from the file one by one.

Example:

[php]<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>[/php]

Code Explanation:

In the above code when we use fgetc() we are telling php script to check each character from the file name webdictionary.txt.

fopen() function to create a file

The fopen() function is used to create a new file if it doesn’t exist in the folder.

Example:

$myfile = fopen("testfile.txt", "w")

Code Explanation;

The fopon() function is used to create a file testfile.txt. It will create a file in the same folder.

fwrite() function

The php fwrite() is used to write a text into the file.

Example:

[php]<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>[/php]

Code Explanation:

In the above code, we have use fwrite() function to write the text $txt in the file $myfile which equal to newfile.txt. We have mentioned the fwrite function twice to write the two separate entries. Once executed we get the following result.

John Doe
Jane Doe

Both the names are written in the file name newfile.txt file.

File Upload

php.ini

First, you need check whether your server allowing the file to uploaded. Check the php.ini file on your server and edit it to find the below line.

file_uploads = On

If it says off then change it to ON and save the file again.

Upload File PHP Script

Example:

[php]<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image – " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>[/php]

Code Explanation:

$target_dir = "uploads/": The variable specifies the directory name where you will be uploading the file.

$target_file : It specifies the path of the file in the directory.

$imageFileType: This variable holds the file extension in the lower case.

Related Posts

Leave a Reply