saftutorials.com
PHP FILE HANDLING

PHP file handling is a very important part.Ofently you need these PHP functions to perform a different type of task.

You can learn this tutorial, File Handling by PHP.

These are some pre-built functions which are used for files handling.

fopen() function (This function is used for open the file)

fclose() function (This function is used for close the file)

fread() function (This function is used for read the file)

fwrite() function (This function is used for write in the file)

filesize() function (This function is used for get the size of file)

file_exist() function (This function is used for file is existing or not)


These are the modes of file open

r  (open file in reading mode only and file pointer point at the beginning of the file)

w  (open file in write mode only and file pointer point at the beginning of the file)

r+ (open file in reading and write mode both and file pointer point at the beginning of file)

w+ (open file in reading and write mode both and file pointer point at the beginning of file)

a (open file in reading and write mode both and file pointer point at the end of the file and if files do not exist then it attempts to create a file.)

a+ (open file in reading and write mode both and file pointer point at the end of the file and if files do not exist then it attempts to create a file.)


An example of PHP fopen() function

This is the text file named test.txt

PHP stands for Hypertext Processor
PHP is server side language while JavaScript is client side language.
PHP files contains  HTML, CSS, JavaScript, MySQL Query and PHP scripts.
PHP can connect to the database and modify the data tables and database.
PHP can create, delete, open, close, add the file and many other things can do on the server.


This is another php file like home.php

<html>
<head>
<title>Reading a file</title>
</head>
<body>
<?php
$file_name = "test.txt";
$file = fopen( $file_name, "r" );
if( $file == false ) {
echo ( "Error" );
exit();
}
$file_size = filesize( $file_name );
$file_text = fread( $file, $file_size );
fclose( $file );
echo ( "<h2>File size : ".$file_size." bytes</h2>" );
echo ( "<p>$file_text</p>" );
?>
</body>
</html>

The above code produces the following results

File size: 357

PHP stands for Hypertext Processor <!-- This code open from test.txt -->

PHP is server side language while JavaScript is client side language.

PHP files contains  HTML, CSS, JavaScript, MySQL Query and PHP scripts.

PHP can connect to the database and modify the data tables and database.

PHP can create, delete, open, close, add the file and many other things can do on the server.

<!-- End of this code open from test.txt -->


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic