File Handling Function

Modes Used In File Handling Function

o   "r" (Read only. Starts at the beginning of the file)
o   "r+" (Read/Write. Starts at the beginning of the file)
o   "w" (Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist)
o   "w+" (Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist)
o   "a" (Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist)
o   "a+" (Read/Write. Preserves file content by writing to the end of the file)
o   "x" (Write only. Creates a new file. Returns FALSE and an error if file already exists)
o   "x+" (Read/Write. Creates a new file. Returns FALSE and an error if file already exists)



1.   fopen : The fopen() function opens a file or URL.
Syntax: fopen(filename,mode)
2.   fread : The fread() reads from an open file. The function will stop at the end of the file or when it reaches the specified length, whichever comes first.
Syntax: fread(file,length)
3.   fread : The fclose() function closes an open file. This function returns TRUE on success or FALSE on failure.
 
Syntax: fclose(file)
<?php
$file = fopen("test.txt","r");
fread($file,"10"); //reads first 10 character
fread($file,filesize("test.txt"));//reads all file
fclose($file);
?
4.   fwrite or fputs: The fwrite() writes to an open file. The function will stop at the end of the file or when it reaches the specified length, whichever comes first.
Syntax: fwrite(file,string)

<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!"); //old content replace with new content
and returns no of characters
fclose($file);
?>
5.    is_readable : The is_readable() function checks whether the specified file is readable.
Syntax: is_readable(file)
<?php
$file = "test.txt";
echo is_radable("test.txt"); //1
?>
6.    is_writable : The is_writable() function checks whether the specified file is writeable.
Syntax: is_writable(file)
<?php
echo is_writable("test.txt"); //1
?>



7.    file_exists : The file_exists() function checks whether or not a file or directory exists.
Syntax: file_exists(path)
<?php
echo file_exists("test.txt");
?>

     8.    fgets : The fgets() function returns a line from an open file. The fgets() function stops returning on a new line, at the specified length, or at EOF, whichever comes first.
Syntax: fgets(file,length)



9.       fgetc : The fgetc() function returns a single character from an open file.

<?php
$file = fopen("test.txt","r"); echo fgets($file);
echo fgetc($file); fclose($file);
?>
 or
<?php
$file = fopen("test2.txt","r"); while (! feof ($file))
echo fgetc($file);     //read all file fclose($file);
?>
 

    10   file: The file() reads a file into an array. Each array element contains a line from the file, with newline still attached.

Syntax: file(path)

<?php print_r(file("test.txt"));

?>
11. file_get_contents : The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. Because it will use memory mapping techniques, if this is supported by the server, to enhance performance.

Syntax: file_get_contents(path)

   <?php

   echo file_get_contents("test.txt");       //read all file

 12.   fputcsv: The fputcsv() function formats a line as CSV and writes it to an open file.

Syntax: fputcsv(file,fields,seperator)
    <?php
        $list = array ( "Peter,Griffin,Oslo,Norway", "Glenn,Quagmire,Oslo,Norway", );
   $file = fopen("contacts.csv","w"); foreach ($list as $line)
{
fputcsv($file,split(',',$line));
}
fclose($file);
?>
Output: Peter,Griffin,Oslo,Norway
              Glenn,Quagmire,Oslo,Norway
13.  file_put_contents : The file_put_contents() writes a string to a file. This function returns the number of character written into the file on success, or FALSE on failure.
Syntax: file_put_contents(file,data)
<?php
echo file_put_contents("test.txt","Hello World. Testing!"); //21
?>
 
 14.      fputs : same as fwrite(alias of fwrite) 
 15.      ftell : The ftell() function returns the current position in an open file.
Syntax: ftell(opened file)
<?php
$file = fopen("test.txt","r"); // print current position
echo ftell($file); //0
fseek($file,"15"); // change current position
echo "<br />".ftell($file);//15
rewind($file);//pointerPosition to start of the file
echo "<br />".ftell($file);//0
?>
16.   fseek : This function moves the file pointer from its current position to a new position, forward or backward, specified by the number of bytes.
Syntax: fseek (opend file,pointer)
17   rewind : set position to the begining of the file
Syntax: rewind(opend file)

18.  copy : The copy() function copies a file.
Syntax: copy(file,to file)
<?php
copy(‘from_file.txt’,’to_file.txt’);
?>
19.  unlink : The unlink() function deletes a file.
Syntax: unlink
<?php
unlink(‘to_file.txt’);
?>
20.   rename: attemps to rename oldname to newname
Syntax: rename(oldname,newname)
<?php
rename(‘from_file.txt’,’new_file.txt’);
?>
21.   move_uploaded_file : The move_uploaded_file() function moves an uploaded file to a new location.
Syntax: move_uploaded_file(file,newloc)
Upload.html

<html>
<head>
<title>Move Uploaded File</title>
</head>
<body>
<form action="upload.php" name="file" method="post" enctype="multipart/form-data"> Upload File : <input type="file" name="filep"/>
<input type="submit" value="Upload File"/>
</form>
</body>
</html>


Upload.php

<?php
$img = $_FILES["filep"]["name"];
$folder = "abc/".$img;
$tmp = $_FILES["filep"]["tmp_name"];
$a = move_uploaded_file($tmp,$folder); if($a)
{
echo "uploaded file $img is moved to $folder...";
}
?>
 





 
 

No comments:

Post a Comment

=>Advanced Java Programming (J2EE)

1.  Syllabus 2. Unit Wise Question/Material 3. Paper 4. Previous Paper