PHP directory and file Manipulation skills Summary (create, delete, traverse, read, write, modify, etc.) _php tips

Source: Internet
Author: User
Tags echo date flock fread readfile rewind strlen

This article summarizes the PHP directory and file manipulation techniques. Share to everyone for your reference, specific as follows:

demo1.php

<?php
  //To assign a path to a variable//
  It is currently just a string, the string represents the path//file name of a directory
  containing the file name + file extension (that is, the file type.)
  // The file extension is plainly the file suffix
  $path = ' C:\AppServ\www\Basic6\Demo1.php ';
Echo basename ($path);
Echo ' <br/> ';
DirName-
The directory part//
echo dirname ($path) that returns the directory part////dirname return path in the path;
Echo ' <br/> ';
Gets the information//Print_r of the path file
(PathInfo ($path));
  $array _path = PathInfo ($path);
  echo $array _path[' dirname ']; This printed directory C:\AppServ\www\Basic6
  echo $array _path[' basename ';//This prints out the filename demo1.php
?>

demo2.php

<?php/
  /This is called absolute path
  //$path = ' C:\AppServ\www\Basic6\Demo2.php ';
  This is called the relative path
//$path = ' demo2.php ';
echo $path;
  $path = '. /index.php '; This is also the relative path of
  echo Realpath ($path);
? >

demo3.php

<?php
  $path = ' C:\AppServ\www\Basic6\Demo3.php ';
  Echo Round (FileSize ($path)/1024,2). ' KB ';
? >

demo4.php

<?php
  //Free space view Disk_free_space
  echo round (Disk_free_space (' C: ')/1024/1024/1024,2). ' GB ';
  Echo ' <br/> ';
  Total Space
  echo round (Disk_total_space (' C: ')/1024/1024/1024,2). ' GB ';
? >

demo5.php

<?php
  $path = ' C:\AppServ\www\Basic1\Demo1.php ';
  All we have to do is translate this timestamp into a normal date
  //echo fileatime ($path);
  Adjust time zone
  date_default_timezone_set (' Asia/shanghai ');
  Format a local date
  //echo date (' y-m-d h:i:s ');
  Fileatime-Gets the file's last access time
  Echo ' last visit: '. Date (' y-m-d h:i:s ', Fileatime ($path)). ' <br/> ';
  Filectime--Gets the inode modification time of the file, file owner, permission
  Echo ' permission owner, and so on: '. Date (' y-m-d h:i:s ', Filectime ($path)). ' <br/> ';
  Filemtime--Get file modification time, the contents of the file changes after the time
  echo ' content modification time: '. Date (' y-m-d h:i:s ', Filemtime ($path));
? >

demo6.php

<?php
  //Open a file
  ///first parameter indicating which file, the second parameter indicates mode, W only write
  //w if, file.txt already has, and has the data. So this file, recreate
  //If not file.txt this file, then I will create
////fopen--open file or URL
////fopen Return is the resource type resource, we generally call it the handle, or the resource handle
//$fp = fopen (' file.txt ', ' W ');
////
want to write some data in the file
////fwrite ($FP, "read who asked the chant, water fall fragrance floating." ");
//$outString = "read who asked you to chant, water fall fragrance floating." ";
echo strlen ($outString);//Get length
//Fwrite ($FP, $outString, strlen ($outString));
////fclose--Close an open file pointer/////////////
fclose ($fp) when opening a file;
  There is only one sentence, do not need any other flexible things to use
  //This relatively good, only in the PHP5 version can use
  file_put_contents (' file2.txt ', ' read who asked you chant, water fall fragrance floating. ');
? >

demo7.php

<?php
  //The first way to
  //\r\n a text file is to change
  the line $fp = fopen (' file.txt ', ' a ');
  $outString = "read who asked you to recite, \ r \ n water Fall fragrance floating." \ r \ n ";
  Fwrite ($fp, $outString, strlen ($outString));
  Fclose ($FP);
  The second type of writing
//file_put_contents (' file2.txt ', ' read who asked you to recite, \ r \ n water Fall fragrance floating. ');
? >

demo8.php

<?php
  //Now read out the file
  $fp = fopen (' file.txt ', ' R ');
  Fgetc-Reads the character
//Echo fgetc ($FP) from the file pointer;
echo fgetc ($fp);
echo fgetc ($fp);
echo fgetc ($FP); Read who
  //fgets--reading a row from the file pointer
  //fread () reads a row from the file pointed to by handle and returns a string that has a maximum length of length-1 bytes.
  //echo fgets ($fp);
  Can filter HTML
  //echo fgetss ($fp);
  Echo Fread ($FP, 2);
  Fread () reads the maximum length of bytes from the file pointed to by handle
  //echo fread ($fp);
  Fpassthru--All the remaining data//fpassthru the output file pointer
  also contains functionality that is output to the browser, so no echo
  echo fpassthru ($FP) is required;
  Fclose ($FP);
? >

demo9.php

<?php
  //file--Reads the entire file into an array
  //file is stored in an array in each row
  //print_r (file (' file.txt '));
  And this prints out the second line in the file.
//$array _file = File (' file.txt ');
echo $array _file[4];
  ReadFile--Output a file
  //readfile is the entire file read out, ReadFile itself can enter the browser directly, so do not need
  the echo//return value is the total length of the file bytes
  //echo ReadFile (' file.txt ');
  File_get_contents-Reads the entire file into a string
  //file_get_contents can read the data into the buffer and print
  echo file_get_contents (' File.txt ');
? >

demo10.php

<?php
  //Now read out the file
  $fp = fopen (' file.txt ', ' R ');
  Feof--Tests whether the file pointer is at the end of the file while
  (!feof ($fp)) {
    echo fgetc ($FP);
  }
  Fclose ($FP);
? >

demo11.php

<?php//
  For example, when reading a file, you have to ensure that the file must exist before you can operate
  //or error, do not let the implementation//
  this time have to do a test
  //file_exists- Check if the file or directory exists if
  (file_exists (' file.txt ')) {
    echo ' performs various text read and write operations! ';
  } else{
    Echo ' This article does not exist, ask the administrator to regenerate in the background. ';
  }
? >

demo12.php

<?php
  //Text size, byte
  //echo filesize (' file.txt ');
  Deletes a file
  //unlink (' a.txt ');
  Rewind--back to the position of the file pointer
  //ftell--Returns the position of the file pointer read/write
  //rewind--The position of the file pointer back to the
  //a representation can append, B represents binary, so portability is good
  //w
  $fp = fopen (' file.txt ', ' ab ');
echo fgetc ($fp);
echo fgetc ($fp);
echo fgetc ($fp);
echo fgetc ($fp);
Rewind ($FP);
Echo Ftell ($fp);
Fseek ($FP);
Echo Ftell ($fp);
echo fgetc ($fp);
echo fgetc ($fp);
  Flock ($FP, LOCK_EX);//Lock
  fwrite ($fp, ' This is lock! ');
  Flock ($FP, lock_un);//Unlock
  fclose ($fp);
? >

demo13.php

<?php////
open a directory
//@ $dir = Opendir (' C:\AppServ\www\Basic6 ');
read out the directory, use a loop to read
////readdir-Read the entry////string from the directory handle
if it is a Boolean value, that is, the string is not empty, then it is true, empty is false
//while (!! $file = Readdir ($dir)) {
//   echo $file. ' <br/> ';
}////
close
//@closedir ($dir);
  Print_r (Scandir (' C:\AppServ\www\Basic6 '));
  Deletes a directory///
absolute Path Deletion directory
//rmdir (' C:\AppServ\www\Basic6\delMe ');
Deletion directory of relative path
//rmdir (' Delme ');
  Deletes a file
  //ssunlink (' file2.txt ');
  Rename, here, directories and files can be changed
  //rename (' file.txt ', ' file2.txt ');
>

More about PHP Interested readers can view the site topics: "PHP file Operation Summary", "PHP Directory Operation skills Summary", "PHP Basic Grammar Primer", "PHP operation and operator Usage Summary", "PHP object-oriented Programming Program", " PHP Network Programming skills Summary, "PHP string (String) Usage summary", "PHP Array" Operation Tips Encyclopedia, "php+mysql database Operation Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.