Loading...
PHP

File Ending Concepts Part 2

fseek: By using this function we can locate the file pointer on specified location
<?php
$fp= fopen(“myfile.txt”, “w+);
fwrite($fp, “Scott”);
fseek($fp, 3);
echo fread($fp, file size(“myfile.txt”));
?>

fgetc: To get a character from a file.
<?php
$fp= fopen(“myfile.txt”, “r”);
echo fgetc($fp);
echo fgetc($fp);
?>

fgets: We can read a line from a string and it locates the file pointer, at the beginning of next line.
<?php
$fp=fopen(“myfile.txt”, “r”);
$count=0;
while($line=fgets($fp)){
echo “<br> Total lines are $count”;
}
?>

fgetss: It is same as fgets but it ignores on Html elements.

File_get_contents: By using this function we can get contents of a file without file pointer.
<?php
echo file_get_contents(“Sunset.jpg”);
?>

file_put_contents: To write new contents in file without file pointer.
<?php
echo file_put_contents(“myfile.txt”, “welcome”);
?>

Read file: By using this function we can read the contents of a file and we can write those contents in current buffer location.
<?php
read file(“myfile.txt”);
?>

file: By using this function we can read each and every line of a file and we can get those lines has array elements.
<?php
#arr= file(“myfile..txt”);
print_r($arr);
?>

filectime: To get clear data and time information of a file as time stamp value.
<?php
echo date(‘d/m/t’,filectime(“myfile.txt”));
?>

fileatime: To get last access data and time information of a file.

filemtime: To get last notify date and time information of a file.

copy: To copy a file any location to another location.
<?php
copy(“myfile.txt”, “c:/abcdef.txt”);
?>

rename: To rename a file with another name.
<?php
rename(“myfile.txt”, “newname.txt”);
?>

unlink: To delete a file permanently from hard disc.
<?php
unlink(“c:/abcdef.txt”);
?>

Example:
<form method= “post” action= ” “>
<input type= “text” name= “
<input type=”submit” value=”create” name=”sub”>
<input type= “submit” value= “rename” name =”sub”>
<input type= “submit” value=”Delete” name= “sub”>
<input type= “submit” value= “append” name= “sub”>
<text area name=”t2>
<?php
if(isset($_POST[‘sub’])
{
if($_POST[‘sub’]==”Read”)
echo file_get_contents($t1);
}
</text area>
</form>
<?php
if(isset($_POST[‘sub’])
{
$t1= $_POST[‘t1’]);
$t2= $_POST[‘t2’];
$sub=$_POST[‘sub’];
if($sub==”create”)
fopen($t1, “w”);
else if($sub== “Delete”);
unlike($t1);
else if($sub= “Append”);
$fp= fopen($t1, “a”);
fwrite($fp, $t2);
}
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *