PHP Learning notes PHP file Operation _php Instance

Source: Internet
Author: User

First, Fstat function: Display all information about a file

$file _path = "test.php";
if ($fp =fopen ($file _path, "A +")) {
  $file _info=fstat ($fp);
  echo "<pre>";
  Print_r ($file _info);
  echo "</pre>";
  echo "File size is". $file _info[' size '];
  echo "File last Access Time". Date ("Y-m-d h:i:s", $file _info[' mtime ');
}
Fclose ($FP);  Be sure to close

Second, the file reads:

First:
$con = fread ($fp, FileSize ($file _path));
$con = Str_replace ("\ r \ n", "<br>", $con);
echo "File content is". $con;
Second: Read all of the files
$con = file_get_contents ($file _path);
$con = Str_replace ("\ r \ n", "<br>", $con);
echo "File content is". $con;
Third: A section of reading
$buffer = 1024; 
For security to download, it is best to use the file byte read counter
$file _count = 0;
Feof is used to determine whether a file is read to the end of the document
while (!feof ($fp) && ($file _size-$file _count>0)) {
$file _data = fread ($fp, $buffer);
Count the number of bytes read
$file _count+ $buffer;
echo $file _data;  

Iii. Write to File:

 1, the traditional method writes
  the file $file _path = "test.txt";
  if (file_exists ($file _path)) {
    $fp = fopen ($file _path, "A +");
    Open mode: A + is the append content. W+ is covered by the original.
    $con = "hello!\r\n";
    Fwrite ($fp, $con);
    echo "Add success!" ";
  } else{
    echo "file does not exist";
  }
  Fclose ($FP);
  2. The second method is written to
  the file $file _path= "test.txt";
  $con = "Beijing Hello!" \ r \ n ";
  File_put_contents ($file _path, $con, file_append);
  

  Iv. Application of file operation:

You can manipulate the INI file. Write the server's configuration in the INI file, and then manipulate it.
  Dbc.ini
  host=192.168.0.1
  admin=admin
  password=123456  
demo.php
  <?php
    $ con = parse_ini_file ("Dbc.ini");
    Print_r ($con);  
    ? > 

 V. Copy of documents:

if (!copy ("E:\\test.txt", "D:\\1.txt")) {
  echo "fail";
} Else{
  echo "Success";

 VI. Create a file

To create a folder:

$path = "E:\\happy"; Folder path
$path = "e:\\happy\aaa\bbb";//Multilevel folder
if (!is_dir ($path)) {
  if (mkdir ($path, 0777,true)) {
    echo "Success";
  } else{
    echo "fail";
  }
else{
  echo "folder already exists";

To create a file:

$file _path = "E:\\happy.txt";
$fp = fopen ($file _path, "w+");
Fwrite ($fp, "Hello");

Vii. Deletion of files:

  To delete a folder:

$path = "E:\\HAPPY\AAA\BBB"; Multilevel folder
if (RmDir ($path)) {
  echo "success";
}

To delete a file:

$file _path = "E:\\happy.txt";
if (Is_file ($file _path)) {
  if (unlink ($file _path)) {
    echo "success";
  } else{
    echo "fail";
  }
else{
  echo "file does not exist";
}

The above is a small set to introduce the PHP file operation related knowledge, I hope to help you.

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.