Php i basic chapter 4

803 views

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
803
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php i basic chapter 4

  1. 1. PHP Workshop 1File Handling with PHP
  2. 2. PHP Workshop 2Files and PHP• File Handling– Data Storage• Though slower than a database– Manipulating uploaded files• From forms– Creating Files for download
  3. 3. PHP Workshop 3Open/Close a File• A file is opened with fopen() as a “stream”,and PHP returns a ‘handle’ to the file thatcan be used to reference the open file inother functions.• Each file is opened in a particular mode.• A file is closed with fclose() or when yourscript ends.
  4. 4. PHP Workshop 4File Open Modes‘r’ Open for reading only. Start at beginning offile.‘r+’ Open for reading and writing. Start atbeginning of file.‘w’ Open for writing only. Remove all previouscontent, if file doesn’t exist, create it.‘a’ Open writing, but start at END of currentcontent.‘a+’ Open for reading and writing, start at ENDand create file if necessary.
  5. 5. PHP Workshop 5File Open/Close Example<?php// open file to read$toread = fopen(‘some/file.ext’,’r’);// open (possibly new) file to write$towrite = fopen(‘some/file.ext’,’w’);// close both filesfclose($toread);fclose($towrite);?>
  6. 6. PHP Workshop 6Now what..?• If you open a file to read, you can usemore in-built PHP functions to read data..• If you open the file to write, you can usemore in-built PHP functions to write..
  7. 7. PHP Workshop 7Reading Data• There are two main functions to read data:• fgets($handle,$bytes)– Reads up to $bytes of data, stops at newlineor end of file (EOF)• fread($handle,$bytes)– Reads up to $bytes of data, stops at EOF.
  8. 8. PHP Workshop 8Reading Data• We need to be aware of the End Of File(EOF) point..• feof($handle)– Whether the file has reached the EOF point.Returns true if have reached EOF.
  9. 9. PHP Workshop 9Data Reading Example$handle = fopen(people.txt, r);while (!feof($handle)) {echo fgets($handle, 1024);echo <br />;}fclose($handle);
  10. 10. PHP Workshop 10Data Reading Example$handle = fopen(people.txt, r);while (!feof($handle)) {echo fgets($handle, 1024);echo <br />;}fclose($handle);Open the file and assign the resource to $handle$handle = fopen(people.txt, r);
  11. 11. PHP Workshop 11Data Reading Example$handle = fopen(people.txt, r);while (!feof($handle)) {echo fgets($handle, 1024);echo <br />;}fclose($handle);While NOT at the end of the file, pointedto by $handle,get and echo the data line by linewhile (!feof($handle)) {echo fgets($handle, 1024);echo <br />;}
  12. 12. PHP Workshop 12Data Reading Example$handle = fopen(people.txt, r);while (!feof($handle)) {echo fgets($handle, 1024);echo <br />;}fclose($handle);Close the filefclose($handle);
  13. 13. PHP Workshop 13File Open shortcuts..• There are two ‘shortcut’ functions that don’trequire a file to be opened:• $lines = file($filename)– Reads entire file into an array with each line aseparate entry in the array.• $str = file_get_contents($filename)– Reads entire file into a single string.
  14. 14. PHP Workshop 14Writing Data• To write data to a file use:• fwrite($handle,$data)– Write $data to the file.
  15. 15. PHP Workshop 15Data Writing Example$handle = fopen(people.txt, a);fwrite($handle, “nFred:Male”);fclose($handle);
  16. 16. PHP Workshop 16Data Writing Example$handle = fopen(people.txt, a);fwrite($handle, nFred:Male);fclose($handle);$handle = fopen(people.txt, a);Open file to append data (mode a)fwrite($handle, “nFred:Male”);Write new data (with linebreak after previous data)
  17. 17. PHP Workshop 17Other 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
  18. 18. PHP Workshop 18Dealing 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
  19. 19. PHP Workshop 19Directory Example$handle = opendir(./);while(false !== ($file=readdir($handle))){echo "$file<br />";}closedir($handle);
  20. 20. PHP Workshop 20Directory Example$handle = opendir(./);while(false !== ($file=readdir($handle))){echo "$file<br />";}closedir($handle);Open current directory$handle = opendir(./);
  21. 21. PHP Workshop 21Directory Example$handle = opendir(./);while(false !== ($file=readdir($handle))){echo "$file<br />";}closedir($handle);Whilst readdir() returns a name, loopthrough directory contents, echoingresultswhile(false !== ($file=readdir($handle))){echo "$file<br />";}
  22. 22. PHP Workshop 22Directory Example$handle = opendir(./);while(false !== ($file=readdir($handle))){echo "$file<br />";}closedir($handle);Close the directory streamclosedir($handle);
  23. 23. PHP Workshop 23Other Directory Operations• Get current directory– getcwd()• Change Directory– chdir(dirname);• Create directory– mkdir(dirname);• Delete directory (MUST be empty)– rmdir(dirname);• And more!– www.php.net/manual/en/ref.dir.php
  24. 24. PHP Workshop 24Review• Can open and close files.• Can read a file line by line or all at one go.• Can write to files.• Can open and cycle through the files in adirectory.
  25. 25. PHP Workshop 25Review all• Create a File$my_file = file.txt;$handle = fopen($my_file, w) ordie(Cannot open file: .$my_file);//implicitly creates file
  26. 26. PHP Workshop 26Review all• Open a File$my_file = file.txt;$handle = fopen($my_file, w) ordie(Cannot open file: .$my_file); //openfile for writing (w,r,a)...
  27. 27. PHP Workshop 27Review all• Read a File$my_file = file.txt;$handle = fopen($my_file, r);$data = fread($handle,filesize($my_file));
  28. 28. PHP Workshop 28Review all• Write to a File$my_file = file.txt;$handle = fopen($my_file, w) ordie(Cannot open file: .$my_file);$data = This is the data;fwrite($handle, $data);
  29. 29. PHP Workshop 29Review all• Append to a File$my_file = file.txt;$handle = fopen($my_file, a) or die(Cannotopen file: .$my_file);$data = New data line 1;fwrite($handle, $data);$new_data = "n".New data line 2;fwrite($handle, $new_data);
  30. 30. PHP Workshop 30Review all• Close a File$my_file = file.txt;$handle = fopen($my_file, w) ordie(Cannot open file: .$my_file);//write some data herefclose($handle);
  31. 31. PHP Workshop 31Review all• Delete a File$my_file = file.txt;unlink($my_file);
  32. 32. PHP Workshop 32Review all
  33. 33. PHP Workshop 33Review all

×