1 / 30

ECA 236

ECA 236. Open Source Server Side Scripting Files & Directories. flat files. 2 ways of storing information flat files databases advantages of flat files no specific knowledge of databases required no extra charge. flat files cont …. disadvantages of flat files

stan
Download Presentation

ECA 236

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. ECA 236 Open Source Server Side Scripting Files & Directories Open Source Server Side Scripting

  2. flat files • 2 ways of storing information • flat files • databases • advantages of flat files • no specific knowledge of databases required • no extra charge Open Source Server Side Scripting

  3. flat files cont … • disadvantages of flat files • slow when working with large files • searching is difficult and slow • problems allowing more than one user simultaneous access • files are processed sequentially • difficult to allow different levels of access Open Source Server Side Scripting

  4. permissions • directory access ( permissions ) may be granted to: • owner: generally has permission to read and write • groups: users can be organized into groups • world: everyone else • types of file permissions • r readable • w writeable • x executable Open Source Server Side Scripting

  5. file processing • to write to a file • open the file – create it if it does not already exist • write data to the file • close the file • to read from a file • open the file – if it cannot be opened, exit gracefully • read data from the file • close the file Open Source Server Side Scripting

  6. opening a file • fopen( ) takes two parameters • the file to be opened • the mode in which to open it • path to file name • create a variable to hold the path to the file • $_SERVER[ 'DOCUMENT_ROOT‘ ] contains the document root directory • $dir_name = dirname($_SERVER["PHP_SELF"]); Open Source Server Side Scripting

  7. opening a file • path to file name • create a variable to hold the path to the file • $_SERVER[ 'DOCUMENT_ROOT‘ ] contains the document root directory • $dir_name = dirname($_SERVER["PHP_SELF"]); $doc_root = $_SERVER["DOCUMENT_ROOT"]; $dir_name = dirname($_SERVER["PHP_SELF"]); $fp = fopen( $doc_root . $dir_name . "/dogs_1.txt", 'w' ) or die ( “Cannot open the file” ); Open Source Server Side Scripting

  8. opening a file cont … • fopen( ) takes 4 parameters the 3rd and 4th are optional • the file to be opened • the mode in which to open it • search option • option to open file in remote location • mode • indicates the purpose for opening the file • read • write • overwrite • append • both Open Source Server Side Scripting

  9. opening a file cont … Open Source Server Side Scripting

  10. opening a file cont … Open Source Server Side Scripting

  11. opening a file cont … • assign the fopen( ) call to a file pointer • use the file pointer to refer to the open file • if the file cannot be opened • the @ operator suppresses the notice • die( ) is called to generate error message and stop execution of script, thus exiting gracefully @ $fp = fopen( $file_name, "w" ) or die ( “Cannot open the file” ); Open Source Server Side Scripting

  12. writing to a file • fwrite( ) takes three parameters, the third is optional • file pointer • string which will be written to the file • maximum number of bytes • if exceeded, PHP truncates the output string fwrite( $fp, $string_output, 1000 ); Open Source Server Side Scripting

  13. writing to a file cont … • separate the sections of data with a delimiter • pipe, comma, tab, etc • if the data will be used in another application, follow the rules of the other application • the delimiter should not be one which may be used in the user’s input • end the string with a record separator (newline ) $string_output = “$name|$gender|$height|$weight|$age\n”; Open Source Server Side Scripting

  14. closing the file • use the fclose( ) function to close the file • fclose( ) takes one parameter, the file pointer fclose( $fp ); Open Source Server Side Scripting

  15. reading from a file • open the file with the fopen( ) function • use the correct mode to write to the file @ $fp = fopen( $file_name, “r" ) or die ( “Cannot open the file” ); if ( filesize( "$doc_root/files/file_name.txt“ ) != 0 ){ while( !feof( $fp ) ) { $line = fgets( $fp, 1000 ); echo $line . “<br />”; } //end while } // end if fclose( $fp ); Open Source Server Side Scripting

  16. reading from a file cont … • if the file opens correctly, read from the file until the end of the file is reached • feof( ) returns TRUE when the end of the file is reached • feof( ) takes one parameter, the file pointer while( !feof( $fp ) ) { Open Source Server Side Scripting

  17. reading from a file cont … • when using feof( )check to see if the filesize is 0 • if the filesize is zero an infinite loop is generated • if there is a problem opening the file an infinite loop is generated • filesize( ) returns the size of the file in bytes • filesize( ) takes one parameter, the path to the file if ( filesize("$doc_root/files/file_name.txt" != 0 ){ Open Source Server Side Scripting

  18. reading from a file cont … • fgets( ) reads from the file one line at a time • fgets( ) takes two parameters, the second is optional • the file pointer • maximum number of bytes to be returned • fgets( ) returns each line as a string $line = fgets( $fp, 1000 ); Open Source Server Side Scripting

  19. reading from a file cont … • fgetss( ) reads from the file one line at a time • fgetss( ) takes three parameters, the third is optional • the file pointer • maximum number of bytes to be returned • allowable tags • fgetss( ) returns each line as a string, with HTML and PHP removed $line = fgetss( $fp, 1000 ); Open Source Server Side Scripting

  20. reading from a file cont … • fgetcsv( ) reads from the file one line at a time • fgetcsv( ) takes four parameters, the third and fourth are optional • the file pointer • maximum number of bytes to be returned • delimiter ( defaults to comma ) • enclosure ( defaults to double quotation marks ) • fgetcsv( ) returns each line as an array, separated on the delimiter $line_array = fgetcsv( $fp, 1000, “|” ); Open Source Server Side Scripting

  21. reading from a file cont … • readfile( ) • opens the file • echoes entire file to browser • closes file • no need to manually open and close file • readfile( ) takes the path to the file as an argument readfile( "$doc_root/files/file_name.txt" ); Open Source Server Side Scripting

  22. reading from a file cont … • file( ) • opens the file • file is returned as an array, each line as an array element ( newline is still attached ) • closes file • no need to manually open and close file • file( ) takes the path to the file as an argument $file_array = file( "$doc_root/files/file_name.txt" ); Open Source Server Side Scripting

  23. reading from a file cont … • file_get_contents( ) • opens the file • file is returned as a string • closes file • no need to manually open and close file • file_get_contents( ) takes the path to the file as an argument $file_string = file_get_contents( "$doc_root/files/file_name.txt" ); Open Source Server Side Scripting

  24. reading from a file cont … • fgetc( ) takes one parameter • file pointer • fgetc( ) returns one character at a time • in addition to string characters, fgetc( ) reads newline and eof characters $char = fgetc( $fp ); Open Source Server Side Scripting

  25. reading from a file cont … • file_exists( ) • checks whether a file or directory exists • returns Boolean TRUE if it exists • FALSE if it does not • file_exists( ) takes the path to the file as an argument file_exists( "$doc_root/files/file_name.txt" ); Open Source Server Side Scripting

  26. reading from a file cont … • unlink( ) • deletes a file • unlink( ) takes the path to the file as an argument unlink( "$doc_root/files/file_name.txt" ); Open Source Server Side Scripting

  27. file locking • flock( ) takes two parameters • file pointer • a constant indicating the kind of lock to acquire Open Source Server Side Scripting

  28. security • to create a file in a directory above the document root • make sure directory exists before writing to it • test it carefully on your server $doc_root = $_SERVER[ 'DOCUMENT_ROOT' ];$file_name ="$doc_root/../files/file_name.txt";$fp = fopen( $file_name, "w+" ); Open Source Server Side Scripting

  29. security cont … • crypt( ) function • one-way algorithm to encrypt a string value • no function to decrypt the string • crypt( ) takes two arguments • string to be encrypted • salt string • salt does not work the same on different systems Open Source Server Side Scripting

  30. security cont … • crypt( ) function • given the same string and the same salt, the same encrypted result is returned each time • we do not need to know the original value of the encrypted string • compare encrypted result to password entered by user after running through the crypt( ) function, with the stored, encrypted password as the salt if( crypt( $user_entered_pw, $enc_pw_stored ) == $enc_pw_stored ) { // code if passwords match} Open Source Server Side Scripting

More Related