introduction to file processing with php - part 2 indexed files

17
Introduction to File Processing with PHP - Part 2 Indexed Files

Upload: nickolas-rodgers

Post on 14-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to File Processing with PHP - Part 2 Indexed Files

Introduction to File Processing with PHP - Part 2

Indexed Files

Page 2: 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 accessindexed 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.

Page 3: Introduction to File Processing with PHP - Part 2 Indexed Files

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

Page 4: Introduction to File Processing with PHP - Part 2 Indexed Files

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

Page 5: Introduction to File Processing with PHP - Part 2 Indexed Files

The PHP filesystem functions

• http://us2.php.net/manual/en/ref.filesystem.php

Page 6: Introduction to File Processing with PHP - Part 2 Indexed Files

A C-like subset

• fopen – http://us2.php.net/manual/en/function.fopen.ph

p• fgets– http://us2.php.net/manual/en/function.fgets.php

• fwrite– http://us2.php.net/manual/en/function.fwrite.ph

p• fclose– http://us2.php.net/manual/en/function.fclose.ph

p

Page 7: Introduction to File Processing with PHP - Part 2 Indexed Files

To Open a File for Processingresource fopen ( string $filename , string $mode [, bool $use_include_path =                 false [, resource $context ]] )

Page 8: Introduction to File Processing with PHP - Part 2 Indexed Files

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

Page 9: Introduction to File Processing with PHP - Part 2 Indexed Files

Read a string from a file

string fgets ( resource $handle [, int $length ] )

Page 10: Introduction to File Processing with PHP - Part 2 Indexed Files

Write a string to a file

int fwrite ( resource $handle , string $string [, int $length ] )

Page 11: Introduction to File Processing with PHP - Part 2 Indexed Files

Close a file to processing

bool fclose ( resource $handle )

Page 12: Introduction to File Processing with PHP - Part 2 Indexed Files

Test if a file is at the end

bool feof ( resource $handle )

Page 13: Introduction to File Processing with PHP - Part 2 Indexed Files

Move the file pointer

int fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] )

Page 14: Introduction to File Processing with PHP - Part 2 Indexed Files

Read a string

string fread ( resource $handle , int $length )

Page 15: Introduction to File Processing with PHP - Part 2 Indexed Files

Read an entire file

array file ( string $filename [, int $flags = 0 [, resource $context ]] )

Page 16: Introduction to File Processing with PHP - Part 2 Indexed Files

Read an entire file

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )

Page 17: Introduction to File Processing with PHP - Part 2 Indexed Files

Write to a file

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )