星期日, 10月 31, 2010

Zend PHP 5 Certification Study Guide 筆記 (五) Streams and Network Programming

A list of possible modes for fopen() using mode
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'.
'c'
Open the file for writing only. If the file does not exist, it is
created. If it exists, it is neither truncated (as opposed to
'w'), nor the call to this function fails (as is
the case with 'x'). The file pointer is
positioned on the beginning of the file. This may be useful if it's
desired to get an advisory lock (see flock())
before attempting to modify the file, as using
'w' could truncate the file before the lock
was obtained (if truncation is desired,
ftruncate() can be used after the lock is
requested).
'c+'
Open the file for reading and writing; otherwise it has the same
behavior as 'c'.

如果檔案不存在,用w,w+,a, and a+會自動建立新檔
如果檔案已存在,用x及x+會產生E_WARNING
fgets reads a line -- i.e. it will stop at a newline.  
fread reads raw data -- it will stop after a specified (or default) number of bytes, independantly of any newline that might or might not be present.

  • 指標
    $file = fopen(’counter.txt’, ’r+’);
    fseek($file, 10, SEEK_SET);
    • SEEK_SET - Set position equal to offset bytes.
    • SEEK_CUR - Set position to current location plus offset.
    • SEEK_END - Set position to end-of-file plus offset.
    範例
    <?php
    
    $fp = fopen('somefile.txt', 'r');
    
    // read some data
    $data = fgets($fp, 4096);
    
    // move back to the beginning of the file
    // same as rewind($fp);
    fseek($fp, 0);
    
    ?>
    
  • Simple File Functions
    • get data
      // Old Way
      $file = implode("\r\n", file("myfile.txt"));
      // New Way
      $file = file_get_contents("myfile.txt");
      
    • write data
      $data = "My Data";
      file_put_contents("myfile.txt", $data, FILE_APPEND);
      
      $data = array("More Data", "And More", "Even More");
      file_put_contents("myfile.txt", $data, FILE_APPEND);
      
  • Controlling File Access
    • is_dir()—Checks if the path is a directory
    • is_executable()—Checks if the path is executable
    • is_file()—Checks if the path exists and is a regular file 
    • is_link()—Checks if the path exists and is a symlink 
    • is_readable()—Checks if the path exists and is readable 
    • is_writable()—Checks if the path exists and is writable
    • is_uploaded_file()—Checks if the path is an uploaded file (sent via HTTP POST)

沒有留言: