Home  >  Article  >  Backend Development  >  php filesystem file system function usage

php filesystem file system function usage

WBOY
WBOYOriginal
2016-07-25 08:54:261075browse
  1. $path = "/home/httpd/phpha.com/index.php";
  2. echo basename($path);
  3. echo basename($path, '.php');
  4. echo basename($path, '.xxx');
  5. echo dirname($path);
  6. ?>
  7. //Result:
  8. index.php
  9. index
  10. index.php
  11. /home/httpd/phpha.com
Copy code

Note: If the file name ends with the correct suffix, this part will also be removed. chgrp — change the group a file belongs to chown — change the owner of a file chmod — change file mode bool chmod ( string $filename , int $mode )

Example 2:

  1. chmod('/home/phpha.txt', 0755);
  2. ?>
Copy code

copy — copy file

  1. if(copy('index.php', 'index.php.bak')){
  2. echo 'copy success';
  3. }
  4. ?>
  5. //The index survived in the current directory. php.bak file
Copy code

delete — see unlink or unset

unlink — delete files Example 3:

  1. if(unlink('index.php.bak')){
  2. echo 'unlink success';
  3. }
  4. ?>
  5. //Deleted index.php.bak
Copy code

disk_free_space — Returns the free space in a directory disk_total_space — Returns the total disk size of a directory diskfreespace — alias for disk_free_space

Example 4:

  1. //Under Windows:
  2. echo disk_free_space("C:"), '
    ';
  3. echo disk_total_space("C:");
  4. ?>
Copy code

///Result: The number of bytes returned 17433419776 32218386432 fopen — open a file or URL fgets — Read a line from a file pointer feof — Tests whether the file pointer reaches the end of the file fread — read a file (safe for binary files) fwrite — write to a file (safe for binary files) fclose — Close an open file pointer

Example 5:

  1. $fp = fopen('hello.txt', 'r'); //Open a file
  2. $n = 1;
  3. while(!feof($fp)){
  4. echo $n, ' - ', fgets($fp), '
    '; //Read a line and output
  5. $n++;
  6. }
  7. fclose($fp); //Close the file
  8. ?> ;
Copy code

//Output: 1 - Welcome to my blog: 2 - http://bbs.it-home.org fgetc — Read characters from a file pointer fgetcsv — Read a line from a file pointer and parse CSV fields fgetss — Read a line from a file pointer and filter out HTML tags fputcsv — Format rows to CSV and write a file pointer fputs — alias for fwrite

  1. $fp = fopen('hello.txt', 'r');
  2. while(false !== ($char = fgetc($fp))){
  3. echo $char , '-';
  4. }
  5. ?>
  6. //Output:
  7. W-e-l-c-o-m-e- -t-o- -m-y- -b-l-o-g-:- - -h-t-t-p-:-/-/-b-l-o-g-.-p-h-p-h-a-.-c-o-m-
Copy code

1 2 Next page Last page



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn