Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

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 ]] )

7 How to create a file ? ModesDescription rRead only. Starts at the beginning of the file r+Read/Write. Starts at the beginning of the file wWrite only. Opens and clears the contents of file; or creates a new file if it doesn't exist w+Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist aAppend. Opens and writes to the end of the file or creates a new file if it doesn't exist a+Read/Append. Preserves file content by writing to the end of the file xWrite only. Creates a new file. Returns FALSE and an error if file already exists x+Read/Write. Creates a new file. Returns FALSE and an error if file already exists

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. bool fclose ( resource $handle )

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) ParameterDescription fileRequired. Specifies the open file to read from lengthRequired. Specifies the maximum number of bytes to read

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.  Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE. bool feof ( 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 "; } 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 ParameterDescription pathRequired. Specifies the name of the directory to create modeOptional. Specifies permissions. By default, the mode is 0777 (widest possible access).The mode parameter consists of four numbers: The first number is always zero The second number specifies permissions for the owner The third number specifies permissions for the owner's user group The fourth number specifies permissions for everybody else Possible values (to set multiple permissions, add up the following numbers): 1 = execute permissions 2 = write permissions 4 = read permissions recursiveOptional. Specifies if the recursive mode is set (added in PHP 5) contextOptional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream (added in PHP 5)

22 PHP Directory Functions 

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 ParameterDescription dirRequired. Specifies the directory to be removed contextOptional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream

25 PHP Remove Directory Functions


Download ppt "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."

Similar presentations


Ads by Google