Saturday 23 April 2011

File Handling Concepts in PHP





File Handling Concepts in PHP


*By using this concept we can read the contents of a file. We can also write the contents in a file.
*To operate a file, first we need to open the file using “file mode”.

R(read) : To read the file contents. If you open the file using read mode the file pointer locates at the beginning of the file.

W(write) : If you open the file with write mode, we can write the content in file. First it removes the previous content of the file and places the file pointer at the beginning of the file. If the file is not available it creates a new file.

A(append) : To append the new content to the existing file. The file pointer locates at the end of the file. If the file is not available it creates a new file.

RT(read,write) : To read and write the contents of a file. It will not create the new file, if the file is not available. It doesn’t remove the previous data.

WT : It Is same as write mode, we can also read and write the file contents. It will create a new file, if the file is not exists. It removes the previous data.

AT: By using this mode we can read and also we can append the file contents.

Fopen: By using this mode we can open the files.

Fread: To read the contents of a file. It contents two arguments 1.file pointer 2. Size in bytes.

Fwrite: To write the contents in a file. The arguments are file pointers and new content.>It over writes the previous content.

Filesize: It returns the total number of bytes occupied by the file.


Practice Scripts on Above Concepts
1. Sample Practice Scripts
<?php
$fp=fopen("abc.txt","r");
$size=filesize("danphp.txt");
$cont=fread($fp,$size);
echo $cont;
?>
* Save one text file in htdocs as as “danphp.txt”, you can see the display of the document text on the browser.
2. Sample Practice Scripts



<?php
$fp=fopen("abc.txt","w");
echo fwrite($fp, " New Content");
?>
*If the file is not exits it will create a new file.

3. Sample Practice Scripts

<?php
$fp=fopen("abc.txt","a");
echo fwrite($fp,"New Cont");
?>

rewind:  By using this function we can locate the file pointers at the beginning of the file.


<?php
$fp=fopen("abc.txt","rt");
fwrite($fp,"smith");
rewind($fp);
echo fread($fp,filesize("abc.txt"));
?>

fseek: By using this function we can locate the file pointers in the specified location.


<?php
$fp=fopen("abc.txt","wt");
fwrite($fp,"PHP is SSS");
fseek($fp,2);
echo fread($fp,filesize("abc.txt"));
?>

fgets: This function is used to read a line from a file and locates the pointers at the beginning of the next line.

<?php
$fp=fopen("abc.txt","r");
$lc=0;
while ($line=fgets($fp))
{
$lc++;
echo $line;
}
echo "<br> Total Lines Are $lc";
?>

fgetss: It is same as “fgets”, but it ignores the HTML elements.

fgetc: By using this function, we can read a character from a file.

file_get_contents: By using this function we can get the all contents of a file, without file 
pointers.

file_put_contents: By using this function we can put some content in a file without file pointers.


<?php
echo file_put_contents("abc.txt", "scott");
?>
readfile: By using this function, we can read the contents of a file and at the same time we can write the file contents on the buffer.

filectime: This function returns the created date nad time information of a file in time stand format.
Time stand format-: total number of seconds from Jan 1st 1970.

filemtime: This function returns the modified date and time information of a file.

fileatime: It returns the last access date and time information of a file.

copy: This function copies the specified file into the target location.

file_exists: By using this function we can check whether the input file is existed or not.

unset: By using this function we can delete the file permanently from hard disk

1.Dan PHP Sample Script
<?php
readfile("abc.txt");
?>
2. Dan PHP Sample Script

<?php
$arr=file(“abc.txt”);
print_r($arr);
?>


mrdir: This function is used to create the new directory.

rmdir: This function is used to remove the directory.

opendir: This function is used to open a directory.

readdir: This function is used to read the content of a directory.

closedir: This function is used to close directory.

scandir: This function reads the all files of a directory and returns as an array.

Dan php Sample Script:


<?php
echo getcwd();
?>

Dan php Sample Script 2 

<?php
$dh=opendir("c:/xampp");
while ($file=readdir($dh))
{
echo $file;
echo "<br>";
}
?>


Dan php Sample Script 3



<?php
$dh=opendir("c:/xampp");
while($file=readdir($dh));
{
$text=strchr($file,".");
if($text==".exe");
{
echo $file;
echo "<br>";
}
}
?>


arry: Array is a collection of hetero genius elements. PHP is loosely typed language, that is the reason why we can store any type of elements in a array. In php array is construct that is why we can store the elements using declaration.

Dan Php Sampe Script 2 

<?php
$arr=array(10,100=>'php',30,0=>40);
print_r($arr);
?>

Dan Php Sampe Script 2 


<?php
$arr=array('Ap'=>array("hyd","DEL"),"TN"=>array('chennai','kanchi'));
print_r($arr);
echo $arr['Ap'][0];
?>

foreach: By using this function we can read the all elements of an array without initialization condition and increment.

Dan Php Sampe Script


<?php
$arr=array(10,'a'=>20,30);
foreach($arr as $ke=>$va)
{
echo "key is $ke value is $va";
echo "<br>";
}
?>

count: By using this function we can display the total number of array elements.

array_push: This function is used to add an element at the end of arry and returns the total number of elements.


