Download presentation
Presentation is loading. Please wait.
Published byRalf Summers Modified over 9 years ago
1
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories
2
Open Source Server Side Scripting 2 ECA 236 flat files 2 ways of storing information flat files databases advantages of flat files no specific knowledge of databases required no extra charge
3
Open Source Server Side Scripting 3 ECA 236 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
4
Open Source Server Side Scripting 4 ECA 236 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
5
Open Source Server Side Scripting 5 ECA 236 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
6
Open Source Server Side Scripting 6 ECA 236 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"]);
7
Open Source Server Side Scripting 7 ECA 236 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” );
8
Open Source Server Side Scripting 8 ECA 236 opening a file cont … fopen( ) takes 4 parameters the 3 rd and 4 th 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
9
Open Source Server Side Scripting 9 ECA 236 opening a file cont … Mode Description r Read Mode: Open for reading only. Place the file pointer at the beginning of the file. r+ Read mode: Open for reading and writing. Place the file pointer at the beginning of the file. w Write Mode: Open for writing only. Place the file pointer at the beginning of the file. If the file already exists, delete the existing data. If the file does not exist, attempt to create it. w+ Write Mode: Open for writing and reading. Place the file pointer at the beginning of the file. If the file already exists, delete the existing data. If the file does not exist, attempt to create it. a Append mode: Open the file for appending only. Start from the end of the existing data. If the file does not exist, attempt to create it. a+ Append mode: Open the file for appending and reading. Start from the end of the existing data. If the file does not exist, attempt to create it.
10
Open Source Server Side Scripting 10 ECA 236 opening a file cont … Mode Description x Cautious Write Mode: Open the file for writing, beginning from the start of the file. If the file already exists, it will not be opened, fopen( ) will return false, and PHP will generate an error. x+ Cautious Write Mode: Open the file for writing and reading, beginning from the start of the file. If the file already exists, it will not be opened, fopen( ) will return false, and PHP will generate an error. b Binary Mode: Used in conjunction with one of the other modes. Use this mode if the Operating System differentiates between binary and text files. Windows OS differentiates, Unix OS does not. PHP developers recommend the use of this option for maximum portability. This is the default mode. t Text Mode: Used in conjunction with one of the other modes. This mode is an option only with Windows OS. It is not recommended except before porting the code with the b option.
11
Open Source Server Side Scripting 11 ECA 236 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” );
12
Open Source Server Side Scripting 12 ECA 236 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 );
13
Open Source Server Side Scripting 13 ECA 236 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”;
14
Open Source Server Side Scripting 14 ECA 236 closing the file use the fclose( ) function to close the file fclose( ) takes one parameter, the file pointer fclose( $fp );
15
Open Source Server Side Scripting 15 ECA 236 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. “ ”; } //end while } // end if fclose( $fp );
16
Open Source Server Side Scripting 16 ECA 236 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 ) ) {
17
Open Source Server Side Scripting 17 ECA 236 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 ){
18
Open Source Server Side Scripting 18 ECA 236 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 );
19
Open Source Server Side Scripting 19 ECA 236 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 );
20
Open Source Server Side Scripting 20 ECA 236 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, “|” );
21
Open Source Server Side Scripting 21 ECA 236 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" );
22
Open Source Server Side Scripting 22 ECA 236 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" );
23
Open Source Server Side Scripting 23 ECA 236 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" );
24
Open Source Server Side Scripting 24 ECA 236 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 );
25
Open Source Server Side Scripting 25 ECA 236 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" );
26
Open Source Server Side Scripting 26 ECA 236 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" );
27
Open Source Server Side Scripting 27 ECA 236 file locking flock( ) takes two parameters file pointer a constant indicating the kind of lock to acquire Constant Description LOCK_SH Reading Lock: the file can be shared with other readers. LOCK_EX Writing Lock: the file cannot be shared. The lock is exclusive. LOCK_UN Unlock: release the existing lock.
28
Open Source Server Side Scripting 28 ECA 236 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+" );
29
Open Source Server Side Scripting 29 ECA 236 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
30
Open Source Server Side Scripting 30 ECA 236 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 }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.