1 / 16

IS 118 Introduction to Development Tools

IS 118 Introduction to Development Tools. Week 2. Things to Cover. Files on Disk File Open File Close Read Write Exist Size Delete Use listings on CD. Files on Disk . FAT – file attribute table FAT16 FAT32 NTFS Others. File Open. Format

jatin
Download Presentation

IS 118 Introduction to Development Tools

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. IS 118 Introduction to Development Tools Week 2 IS 118

  2. Things to Cover • Files on Disk • File Open • File Close • Read • Write • Exist • Size • Delete • Use listings on CD IS 118

  3. Files on Disk • FAT – file attribute table • FAT16 • FAT32 • NTFS • Others IS 118

  4. File Open • Format • $fp = fopen(“location and name of file”, ‘ab’); • Normally name would be: • $DOCUMENT_ROOT/../orders/orders.txt • Why? • File Modes – page 61 • r, r+, w, w+, x, x+, a, a+, b, t IS 118

  5. Problems opening files • @ $fp = fopen(“"$DOCUMENT_ROOT/../orders/orders.txt", 'ab'); • if (!$fp) • { • echo '<p><strong> Your order could not be processed at this time. ' • .'Please try again later.</strong></p></body></html>'; • exit; • } IS 118

  6. File Close • Format • Fclose($fp) • Note the $fp, it was used when opening the file • More later IS 118

  7. Writing to a file • fwrite($fp, $outstring); • This say write the string $outstring to the file represented by $fp • New to PHP 5 • Int file_put_contents( string filename string data [, int flags [, resource context]]) IS 118

  8. fwrite • Int fwrite( resource handle, string string [, int length]) • The length is optional and is the MAXIMUM bytes to be written • Ex; • fwrite($fp, $outstring, strlen($outstring)); IS 118

  9. Reading a file • While (!feof($fp)) { $order – fgets($fp, 999) echo $order.’<br>’; } • What is going on? IS 118

  10. Stopping • feof($fp) • Looks for the end of file of $fp – returns true when reaches it • So while (!feof($fp)) • Says: while we do not have end of file do something IS 118

  11. Getting data • fgets(), fgetss(), fgetcsv() • $order = fgets($fp, 999) • read one line at a time (till encounter a newline (\n)) • or a maximum of 998 bytes • Fgetss( $fp, length, allowable tags) • Same thing but strips out php and html, leaving in the ones in allowable tags • This is a safe way to read a file created by someone else, strips away code that could be damaging to your system IS 118

  12. Getting Data - 2 • Fgetcsv( $fp, length, delimiter, enclosure) • Breaks up input by delimiter, say a comma • Enclosure is the character each field is surrounded by, “field1”, “field2”, “fieldx” • Commonly used to read csv files created by Excel or some other program IS 118

  13. Reading the whole file • Read the whole file with one command • Readfile(“$DOCUMENT_ROOT/../orders/orders.txt”); • $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab'); fpassthru($fp); • X • $filearray = file("$DOCUMENT_ROOT/../orders/orders.txt“); • Which reads the file into an array (covered in chapter 3) IS 118

  14. Other Useful File Functions • file_exist(“name of file”)) • If (file_exist(“$doc_root”)) echo ‘we have a file’; Else echo ‘Sorry, no file there’; IS 118

  15. Other Useful File Functions - 2 • filesize( ) • $fp = fopen(“…….”, r) echo nl2br(fread($fp, filesize(“……”))); fclose($fp) • This opens a file, • reads it to a function called nl2br • Which converts new lines to html breaks IS 118

  16. Delete a File • Unlink( ) • Unlink(“………..”); • There is no delete command IS 118

More Related