0
File Handling with PHP
Files and PHP <ul><li>File Handling </li></ul><ul><ul><li>Data Storage </li></ul></ul><ul><ul><ul><li>Though slower than a...
Open/Close a File <ul><li>A file is opened with  fopen () as a “stream”, and PHP returns a ‘handle’ to the file that can b...
File Open Modes ‘ r’ Open for reading only. Start at beginning of file. ‘ r+’ Open for reading and writing. Start at begin...
File Open/Close Example <ul><li><?php </li></ul><ul><li>// open file to read </li></ul><ul><li>$toread =  fopen ( ‘some/fi...
Now what..? <ul><li>If you open a file to read, you can use more in-built PHP functions to read data.. </li></ul><ul><li>I...
Reading Data <ul><li>There are two main functions to read data: </li></ul><ul><li>fgets ($handle,$bytes)   </li></ul><ul><...
Reading Data <ul><li>We need to be aware of the End Of File (EOF) point.. </li></ul><ul><li>feof ($handle)   </li></ul><ul...
Data Reading Example <ul><li>$handle =  fopen ( 'people.txt' ,  'r' ); </li></ul><ul><li>while (! feof ($handle)) { </li><...
Data Reading Example <ul><li>$handle =  fopen ( 'people.txt' ,  'r' ); </li></ul><ul><li>while (! feof ($handle)) { </li><...
Data Reading Example <ul><li>$handle =  fopen ( 'people.txt' ,  'r' ); </li></ul><ul><li>while (! feof ($handle)) { </li><...
Data Reading Example <ul><li>$handle =  fopen ( 'people.txt' ,  'r' ); </li></ul><ul><li>while (! feof ($handle)) { </li><...
File Open shortcuts.. <ul><li>There are two ‘shortcut’ functions that don’t require a file to be opened: </li></ul><ul><li...
Writing Data <ul><li>To write data to a file use: </li></ul><ul><li>fwrite ($handle,$data)   </li></ul><ul><ul><li>Write $...
Data Writing Example <ul><li>$handle =  fopen ( 'people.txt' ,  'a' ); </li></ul><ul><li>fwrite ($handle,  “nFred:Male” );...
Data Writing Example <ul><li>$handle =  fopen ( 'people.txt' ,  'a' ); </li></ul><ul><li>fwrite ($handle,  'nFred:Male' );...
Other File Operations <ul><li>Delete file </li></ul><ul><ul><li>unlink ( 'filename' ); </li></ul></ul><ul><li>Rename (file...
Dealing With Directories <ul><li>Open a directory </li></ul><ul><ul><li>$handle =  opendir ('dirname'); </li></ul></ul><ul...
Directory Example <ul><li>$handle =  opendir ( './' ); </li></ul><ul><li>while ( false  !== ($file= readdir ($handle))) </...
Directory Example <ul><li>$handle =  opendir ( './' ); </li></ul><ul><li>while ( false  !== ($file= readdir ($handle))) </...
Directory Example <ul><li>$handle =  opendir ( './' ); </li></ul><ul><li>while ( false  !== ($file= readdir ($handle))) </...
Directory Example <ul><li>$handle =  opendir ( './' ); </li></ul><ul><li>while ( false  !== ($file= readdir ($handle))) </...
Other Directory Operations <ul><li>Get current directory </li></ul><ul><ul><li>getcwd () </li></ul></ul><ul><li>Change Dir...
Review <ul><li>Can open and close files. </li></ul><ul><li>Can read a file line by line or all at one go. </li></ul><ul><l...
Upcoming SlideShare
Loading in...5
×

Files

340

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
340
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Files"

  1. 1. File Handling with PHP
  2. 2. Files and PHP <ul><li>File Handling </li></ul><ul><ul><li>Data Storage </li></ul></ul><ul><ul><ul><li>Though slower than a database </li></ul></ul></ul><ul><ul><li>Manipulating uploaded files </li></ul></ul><ul><ul><ul><li>From forms </li></ul></ul></ul><ul><ul><li>Creating Files for download </li></ul></ul>
  3. 3. Open/Close a File <ul><li>A file is opened with fopen () as a “stream”, and PHP returns a ‘handle’ to the file that can be used to reference the open file in other functions. </li></ul><ul><li>Each file is opened in a particular mode . </li></ul><ul><li>A file is closed with fclose () or when your script ends. </li></ul>
  4. 4. File Open Modes ‘ r’ Open for reading only. Start at beginning of file. ‘ r+’ Open for reading and writing. Start at beginning of file. ‘ w’ Open for writing only. Remove all previous content, if file doesn’t exist, create it. ‘ a’ Open writing, but start at END of current content. ‘ a+’ Open for reading and writing, start at END and create file if necessary.
  5. 5. File Open/Close Example <ul><li><?php </li></ul><ul><li>// open file to read </li></ul><ul><li>$toread = fopen ( ‘some/file.ext’ , ’r’ ); </li></ul><ul><li>// open (possibly new) file to write </li></ul><ul><li>$towrite = fopen ( ‘some/file.ext’ , ’w’ ); </li></ul><ul><li>// close both files </li></ul><ul><li>fclose ($toread); </li></ul><ul><li>fclose ($towrite); </li></ul><ul><li>?> </li></ul>
  6. 6. Now what..? <ul><li>If you open a file to read, you can use more in-built PHP functions to read data.. </li></ul><ul><li>If you open the file to write, you can use more in-built PHP functions to write.. </li></ul>
  7. 7. Reading Data <ul><li>There are two main functions to read data: </li></ul><ul><li>fgets ($handle,$bytes) </li></ul><ul><ul><li>Reads up to $bytes of data, stops at newline or end of file (EOF) </li></ul></ul><ul><li>fread ($handle,$bytes) </li></ul><ul><ul><li>Reads up to $bytes of data, stops at EOF. </li></ul></ul>
  8. 8. Reading Data <ul><li>We need to be aware of the End Of File (EOF) point.. </li></ul><ul><li>feof ($handle) </li></ul><ul><ul><li>Whether the file has reached the EOF point. Returns true if have reached EOF. </li></ul></ul>
  9. 9. Data Reading Example <ul><li>$handle = fopen ( 'people.txt' , 'r' ); </li></ul><ul><li>while (! feof ($handle)) { </li></ul><ul><li>echo fgets ($handle, 1024 ); </li></ul><ul><li>echo '<br />' ; </li></ul><ul><li>} </li></ul><ul><li>fclose ($handle); </li></ul>
  10. 10. Data Reading Example <ul><li>$handle = fopen ( 'people.txt' , 'r' ); </li></ul><ul><li>while (! feof ($handle)) { </li></ul><ul><li>echo fgets ($handle, 1024 ); </li></ul><ul><li>echo '<br />' ; </li></ul><ul><li>} </li></ul><ul><li>fclose ($handle); </li></ul>Open the file and assign the resource to $handle $handle = fopen ( 'people.txt' , 'r' );
  11. 11. Data Reading Example <ul><li>$handle = fopen ( 'people.txt' , 'r' ); </li></ul><ul><li>while (! feof ($handle)) { </li></ul><ul><li>echo fgets ($handle, 1024 ); </li></ul><ul><li>echo '<br />' ; </li></ul><ul><li>} </li></ul><ul><li>fclose ($handle); </li></ul>While NOT at the end of the file, pointed to by $handle, get and echo the data line by line while (! feof ($handle)) { echo fgets ($handle, 1024 ); echo '<br />' ; }
  12. 12. Data Reading Example <ul><li>$handle = fopen ( 'people.txt' , 'r' ); </li></ul><ul><li>while (! feof ($handle)) { </li></ul><ul><li>echo fgets ($handle, 1024 ); </li></ul><ul><li>echo '<br />' ; </li></ul><ul><li>} </li></ul><ul><li>fclose ($handle); </li></ul>Close the file fclose ($handle);
  13. 13. File Open shortcuts.. <ul><li>There are two ‘shortcut’ functions that don’t require a file to be opened: </li></ul><ul><li>$lines = file ($filename) </li></ul><ul><ul><li>Reads entire file into an array with each line a separate entry in the array. </li></ul></ul><ul><li>$str = file_get_contents ($filename) </li></ul><ul><ul><li>Reads entire file into a single string. </li></ul></ul>
  14. 14. Writing Data <ul><li>To write data to a file use: </li></ul><ul><li>fwrite ($handle,$data) </li></ul><ul><ul><li>Write $data to the file. </li></ul></ul>
  15. 15. Data Writing Example <ul><li>$handle = fopen ( 'people.txt' , 'a' ); </li></ul><ul><li>fwrite ($handle, “nFred:Male” ); </li></ul><ul><li>fclose ($handle); </li></ul>
  16. 16. Data Writing Example <ul><li>$handle = fopen ( 'people.txt' , 'a' ); </li></ul><ul><li>fwrite ($handle, 'nFred:Male' ); </li></ul><ul><li>fclose ($handle); </li></ul>$handle = fopen ( 'people.txt' , 'a' ); Open file to append data (mode 'a') fwrite ($handle, “nFred:Male” ); Write new data (with line break after previous data)
  17. 17. Other File Operations <ul><li>Delete file </li></ul><ul><ul><li>unlink ( 'filename' ); </li></ul></ul><ul><li>Rename (file or directory) </li></ul><ul><ul><li>rename ( 'old name' , 'new name' ); </li></ul></ul><ul><li>Copy file </li></ul><ul><ul><li>copy ( 'source' , 'destination' ); </li></ul></ul><ul><li>And many, many more! </li></ul><ul><ul><li>www.php.net/manual/en/ref.filesystem.php </li></ul></ul>
  18. 18. Dealing With Directories <ul><li>Open a directory </li></ul><ul><ul><li>$handle = opendir ('dirname'); </li></ul></ul><ul><ul><ul><li>$handle 'points' to the directory </li></ul></ul></ul><ul><li>Read contents of directory </li></ul><ul><ul><li>readdir ($handle) </li></ul></ul><ul><ul><ul><li>Returns name of next file in directory </li></ul></ul></ul><ul><ul><ul><li>Files are sorted as on filesystem </li></ul></ul></ul><ul><li>Close a directory </li></ul><ul><ul><li>closedir ($handle) </li></ul></ul><ul><ul><ul><li>Closes directory 'stream' </li></ul></ul></ul>
  19. 19. Directory Example <ul><li>$handle = opendir ( './' ); </li></ul><ul><li>while ( false !== ($file= readdir ($handle))) </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;$file<br />&quot; ; </li></ul><ul><li>} </li></ul><ul><li>closedir ($handle); </li></ul>
  20. 20. Directory Example <ul><li>$handle = opendir ( './' ); </li></ul><ul><li>while ( false !== ($file= readdir ($handle))) </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;$file<br />&quot; ; </li></ul><ul><li>} </li></ul><ul><li>closedir ($handle); </li></ul>Open current directory $handle = opendir ( './' );
  21. 21. Directory Example <ul><li>$handle = opendir ( './' ); </li></ul><ul><li>while ( false !== ($file= readdir ($handle))) </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;$file<br />&quot; ; </li></ul><ul><li>} </li></ul><ul><li>closedir ($handle); </li></ul>Whilst readdir() returns a name, loop through directory contents, echoing results while ( false !== ($file= readdir ($handle))) { echo &quot;$file<br />&quot; ; }
  22. 22. Directory Example <ul><li>$handle = opendir ( './' ); </li></ul><ul><li>while ( false !== ($file= readdir ($handle))) </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;$file<br />&quot; ; </li></ul><ul><li>} </li></ul><ul><li>closedir ($handle); </li></ul>Close the directory stream closedir ($handle);
  23. 23. Other Directory Operations <ul><li>Get current directory </li></ul><ul><ul><li>getcwd () </li></ul></ul><ul><li>Change Directory </li></ul><ul><ul><li>chdir ( 'dirname' ); </li></ul></ul><ul><li>Create directory </li></ul><ul><ul><li>mkdir ( 'dirname' ); </li></ul></ul><ul><li>Delete directory (MUST be empty) </li></ul><ul><ul><li>rmdir ( 'dirname' ); </li></ul></ul><ul><li>And more! </li></ul><ul><ul><li>www.php.net/manual/en/ref.dir.php </li></ul></ul>
  24. 24. Review <ul><li>Can open and close files. </li></ul><ul><li>Can read a file line by line or all at one go. </li></ul><ul><li>Can write to files. </li></ul><ul><li>Can open and cycle through the files in a directory. </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×