Saturday, 6 August 2011

Files

                                                
                                                       FILES
------------------------------------------------------------------------------------------


1) Existing files are opened (or)created by using    fopen()  function.It accepts 2 arguments.
One is file path (what ever the file you want to open ) and second one is mode of the file.
2) How many type of modes is there in files:-
Ans)     8 type of modes are there
            r    ->read only mode(pointer is started from the starting of the file)
            r+ ->read and write mode(pointer is started from the srarting of the file)
w ->write mode
w+->read and write mode
a  ->write only access
a+ ->read and write access
x  ->create and open for write only
x+ ->create and open write and read only.

3)how to close the file
Ans)     using fclose() function .
Ex:-
<?php
$filehandler=fopen(“path of the file”,w+) or die(‘canot open the file’);
Fclose($filehandler);
?>

4)how can u write the file using php?
Ans)     <?php
$filehandler=fopen(“path of the file”,w+) or die(‘canot open the file’);
$result=fwrite(‘$filehandler’,”this was written by apparao”);
If($result)  {
               Echo “successfully written”;
                 }
Else
{
echo “failed to written”;
}
Fclose($filehandler);
?>
5)how can u read the file contents?
Ans)     by using fread() function.
<?php
$filehandler=fopen(“path of the file”,w+) or die(‘canot open the file’);
$read=fread(“path of the file”,1024);
echo “$read”;
fclose($filehandler);
?>
Note:-1024 means how many bytes u want to read from the file.
See 151 page it is very imp program.(sir globe material)

*Php out put Buffering:-
1 Out put buffering is a mechanism .content directly send to the output stream.
2 Output buffering is initiating by using ob_start() function.it takes three optional p          arameters.
3 to stop the buffering by using ob_end_flush() function.//flush the out put from the b      uffer
4 to delete the all buffer content with our flushing by using ob_clean() function.
**Example:-
<?php
ob_start();
Echo “hai friends this is for  output buffer testing”;
ob_end_flush();
?>
File permissions:-
if u donot have the  file permissions,the others are also directly use the file.Before starting the file permission we should know that file is readable (or)the file is writeble.
We can know the above from this 2 functions.            Is_readable($filename);                   
                                                                                     Is_writable($filename);
**How can u lock the file by using php?
Ans)     using flock function.it takes 2 arguments
One is filename and another is global variable i.e LOCK_EX
Unlock the file :- this is also do byusing the flock function .this also have take 2 arguments
One for file and another one for global variable for LOCK_UN
<?php
            $fp = fopen("/tmp/lock.txt", "w+");
            if (flock($fp, LOCK_EX)) { // do an exclusive lock
   fwrite($fp, "Write something here\n");
   flock($fp, LOCK_UN); // release the lock
} else  {
               echo "Couldn't lock the file !";           
}
fclose($fp);
?>

**How can u know the path info and file extension by using php script?:-
 <?php
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";//it will return the directory name
echo $path_parts['basename'], "\n";//it will return the file name
echo $path_parts['extension'], "\n";//it will return the extension of //the file
?>

What is the output  of this: program:---
<?php
$a=0110;
echo $a;
?>
Ans)72
Explanation:- the above number is octal number
0*8power0+1*8power1+1*8power2+0*8power3=72(fine)
           
Some more functions on file systems

1->basename($path):- it will return only file name not the directory name

2->chmod(“…./…/file1.txt”,0755)

3->copy($file,$newfile):-copu to destination.return on success or false in failure.

4->unlink()->delete the whole file,and it contents also

5->unset()->it will remove only data of a file.

6-> disk_free_space(“c:”):-it will  return the total free space of a  particular drive.
                        (Or)     diskfreespace(“c:”)

7->disk-total_space(“c:”)->it will return the total size of the drive.

8->feof($file)->end of the file

9->fflush()->forcely write all buffered output to the resource.pointed to by the resource   
                      pointed to by the file handle.
                        True->success
                        False->fail.

10->fgetc()->read the  string character wise.
11->fgets()->read the file string wise.
12->file_exists($filename)
            {
            Echo “file is exists”;
            }

*13->file_get_contents()->read the entire file content string by string.

*14->file_put_contents()->
15->filesize(“filename”)->it will returns the size of the file in bytes.

16->filetype()->it will return the type of the file.

17->var_dumpo(is_file(‘a.txt’));
            If it is a file it will return bool(true)
            Other wise it will return false.

18->is_upload_file()->return true if the file name was uploaded via http post.

19->mkdir(“…./dir1”,0700);
Return true on success ot false on failure

*20 move_uploaded_file():-

21->rename(“-----/1.txt”,”……./2.txt)
            Old name is 1.txt it is changed into 2.txt

22->rewind()read once again .means pointer is starts from start of the file.

23->rmdir(“…”):-remove the directory

24->touch():-create a  file

25->tempnam():-create a log file.

26->is_readable():-it will return whether the file readable file or not.
Ex:-$f1-“test.txt”;

If(is_readble($f1))
{
echo “file is readable”);
}
Else
{
echo “not readable”;
}
           
27->is_writable():-file is writable or not
Ex:-
                        If(is_writable($filename))
{
                 echo “writable file”;
}         

28->filesize($file):-
$f=’hellow.txt’;
echo  filesize($hellow.txt);







No comments:

Post a Comment

Visual comparison of the two methods, creating a simple table.

Option 1, using PHP: // PHP $html = '<table>' ;     foreach ( $data as $row ) {     $html .= '<tr>...