IT420: Database Management and Organization PHP – Using Files 1 March 2006 Adina Crainiceanu www.cs.usna.edu/~adina 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 Open File $fp = fopen(filename,mode); Test for errors and handle them! 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. Open File $fp = fopen(“orders.txt”,’w’); File Modes Description r Read only r+ Read/Write. w Write only. Overwrites. Creates. w+ Read/Write. Overwrites. Creates. a Append. Creates a+ Read/Append. Creates x Create and open for write only. If exists, fail. x+ Create and open for read/write. If exists, fail. 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); Close File int fclose(filePointer) Example: fclose($fd); 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 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 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 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); } ?> 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) Other Reads char fgetc(filePointer) string fread(filePointer, nbBytes) Useful File Functions boolean file_exists(fileName) int filesize(fileName) unlink(fileName) //deletes the file Locking What if multiple users try to read/write the file in the same time? flock(filePointer, lockType) LOCK_SH LOCK_EX LOCK_UN // read // write //release lock 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) 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.