260 likes | 410 Views
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 .
E N D
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 • 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.
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.
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).
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");?>
How to create a file ? <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); ?>
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);?>
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)
PHP fread() Function <?php $file = fopen("test.txt","r");fread($file,"10");fclose($file); ?>
PHP fread() Function <?php $file = fopen("test.txt","r"); fread($file,"10"); fclose($file); ?>
PHP fread() Function <?php $file = fopen("test.txt","r"); fread($file,filesize("test.txt")); fclose($file); ?>
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!
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"; ?>
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 ] )
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);?>
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 )
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); ?>
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)
PHP Directory Functions • <?phpmkdir("testing"); // make directory or folder?> • <?phpmkdir("testing/sub"); // make sub directory or sub folder?>
PHP Remove Directory Functions • The rmdir() function removes an empty directory. • This function returns TRUE on success, or FALSE on failure. rmdir(dir,context)
PHP Remove Directory Functions <?php $path = "images"; if(!rmdir($path)) { echo ("Could not remove $path"); }?>