PHP file Operations

Source: Internet
Author: User
Tags fread

 Fstat function: Displays all the information of 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, file read:

//The first type:$con=fread($fp,filesize($file _path));$con=Str_replace("\ r \ n", "<br>",$con);Echo"File content is".$con;//the second type: Read all the files$con=file_get_contents($file _path);$con=Str_replace("\ r \ n", "<br>",$con);Echo"File content is".$con;//Third: A period of reading$buffer= 1024; //for the security of the download, it is best to read the counter using File bytes$file _count= 0;//feof used to determine whether a file is read to the end of a document while(!feof($fp) && ($file _size-$file _count>0)){$file _data=fread($fp,$buffer);//How many bytes did the statistic read ?$file _count+$buffer;Echo $file _data; }

  Third, write the file:

   //1. Traditional methods to write files    $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= "Hello Beijing!" \ r \ n "; file_put_contents($file _path,$con,file_append); Echo"Success";

  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);            ? >    // The data in the INI file is read as an array and can be manipulated. 

  V. Copy files:

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

  Vi. Creating files

To create a folder:

//$path = "E:\\happy"; Folder path$path= "E:\\HAPPY\AAA\BBB";//Multilevel Foldersif(!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"); fclose ($fp);

Vii. Deletion of files:

To delete a folder:

$path // Multilevel Folders if (rmdir($path)) {    echo "Success";} // rmdir can only delete empty folders, files or directories under the folder cannot be deleted. 

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";}

PHP file Operations

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.