Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

30
Open Source Server Side Scrip ting ECA 236 Open Source Server Side Scripting Files & Directories

Transcript of Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Page 1: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting

ECA 236

Open Source Server Side ScriptingFiles & Directories

Page 2: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 2ECA 236

flat filesflat files

2 ways of storing informationflat filesdatabases

advantages of flat filesno specific knowledge of databases requiredno extra charge

Page 3: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 3ECA 236

flat files cont … flat files cont …

disadvantages of flat filesslow when working with large filessearching is difficult and slowproblems allowing more than one user simultaneous

accessfiles are processed sequentiallydifficult to allow different levels of access

Page 4: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 4ECA 236

permissionspermissionsdirectory access ( permissions ) may be granted to:

owner: generally has permission to read and writegroups: users can be organized into groupsworld: everyone else

types of file permissionsr readablew writeablex executable

Page 5: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 5ECA 236

file processingfile processingto write to a file

open the file – create it if it does not already existwrite data to the fileclose the file

to read from a fileopen the file – if it cannot be opened, exit gracefullyread data from the fileclose the file

Page 6: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 6ECA 236

opening a fileopening a filefopen( ) takes two parameters

the file to be openedthe mode in which to open it

path to file namecreate a variable to hold the path to the file$_SERVER[ 'DOCUMENT_ROOT‘ ] contains the document

root directory$dir_name = dirname($_SERVER["PHP_SELF"]);

Page 7: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 7ECA 236

opening a fileopening a filepath 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” );

Page 8: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 8ECA 236

opening a file cont …opening a file cont …fopen( ) takes 4 parameters the 3rd and 4th are optional

the file to be openedthe mode in which to open itsearch optionoption to open file in remote location

modeindicates the purpose for opening the file

readwrite

> overwrite > append

both

Page 9: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 9ECA 236

opening a file cont …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.

wWrite 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.

Page 10: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 10ECA 236

opening a file cont …opening a file cont …

Mode Description

xCautious 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.

tText 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.

Page 11: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 11ECA 236

opening a file cont …opening a file cont …assign the fopen( ) call to a file pointer

use the file pointer to refer to the open fileif the file cannot be opened

the @ operator suppresses the noticedie( ) 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” );

Page 12: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 12ECA 236

writing to a filewriting to a file

fwrite( ) takes three parameters, the third is optional

file pointerstring which will be written to the filemaximum number of bytes

if exceeded, PHP truncates the output string

fwrite( $fp, $string_output, 1000 );

Page 13: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 13ECA 236

writing to a file cont …writing to a file cont …

separate the sections of data with a delimiterpipe, comma, tab, etcif the data will be used in another application, follow

the rules of the other applicationthe delimiter should not be one which may be used in

the user’s inputend the string with a record separator (newline )

$string_output = “$name|$gender|$height|$weight|$age\n”;

Page 14: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 14ECA 236

closing the fileclosing the file

use the fclose( ) function to close the filefclose( ) takes one parameter, the file pointer

fclose( $fp );

Page 15: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 15ECA 236

reading from a filereading from a fileopen 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 );

Page 16: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 16ECA 236

reading from a file cont …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 ) ) {

Page 17: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 17ECA 236

reading from a file cont …reading from a file cont …

when using feof( )check to see if the filesize is 0if the filesize is zero an infinite loop is generatedif there is a problem opening the file an infinite loop is

generatedfilesize( ) 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 ){

Page 18: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 18ECA 236

reading from a file cont …reading from a file cont …

fgets( ) reads from the file one line at a timefgets( ) takes two parameters, the second is

optionalthe file pointermaximum number of bytes to be returned

fgets( ) returns each line as a string$line = fgets( $fp, 1000 );

Page 19: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 19ECA 236

reading from a file cont …reading from a file cont …

fgetss( ) reads from the file one line at a timefgetss( ) takes three parameters, the third is optional

the file pointermaximum number of bytes to be returnedallowable tags

fgetss( ) returns each line as a string, with HTML and PHP removed

$line = fgetss( $fp, 1000 );

Page 20: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 20ECA 236

reading from a file cont …reading from a file cont …

fgetcsv( ) reads from the file one line at a timefgetcsv( ) takes four parameters, the third and fourth are optional

the file pointermaximum number of bytes to be returneddelimiter ( 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, “|” );

Page 21: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 21ECA 236

reading from a file cont …reading from a file cont …

readfile( )opens the fileechoes entire file to browsercloses 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" );

Page 22: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 22ECA 236

reading from a file cont …reading from a file cont …

file( )opens the filefile 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" );

Page 23: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 23ECA 236

reading from a file cont …reading from a file cont …

file_get_contents( )opens the filefile is returned as a stringcloses 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" );

Page 24: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 24ECA 236

reading from a file cont …reading from a file cont …

fgetc( ) takes one parameter file pointer

fgetc( ) returns one character at a timein addition to string characters, fgetc( ) reads newline

and eof characters

$char = fgetc( $fp );

Page 25: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 25ECA 236

reading from a file cont …reading from a file cont …

file_exists( )checks whether a file or directory existsreturns Boolean TRUE if it existsFALSE if it does not

file_exists( ) takes the path to the file as an argument

file_exists( "$doc_root/files/file_name.txt" );

Page 26: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 26ECA 236

reading from a file cont …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" );

Page 27: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 27ECA 236

file lockingfile lockingflock( ) takes two parameters

file pointera 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.

Page 28: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 28ECA 236

securitysecurityto create a file in a directory above the document

rootmake sure directory exists before writing to ittest it carefully on your server

$doc_root = $_SERVER[ 'DOCUMENT_ROOT' ];$file_name ="$doc_root/../files/file_name.txt";$fp = fopen( $file_name, "w+" );

Page 29: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 29ECA 236

security cont … security cont … crypt( ) function

one-way algorithm to encrypt a string valueno function to decrypt the string

crypt( ) takes two argumentsstring to be encryptedsalt string

salt does not work the same on different systems

Page 30: Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Open Source Server Side Scripting 30ECA 236

security cont … 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 stringcompare 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}