Introduction to File Processing with PHP 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. File Structures • • • • File Structures are persistent data structures Files composed of records Records composed of fields Files can be viewed as tables • File -> Table • Record -> Row • Field -> Column File Organization • The data is stored as a collection of files. Each file is a sequence of records. A record is a sequence of fields. • One approach: • assume record size is fixed • each file has records of one particular type only • this case is easiest to implement; we will consider it further 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 Fixed-Length Records • Simple approach: • Store record i starting from byte n (i – 1), where n is the size of each record. • Record access is simple but records may cross blocks • Modification: do not allow records to cross block boundaries • Ways to delete record i: • move records i + 1, . . ., n to i, . . . , n – 1 • move record n to i • do not move records, but mark deleted record Variable-Length Records • Variable-length records arise in database systems in several ways: • Storage of multiple record types in a file. • Record types that allow variable lengths for one or more fields such as strings • Record types that allow repeating fields (used in some older data models). • We won’t talk about VL records Sequential File Organization • For sequential processing of entire file • Records ordered by a search-key Sequential File Organization • Deletion – use pointer chains • Insertion –locate the position where the record is to be inserted – if there is free space insert there – if no free space, insert the record in an overflow block – In either case, pointer chain must be updated • Need to reorganize the file from time to time to restore sequential order The CRUD paradigm • Open the current version of a file • Process it using the CRUD operations – Create records – Retrieve records – Update records – Delete records • Output and close the new version of the file 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 Some PHP File Tutorials • http://www.tizag.com/phpT/files.php • http://php.about.com/od/advancedphp/ss/php_read_file_5.htm • http://www.codingunit.com/php-tutorial-file-handling