Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: php-file-handling

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: php-file-handling
Showing posts with label php-file-handling. Show all posts
Showing posts with label php-file-handling. Show all posts

PHP File Handling

PHP File Handling

php file handling

For New Beginners I know They Think What is File Handling?
In Simple Word Reading and Writing in Files is Called file handling. 

Let's Start:-

1.How we Open Files Using PHP and Read its Contents.
2.How To write in Files.
3.How to Append Text on Existing Files After Old Contents.
4.How to Append Text on Existing Filed Before Old Contents.


List Of Some Important Function Of File Handling.
readfile("path/to/file");  : -   Simply Read the Contents of text files.

file_get_contents("path/to/file"); : - Get Contents from anytypes of file.

fopen("path/to/file","r");  : - fopen is a standard php function used for Opening and writing in files. In its Second Parameter we Passed the mode How we Opening the file.  

fread("file_open_fopen_file",filesize("path/to/file")) : - fread read the file contents.

fclose("file_open_fopen_file") : - Used to Closed the  Open file.

feof("file_open_fopen_file") : - Used to check reached the end of file when reading file line by line in loop.

fgets("file_open_fopen_file") : -Used To Read file Line by line.

fgetc("file_open_fopen_file") : - Used to Read file character by Character.

fwrite("file_open_fopen_file","contents") : - Used to write the contents in opened file.


Here is a List Of Modes For Opening Files.

r    For Reading Only : - For Reading From the Beginning of files.

w   Open a file for write only : - Erase The Contents of Old file if exist and create the new contents.

a   Open a file for write only : -  Used For Appending the contents on Already Exist File if File not Exist Create a new File.

x   Creates a new file : - Create a new file for writing . It Returns false if filename already exist.

r+  Open a file for read/write : -  Used For Reading and Writing Cursor at the beginning.

w+ Open a file for read/write : - Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file.

a+  Open a file for read/write : - Existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist.

x+  Creates a new file for read/write : - Returns FALSE and an error if file already exists


Code 1 . Reading Files Using All Methods and Prints the Contents: 


<?php

//=======file_get_contents("filename")=======================
echo "This is The Contents of file.txt Reading Using file_get_contents function()  : = ".file_get_contents("file.txt")."<br>";
echo "<hr>";


//======readfile("filename")==============
echo "This is The Contents of file.txt Reading Using readfile()  : = <br>".readfile("java.txt")."<br>";
echo "<hr>";


//========fread(fopen("filename"),filesize("filename"))=============
echo "This is The Contents of file.txt Reading Using fopen function()  using mode <b>r</b> : = ".fread(fopen("javascript.txt","r"),filesize("javascript.txt"))."<br>";

php read file


Code 2. Simple Writing Contents in Files

 
<?php

//===Creating File Using file_put_contents (If file Not Exist Then Create a New File)====
file_put_contents("newfile.txt","Hello i am writing data in newfiles.txt");

//==Writing Contents in file using fwrite (If file Not Exist Then Create a New File)============

$file=fopen("newfile2.txt","w");
fwrite($file,"I am New String and Writing Data in newfile2.txt");
fclose($file);


php write


Code 3 . Appending Text in Already Exist Files After The Old Contents

<?php

//===Appending Text in Already Exist Files After The Old Contents=====


$file=fopen("file.txt","a");
fwrite($file,"I am New String and Writing Data in newfile2.txt");
fclose($file);

php write


Code 4 . Appending Text in Already Exist Files Before The Old Contents

<?php

//===Appending Text in Already Exist Files Before The Old Contents======

$file=fopen("file.txt","r+");
fwrite($file,"I am New String and Writing Data in newfile2.txt");
fclose($file);
 

Code 5 . Reading Long Text File Line By Line
<?php

//========Reading File Line By Line==========

$file=fopen("filelong.txt","r");
// Output one line until end-of-file
while(!feof($file)) {
    echo fgets($file) . "<br>";
}
fclose($file);


php read line


Code 6 . Reading Long Text File Character By Character
<?php

//========Reading File Character By Character==========

$file=fopen("filelong.txt","r");
// Output one line until end-of-file
while(!feof($file)) {
    echo fgetc($file) . "<br>";
}
fclose($file);
Download Complete Source Code : Download