1 / 25

Chapter - 5

Chapter - 5. Handling Files. PHP: Hypertext Preprocessor. Outline. Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character. Overview .

soo
Télécharger la présentation

Chapter - 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter - 5 Handling Files PHP: Hypertext Preprocessor

  2. Outline • Overview • Opening a file • How to create a file ? • Closing a File • Check End-of-file • Reading a File Line by Line • Reading a File Character by Character

  3. Overview • We can after this chapter to understanding of all types of file manipulation in PHP, like create, open, and close a file. After establishing those basics, we will then cover other important file tasks, such as: read, write, append, truncate, and uploading files with PHP.

  4. Overview • When you are manipulating files you must be very careful because you can do a lot of damage if you do something wrong. Common errors include editing the wrong file, filling a hard-drive with garbage data, and accidentally deleting a file's contents.

  5. Opening a File • In PHP, a file is created using a command that is also used to open files. It may seem a little confusing, but we'll try to clarify this conundrum. • In PHP the fopen function is used to open files. However, it can also create a file if it does not find the file specified in the function call. So if you use fopen on a file that does not exist, it will create it, given that you open the file for writing or appending (more on this later).

  6. How to create a file ? • fopen() binds a named resource, specified by filename, to a stream. • Returns a file pointer resource on success, or FALSE on error. • Note: If the fopen() function is unable to open the specified file, it returns 0 (false). • resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] ) <?php $file=fopen("welcome.txt","r");?>

  7. How to create a file ?

  8. How to create a file ? <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); ?>

  9. Closing a File • The file pointed to by handle is closed. • Returns TRUE on success or FALSE on failure. boolfclose ( resource $handle ) <?php $file = fopen("test.txt","r"); //some code to be executedfclose($file);?>

  10. PHP fread() Function • 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. • This function returns the read string, or FALSE on failure. • fread(file,length)

  11. PHP fread() Function <?php $file = fopen("test.txt","r");fread($file,"10");fclose($file); ?>

  12. PHP fread() Function <?php $file = fopen("test.txt","r"); fread($file,"10"); fclose($file); ?>

  13. PHP fread() Function <?php $file = fopen("test.txt","r"); fread($file,filesize("test.txt")); fclose($file); ?>

  14. Check End-of-file • The feof() function checks if the "end-of-file" (EOF) has been reached. • The feof() function is useful for looping through data of unknown length. • Note: You cannot read from files opened in w, a, and x mode!

  15. Check End-of-file • Tests for end-of-file on a file pointer. • ReturnsTRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE. boolfeof ( resource $handle ) <?php $file = “name.txt”; if (feof($file)) echo "End of file"; ?>

  16. Reading a File Line by Line • Gets a line from file pointer. • Returns a string of up to length - 1 bytes read from the file pointed to by handle. If there is no more data to read in the file pointer, thenFALSE is returned. • If an error occurs, FALSE is returned. string fgets ( resource $handle [, int $length ] )

  17. Reading a File Line by Line <?php$file = fopen("welcome.txt", "r") or exit("Unable to open file!");//Output a line of the file until the end is reachedwhile(!feof($file))  {  echo fgets($file). "<br>";  }fclose($file);?>

  18. Reading a File Character by Character • Gets a character from the given file pointer. • Returns a string containing a single character read from the file pointed to by handle. Returns FALSE on EOF. string fgetc ( resource $handle )

  19. Reading a File Character by Character <?php$file=fopen("welcome.txt","r") or exit("Unable to open file!");while (!feof($file))  {  echo fgetc($file);  }fclose($file); ?>

  20. PHP Directory Functions • The directory functions allow you to retrieve information about directories and their contents. • The mkdir() function creates a directory. • This function returns TRUE on success, or FALSE on failure. mkdir(path,mode,recursive,context)

  21. PHP Directory Functions

  22. PHP Directory Functions • <?phpmkdir("testing"); // make directory or folder?> • <?phpmkdir("testing/sub"); // make sub directory or sub folder?>

  23. PHP Remove Directory Functions • The rmdir() function removes an empty directory. • This function returns TRUE on success, or FALSE on failure. rmdir(dir,context)

  24. PHP Remove Directory Functions

  25. PHP Remove Directory Functions <?php $path = "images"; if(!rmdir($path))  {  echo ("Could not remove $path");  }?>

More Related