Searching...
Sunday, 7 October 2012

PHP:File Functions In Php

9:31 pm

File Functions In Php

PHP:File Functions In Php:

 Basename():   this function returns the file name from a path.
              Basename(path[,suffix]);
Where path argument specifies the path to check and suffix argument specifies a file extensions. If the file name has this extension , the file extension willnot show.
Copy(sourcefile , target files) : this function copies a file and returns TRUE on success and FALSE on failure.if the destination file already exists , it will be over written.
Dirname(path) : this function returns the directory name from a path.
Disk-free-space(directory)/diskspace(directory):  this function returns the free space in bytes of the soecified directory.
Disk-total-space(directory):  
<?php
$path="c:/xampp/htdocs/india.txt";
$fname= basename($path);
$fname = basename($path,".txt");
echo "file name=".$fname."<br>";
$dname = dirname($path);
echo "directory name=".$dname."<br>";
$res = copy("india.txt","india1.txt");
if($res)
    echo "file copied!!!<br>";
echo "disk free space in c:drive-".  disk_free_space("C:/")."bytes";
OutPut:
?>
file name=india
directory name=c:/xampp/htdocs
file copied!!!
disk free space in c:drive-34333818880bytes
fgetss():
                 this function returns a line with html and php tags removed, from an open file. The  fgetss() function stops returning on a new line , at the specified length,or at EOF which never comes first. This function returns FALSE on Failure.
 fgetss(file resource[,length,tags])
where length argument specifies the member of bytes to read. Default is 1024 bytes and”tags” attribute specifies tags that will not be removed.
<?php
$fp = fopen("india.txt","r") or exit("unabel to open file!!!");
while(!feof($fp))
{
   // $str = fgetss($fp);
    $str = fgetss($fp,1024,"<ins><font>");
    echo $str."<br>";
}
fclose($fp);
?>
File-exists(path):  this function checks whether or not a file or directory exists.
<?php
$a = file_exists("india.txt");
if($a)
    echo "<h1> file exists";
else
    echo "<h1>file not exists";
?>
OutPut:
file exists
file-get-contents(path):  this function reads a file contents into a string.
File-put-contents(filename,data):  this function writes a string to a file. This function returns the number of bytes/characters returns to the file.
Fielatime(filename):  this function returns the last access time of the specified file and it returns the timestamp[in seconds from 1970 jan 1st onwards].
Filectime(filetime) :  this function returns the creation time as time stamp.
Filegroup(filename):  this function returns the groupID of the specified file.
Filesize(filename)
Filetype(filename): thie function returns the file type of a specified file or directory and it returns one of the seven possible values on success or False on failure.
Possible return values:  fifo, char, dir , block, link, file , unknown.
<?php
echo "<h1>";
$str = file_get_contents("india.txt");
echo $str."<br>";
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$n = file_put_contents("alpha.txt", $str);
echo " no.of chars written to file=".$n."<br>";
date_default_timezone_set("Asia/Calcutta");
$c= filectime("india.txt");
$cdate = date("1,ds-M-y::H:i:s",$c);
echo "india.txt is created on - ".$cdate."<br>";
echo "file group -".  filegroup("india.txt")."<br>";
echo "file type -" .  filetype("images")."<br>";
echo "file size = ".  filesize("alpha.txt")."bytes";
?>
OutPut:
no.of chars written to file=26
india.txt is created on - 1,0438-Oct-12::10:33:38
file group -0
file type -dir
file size = 26bytes

php file functions, file functions php, php file handling functions, file functions in php., 

0 comments:

Post a Comment