CodesJava

Easy learning with example program codes

PHP File Handling


File handling simply means to open a file and to process it according to the required tasks. PHP facilitates several functions to create, read, write, append, delete and close files.

Open File:
To open a file in PHP, fopen() function is used.

Syntax:

resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )

Close File:
To close a file in PHP, fclose() function is used.
Syntax:
ool fclose ( resource $handle )  
Read File:
To read a file in PHP, fread() function is used.
Syntax:
string fread (resource $filename , int $filesize )  
Write File:
To write a file in PHP, fwrite() function is used.
Syntax:
int fwrite ( resource $handle , string $string )  
Delete File:
To delete a file in PHP, unlink() function is used.
Syntax:
bool unlink ( string $filename [, resource $context ] )  

Example:

<!DOCTYPE html>
<html>
<body>
 
<?php
$samplefile = fopen("phpfile.txt", "r");
echo fread($samplefile,filesize("phpfile.txt"));
fclose($samplefile);
?>
 
</body>
</html>

Output

Test file
Posted in PHP   


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap