1 / 19

File Handling with PHP

File Handling with PHP. Open/Close a File. A file is opened with fopen () as a “stream” PHP returns a ‘handle’ to the file Used to reference the open file Each file is opened in a particular mode A file is closed with fclose () or when your script ends. File Open Modes.

keola
Download Presentation

File Handling with PHP

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. File Handling with PHP

  2. Open/Close a File • A file is opened with fopen() as a “stream” • PHP returns a ‘handle’ to the file • Used to reference the open file • Each file is opened in a particular mode • A file is closed with fclose() or when your script ends

  3. File Open Modes

  4. File Open/Close Example <?php // open and close file to read $toread = fopen(‘some/file.txt’,’r’); fclose($toread); // open and close file to write $towrite = fopen(‘some/file.txt’,’w’); fclose($towrite); ?>

  5. Writing Data • To write data to a file use • fwrite($handle,$data) EXAMPLE: $handle = fopen('people.txt', 'a'); fwrite($handle, ‘\nFred:Male'); fclose($handle);

  6. But first… • Create the file on the web server • Set permissions to write

  7. <?php /* Write to a file http://ned.highline.edu/~tostrander/215/file_io/write.php */ // open file to write $file = fopen('poem.txt', 'w'); $text = "Mama said I'd lose my head if it wasn't fastened on. Today I guess it wasn't 'cause while playing with my cousin it fell off and rolled away and now it's gone. ~ Shel Silverstein"; // write to the file fwrite($file, $text); echo "File created successfully."; // close file fclose($file); ?>

  8. Reading Data • There are two main functions to read data: • fgets($handle) • Reads a line of data at a time • fread($handle,$bytes) • Reads up to $bytes of data, stops at EOF (end of file) • Used to read entire file at once

  9. fgets() • feof($handle) returns true if we have reached end of file $fileName = "poem.txt"; echo "<h3>Method 1: fgets()</h3>"; // open file to read $file = fopen($fileName, 'r'); // read file contents one line at a time while (!feof($file)) { echo nl2br(fgets($file)); } fclose($file); • nl2br($string) converts new line characters into <br /> tags

  10. Delimiters echo "<h3>Method 1: fgets() – Delimiters</h3>"; $fileName = "students.txt"; $file = fopen($fileName, 'r'); while (!feof($file)) { $line = fgets($file); $array = explode("^", $line); $sid = $array[0]; $first = $array[1]; $last = $array[2]; echo "$sid: $first $last<br />"; } fclose($file); students.txt 1^Cheryl^McCalop 2^Laura^Lavell 3^Thu^Trinh

  11. fread() $fileName = "poem.txt"; echo "<h3>Method 2: fread()</h3>"; // open file to read $file = fopen($fileName, 'r'); // read file contents all at once $contents = fread($file, 1024); echo nl2br($contents); fclose($file);

  12. File Open shortcuts • There are two ‘shortcut’ functions that don’t require a file to be opened • $lines = file($filename) • Reads entire file into an array with each line a separate entry in the array. • $str = file_get_contents($filename) • Reads entire file into a single string.

  13. file() $fileName = "poem.txt"; echo "<h3>Method 3: file()</h3>"; // read entire file into an array $lines = file($fileName); foreach($lines as $line) echo nl2br($line);

  14. file_get_contents() $fileName = "poem.txt"; echo "<h3>Method 4: file_get_contents()</h3>"; // read entire file into a string $fileString = file_get_contents($fileName); echo nl2br($fileString);

  15. Other File Operations • Delete file • unlink('filename'); • Rename (file or directory) • rename('old name', 'new name'); • Copy file • copy('source', 'destination'); • And many, many more • www.php.net/manual/en/ref.filesystem.php

  16. Dealing With Directories • Open a directory • $handle = opendir('dirname'); • $handle 'points' to the directory • Read contents of directory • readdir($handle) • Returns name of next file in directory • Files are sorted as on filesystem • Close a directory • closedir($handle) • Closes directory 'stream'

  17. Directory Example $handle = opendir('.'); while ($fileName=readdir($handle)) { echo"$fileName<br />"; } closedir($handle);

  18. Other Directory Operations • Get current directory • getcwd() • Change Directory • chdir('dirname'); • Create directory • mkdir('dirname'); • Delete directory (MUST be empty) • rmdir('dirname'); • Change directory permissions • chmod('dirname', value); • And more... • www.php.net/manual/en/ref.dir.php

  19. Try It Write a script that: • Creates a subdirectory called “sub” in file IO • Creates a file in “sub,” and write some text to the file • Renames the file • Renames the directory • chmod's the file and the directory to 772

More Related