PHP Learning notes PHP file operation, _php tutorial

Source: Internet
Author: User
Tags fread

PHP Learning notes PHP file operation,


Fstat function: Displays all the information of a file

$file _path = "test.php", if ($fp =fopen ($file _path, "A +") {  $file _info=fstat ($fp);  echo "
";  Print_r ($file _info);  echo "
"; 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, file read:

The first type: $con = Fread ($fp, FileSize ($file _path)) $con = Str_replace ("\ r \ n", "
", $con); echo" File content is ". $con;//The second: Read all the files $con = file_get_contents ($file _path); $con = Str_replace (" \ r \ n ","
", $con); echo" File content is ". $con;//The third type: Read $buffer = 1024; In order to download the security, it is best to use the file byte read counter $file_count = 0;//feof used to determine whether the file is read to the end of the document while (!feof ($fp) && ($file _size-$file _count> 0) {$file _data = fread ($fp, $buffer);//Statistics read the number of bytes $file_count+ $buffer; echo $file _data;

Third, write the 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 an additional content. The w+ is covered by the original.    $con = "hello!\r\n";    Fwrite ($fp, $con);    echo "added successfully! ";  } else{    echo "file does not exist";  }  Fclose ($FP);  2, the second method of writing to  the file $file _path= "test.txt";  $con = "Hi Beijing!" \ 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 files:

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

 Vi. Creating files

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{  

To create a file:

Vii. Deletion of files:

  To delete a folder:

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

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 part of the PHP file to introduce the operation of the relevant knowledge, I hope to be helpful to all of you.

http://www.bkjia.com/PHPjc/1133023.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133023.html techarticle PHP Learning notes PHP file operation, one, Fstat function: Display all the information of the file $file _path = "test.php", if ($fp =fopen ($file _path, "A +")) {$file _info=fstat ($ FP); echo "Pre"; Prin ...

  • Related Article

    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.