Transcript file

Introduction to File Processing
with PHP - Part 2
Indexed Files
Review of Course Outcomes
1. Implement file reading and writing programs using PHP.
2. Identify file access schemes, including:
sequential file access
direct file access
indexed sequential file access.
3. Describe file-sorting and file-searching techniques.
4. Describe data compression and encryption techniques.
5. Design a rational database using E-R modeling techniques.
6. Build a relational database.
7. Write database queries using SQL.
8. Implement a web-based relational database using MySQL.
Organization of Records in Files
• Heap – a record can be placed anywhere in the
file where there is space
• Sequential – store records in sequential order,
perhaps based on the value of the search key of
each record
• Indexing – Keep two files, the Data File and an
Index File and the data file. Index records hold
file pointers of Data records
• Hashing – a hash function computed on some
attribute of each record; the result specifies in
which block of the file the record should be
placed
Implementing CRUD paradigm in PHP
• Use PHP file functions
• There a many of them
• We will start with a simple subset that are
similar to file functions used in C and in other
C-based languages
The PHP filesystem functions
• http://us2.php.net/manual/en/ref.filesystem.
php
A C-like subset
• fopen
– http://us2.php.net/manual/en/function.fopen.php
• fgets
– http://us2.php.net/manual/en/function.fgets.php
• fwrite
– http://us2.php.net/manual/en/function.fwrite.php
• fclose
– http://us2.php.net/manual/en/function.fclose.php
To Open a File for Processing
resource fopen ( string $filename , string $mode [, bool $use_include_path =
false [, resource $context ]] )
Mode Codes
mode
Description
'r'
Open for reading only; place the file pointer at the beginning of the file.
'r+'
Open for reading and writing; place the file pointer at the beginning of the file.
'w'
Open for writing only; place the file pointer at the beginning of the file and truncate the
file to zero length. If the file does not exist, attempt to create it.
'w+'
Open for reading and writing; place the file pointer at the beginning of the file and
truncate the file to zero length. If the file does not exist, attempt to create it.
'a'
Open for writing only; place the file pointer at the end of the file. If the file does not exist,
attempt to create it.
'a+'
Open for reading and writing; place the file pointer at the end of the file. If the file does
not exist, attempt to create it.
'x'
Create and open for writing only; place the file pointer at the beginning of the file. If the
file already exists, the fopen() call will fail by returning FALSE and generating an error of
level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to
specifying O_EXCL|O_CREAT flags for the underlying open(2)system call.
'x+'
Create and open for reading and writing; otherwise it has the same behavior as 'x'.
Read a string from a file
string fgets ( resource $handle [, int $length ] )
Write a string to a file
int fwrite ( resource $handle , string $string [, int
$length ] )
Close a file to processing
bool fclose ( resource $handle )
Test if a file is at the end
bool feof ( resource $handle )
Move the file pointer
int fseek ( resource $handle , int $offset [, int $w
hence = SEEK_SET ] )
Read a string
string fread ( resource $handle , int $length )
Read an entire file
array file ( string $filename [, int $flags =
0 [, resource $context ]] )
Read an entire file
string file_get_contents ( string $filename [, boo
l $use_include_path =
false [, resource $context [, int $offset = 1 [, int $maxlen ]]]] )
Write to a file
int file_put_contents ( string $filename , mixed
$data [, int $flags = 0 [, resource $context ]] )