Dan Php Sampe Script
<?php
$arr=array(10,20,30);
echo array_push($arr,40);
print_r($arr);
?>

array_pop: This function deletes the last elements of an array and returns the value of that element.


Dan Php Sampe Script

<?php
$arr=array(10,20,30);
echo array_pop($arr);
print_r($arr);
?>

array_shift: This function deletes the fist element of an array and retunes the value of the elements.

Dan Php Sampe Script


<?php
$arr=array(10,20,30);
echo array_shift($arr);
print_r($arr);
?>

array_unshift: By using this function we can add the elements at the beginning of an array and it displays the total number of elements.

Dan Php Sampe Script



<?php
$arr=array(10,20,30);
echo array_unshift($arr,5);
print_r($arr);
?>

sort: This. Function erases the array elements in the ascending order with new keys.

Dan Php Sampe Script


<?php
$arr=array(40,'a'=>10,20,30);
sort($arr);
print_r($arr);
?>

rsort: This function arranges the array elements in descending order.

assort: This function arranges the array elements in ascending order with the original keys.

ksort: This function arranges the array elements in ascending order base on the keys.

Dan Php Sampe Script

<?php

$arr=array(1=>40,3=>10,2=>20,0=>30);
ksort($arr);
print_r($arr);
?>

krsort: Based on the key, this function arranges the array elements in descending order.

array_key: It returns the all keys of an array as new array.

array_values: It returns the all values of an array as new array.

Dan Php Sampe Script

<?php
$arr=array('a'=>40,3=>10,2=>20,0=>30);
print_r(array_keys($arr));
?>


explode: This function divides the string as array elements based on the input elements.

Dan Php Sampe Script

<?php
$str="Welcome to Dan PHP Site";
$arr=explode("",$str);
print_r($arr);
?>

implode: By using this function we can join the array elements as string.

Dan Php Sampe Script


<?php
$arr=array("dan","PHP");
echo implode($arr,"/");
?>

extract: This function divides the array elements as a variables. This function will work with
only associative array elements.


Dan Php Sampe Script
<?php
$arr=array('a'=>10,'b'=>20);
extract($arr);
echo $a;
echo $b;
?>

array_chunk: This function splits an array into chunks of array.

Dan Php Sampe Script

<?php
$arr=array(10,20,30,40,50);
print_r(array_chunk($arr,3));
?>



array_combine: It combines the elements of two arrays, 1.it returns the new array keys are the
values of the first array 2.new array values are the values of second array.

Dan Php Sampe Script


<?php
$arr=array('a','b','c');
$arr1=array(10,20,30);
print_r(array_combine($arr,$arr1));
?>

array_count_values : This function returns an array with number of occurrence for each value.

Dan Php Sampe Script


<?php
$arr=array(10,20,30,10);
print_r(arraY_count_values($arr));
?>

array_diff: It compares the two array elements and returns the differences. 

Dan Php Sampe Script

<?php
$arr=array(10,20,30,);
$arr1=array(10,40,20);
print_r(array_diff($arr,$arr1));
?>

array_diff_assoc: This function compares the both array keys and values and returns the
differences.


Dan Php Sampe Script


<?php
$arr=array(10,20,30,);
$arr1=array(10,40,20);
print_r(array_diff_assoc($arr,$arr1));
?>
array_diff_key: Compares the keys of 2 arrays and returns the differences.

Dan Php Sampe Script


<?php
$arr=array(10,'a'=>20,30,);
$arr1=array(10,40,20);
print_r(array_diff_key($arr,$arr1));
?>

array_sum: It returns the sum of all elements of an array.

Dan Php Sampe Script



<?php
$arr=array(10,'a'=>20,30,);
echo array_sum($arr);
?>

array_product: It is a function and it returns the product of all elements of an array.

Dan Php Sampe Script

<?php
$arr=array(10,'a'=>20);
echo array_product($arr);
?>

shuffle : This function shuffles the elements of an array.

Dan Php Sampe Script


<?php
$arr=array(10,'a'=>20,30,'a');
shuffle($arr);
print_r($arr);
?>

array_merge: It merges two or more than two array as new arrays.


Dan Php Sampe Script



<?php
$arr=array(10,'a'=>20,30,);
$arr1=array('a','b','c');
print_r(array_merge($arr,$arr1));
?>

array_unique: This function removes the duplicate elements and returns the unique values of an array.


join: It is same as implode used to join the elements of an array. Returns as string. 


Dan Php Sampe Script



<?php
$arr=array(10,20,30,40);
print_r(array_unique($arr));
?>

array_key_exists: This function checks the specified key existed in the array or not.


Dan Php Sampe Script

<?php
$arr=array(10,20,30,40);
echo array_key_exists(1,$arr);
?>
array_search: This function searches an array for a given value and returns the key.

Dan Php Sampe Script


<?php
$arr=array(10,20,30,40);
echo array_search(20,$arr);
?>

array_reverse: This function displays the elements of an array in reverse direction.

Dan Php Sampe Script


<?php
$arr=array(10,20,30,10);
print_r(array_reverse($arr);
?>



array_slice: This function returns selected parts of an array in reverse direction.


Dan Php Sampe Script



<?php
$arr=array(10,20,30,40);
print_r(array_slice($arr,2));
?>

0 comments:

Post a Comment