160 likes | 252 Views
Explore essential file operations like File Open, Write, Close, Read, and Delete in this comprehensive guide. Understand file structures and efficient handling techniques.
E N D
IS 118 Introduction to Development Tools Week 2 IS 118
Things to Cover • Files on Disk • File Open • File Close • Read • Write • Exist • Size • Delete • Use listings on CD IS 118
Files on Disk • FAT – file attribute table • FAT16 • FAT32 • NTFS • Others IS 118
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
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
File Close • Format • Fclose($fp) • Note the $fp, it was used when opening the file • More later IS 118
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
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
Reading a file • While (!feof($fp)) { $order – fgets($fp, 999) echo $order.’<br>’; } • What is going on? IS 118
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
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
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
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
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
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
Delete a File • Unlink( ) • Unlink(“………..”); • There is no delete command IS 118