Download presentation
Presentation is loading. Please wait.
1
IS 1181 IS 118 Introduction to Development Tools Week 2
2
IS 1182 Things to Cover Files on Disk File Open File Close Read Write Exist Size Delete Use listings on CD
3
IS 1183 Files on Disk FAT – file attribute table FAT16 FAT32 NTFS Others
4
IS 1184 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
5
IS 1185 Problems opening files @ $fp = fopen(“"$DOCUMENT_ROOT/../orders/orders.txt", 'ab'); if (!$fp) { echo ' Your order could not be processed at this time. '.'Please try again later. '; exit; }
6
IS 1186 File Close Format Fclose($fp) Note the $fp, it was used when opening the file More later
7
IS 1187 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]])
8
IS 1188 fwrite Int fwrite( resource handle, string [, int length]) The length is optional and is the MAXIMUM bytes to be written Ex; fwrite($fp, $outstring, strlen($outstring));
9
IS 1189 Reading a file While (!feof($fp)) { $order – fgets($fp, 999) echo $order.’ ’; } What is going on?
10
IS 11810 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
11
IS 11811 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
12
IS 11812 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
13
IS 11813 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)
14
IS 11814 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’;
15
IS 11815 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
16
IS 11816 Delete a File Unlink( ) Unlink(“………..”); There is no delete command
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.