Standard file I/Ofopen, unlink, flock, file, opendir, readdir

Source: Internet
Author: User
Tags flock
Standard file I/Ofopen, unlink, flock, file, opendir, readdir fopen (public file)
$ Fno = fopen ("path/filename", 'r '); | _ r read-only | r + input/output | w write-only | w + input/output (overwrite) | a append output | a + input/output (append output) | _ file name | _ file processing Flase = error}
Fclose (close file)
Fclose ($ fno); | _ file processing

If you want to disable unopened file processing, an error will occur.

Unlink (delete)
$ Rcd = unlink ("path/filename"); | _ file name | _ True = normal False = error
File_exists (check whether the file exists)
$ Rcd = file_exists ("path/filename"); | _ file name | _ True = existence False = nonexistent
Filesize (file size)
$ Size = filesize ("path/filename"); | _ file name | _ bytes
Filemtime (File time Mark)
$ Time = filemtime ("path/filename"); | _ file name | _ UNIX time Mark $ dttm = date ('Y. m. d H: I: S', filemtime ("path/filename"); | _ format | _ '2002. 06.01 15:00:00'
Copy)
$ Rcd = copy ("path/filename", "path2/filename2"); | _ target file name | _ source file name | _ True = normal False = error
Rename (change file name)
$ Rcd = rename ("path/filename", "path2/filename2"); | _ new file name | _ old file name | _ True = normal False = error
Flock (file lock, exclusive control)
Flock ($ fno, 2); | _ 1 lock read shares (in PHP4, with LOCK_SH constant) | 2 lock write exclusively (in PHP4, with LOCK_EX constant) | 3 unlock (in PHP4, there are LOCK_UN constants) | _ file processing set_file_buffer ($ fno, 0); | _ specify 0 to prevent misoperation when locking the file and fwrite buffer.
Text stream I/O example
$ Rcd = rename ("path/filename", "path2/filename2"); | _ new file name | _ old file name | _ True = normal False = error
Flock (file lock, exclusive control)

[Example 1] input

if (!($fno = fopen("test.txt", 'r'))) {    die;}  while (!feof($fno)) {    $rec = fgets($fno, 32000);}  fclose($fno);

[Example 2] input

$rec = file("test.txt");  reset($rec);  while ($a = each($rec)) {  print "$a[0] - $a[1]\n";}

(Example 3) enter foreach to use only PHP4)

$rec = file("test.txt");  foreach ($rec as $a) {  print "$a\n";  }

[Example 4] input

if (!($fno = fopen("test.txt", 'w'))) {    die;}  fwrite($fno, "aaaaa\n", 32000);  fwrite($fno, "bbbbb\n", 32000);  fclose($fno);
Opendir (Open Directory)
$ Dir = opendir ("path"); | _ directory | _ file processing Flase = error
Closedir (close directory)
Closedir ($ dir); | _ file processing
Directory reference example

[Example]

if (!($dir = opendir("./"))) {    die;}  while ($fnm = readdir($dir)) {    print "$fnm\n";}  closedir($dir);

** Note that "." and "." are also included.

[Example] extract only the specified extension

if (!($dir = opendir("./"))) {    die;}  while ($fnm = readdir($dir)) {    if (ereg('.php$', $fnm)) {    print "$fnm\n";}}  closedir($dir);

[Example] ascending order

if (!($dir = opendir("./"))) {    die;}  while ($fnm[] = readdir($dir));  closedir($dir);  sort($fnm);  reset($fnm);  while ($a = each($fnm)) {  print "$a[1]\n"; }

** Note that "." and "." are also included.

[Example] descending order

if (!($dir = opendir("./"))) {    die;}  while ($fnm[] = readdir($dir));  closedir($dir);  rsort($fnm);  reset($fnm);  while ($a = each($fnm)) {  print "$a[1]\n";}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.