Download
it420 database management and organization n.
Skip this Video
Loading SlideShow in 5 Seconds..
IT420: Database Management and Organization PowerPoint Presentation
Download Presentation
IT420: Database Management and Organization

IT420: Database Management and Organization

98 Views Download Presentation
Download Presentation

IT420: Database Management and Organization

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. IT420: Database Management and Organization PHP – Using Files 1 March 2006 Adina Crainiceanu www.cs.usna.edu/~adina

  2. Storing and Retrieving Data • Write data to file • Open file • Write data to file • Close the file • Read data from file • Open file • Read from file • Close file

  3. Open File • $fp = fopen(filename,mode); • Test for errors and handle them!

  4. name_age.php Suppress the error! <html> <body> Welcome <?php echo $_POST[‘name’]; ?>.<br /> You are <?php echo $_POST[‘age’]; ?> years old! <br/> <?php @ $fp = fopen(“names.txt”,’w’); if (!$fp){ echo ‘Your name could not be added. ’ . ‘Try again later</body></html>’; exit; } else{ echo ‘Your name will be added to a file.’; } ?> </body> </html> Exit the php script.

  5. Open File • $fp = fopen(“orders.txt”,’w’);

  6. Write into File • int fwrite(filePointer, string [, stringLength]) • int fputs(filePointer, string [, stringLength]) • int file_put_contents(fileName, stringOrArray) • Starting in PHP 5 only! • Example: • $outputstring = $name. “\t”. $age. “\n”; • fwrite($fp, $outputstring);

  7. Close File • int fclose(filePointer) • Example: fclose($fd);

  8. Class Exercise • Create PHP script to: • Open/create a file, without overwriting it • Write the numbers 1 to 20 to file, separated by space • Close the file

  9. Retrieve Data from File • Open file for reading • $fp = fopen(“names.txt”, ‘r’); • Test and handle errors! • Start reading • Stop when end of file reached • feof(filePointer) == true

  10. Read Line from File • string = fgets(filePointer, maxLength) • Read one line, up to maxLength-1 bytes • string = fgetss(filePointer, maxLength [, allowableTags]) • Like fgets, but strip out any PHP or HTML tags found in the string • array = fgetcsv(filePointer, maxLength [, string delimiter]) • Like fgets, but breaks the string into components

  11. Read Data Example <?php @ $fp = fopen(“names.txt”, ‘r’); if (!$fp){ echo ‘No names in file.’; } else{ while( !feof($fd) ){ $name_age = fgets($fp, 500); echo $name_age.’<br>’; } fclose($fp); } ?>

  12. Read Whole File • int readfile(fileName) • Open file • Read content, echo it to the browser • Close file • fpassthru(filePointer) • Read content, echo it to the browser • Close file • array file(fileName) • string file_get_contents(fileName)

  13. Other Reads • char fgetc(filePointer) • string fread(filePointer, nbBytes)

  14. Useful File Functions • boolean file_exists(fileName) • int filesize(fileName) • unlink(fileName) //deletes the file

  15. Locking • What if multiple users try to read/write the file in the same time? • flock(filePointer, lockType) • LOCK_SH // read • LOCK_EX // write • LOCK_UN //release lock

  16. PHP Configuration • Display the errors • Open Sokkit Control Panel • Click on “PHP Config” • Search for “display_errors = Off” • Change to “On” and remove ; at the beginning of line • Search for “error_reporting = E_ALL” • Remove the ; at the beginning of line • Save and return to Main Menu • Restart Web Server • Copy some bad .php file to D:\sokkit\site • Run the file (http://localhost/fileName.php)

  17. Lab Exercise • Save order data from VP-5 Fund Raiser application into a file. Display appropriate message in order confirmation screen. • Display all orders from VP-5 Fund Raiser application. • Display all orders from VP-5 Fund Raiser application with shipping address in Maryland. Use strstr(longString,shortString) to check if ‘MD’ is in shipping address.