PHP file Operations

Source: Internet
Author: User
Tags echo date glob

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHP// file Operation//1. You can only manipulate the server's file//2. Files contain directories and files//Judging file Types//var_dump (filetype ("./img")); Returns the type of file: Directory dir file File//var_dump (Is_dir ("./img")); Determine if the path is not correctly returned by the directory true error//var_dump (Is_file ("./add.php")); To determine if the path is correct, return a true error return false//file property//echo date ("Y-m-d h:i:s", Fileatime ("./add.php")); Last Access time//echo date ("Y-m-d h:i:s", Filectime ("./add.php")); Creation time//echo date ("Y-m-d h:i:s", Filemtime ("./add.php")); Modification Time//echo filesize ("./add.php"); The file size, in bytes//var_dump (file_exists ("./add.php")); Determine if the file exists//file path//current path:./parent directory:. /Subordinate Directory: Directory//root path:///If the root (/) path inside PHP represents: The root of the disk//if the root (/) path inside the page represents: root of the server (www directory)//echo $_server[' document_root '); Fetch the root path of the server//echo basename ("./add.php"); Gets the filename with the suffix//echo basename ("./add.php", ". php") from the path; Gets the file name without a suffix from the path//echo dirname ("./img/aa.php"); Directory//var_dump (PathInfo ("./img/aa.php") that removes the filename from the fetch path; Returns the directory information, formatted as an array//echo realpath ("./img/aa.php"); Convert relative path to absolute path//Operation directory//var_dump (Glob ("./img/*")); Get all Files under directory * A suffix of *.php /*$attr = Glob ("./img/*"); for ($i =0; $i <count ($attr); $i + +) {echo $attr [$i]. " <br> ";}*///use global variables to calculate the number of files and folders/*//define two variables $filenum=0; $dirnum = 0; Num ("./img"); echo "The number of files is:". $filenum. "    The number of <br> "; echo" Folders is: ". $dirnum; function num ($url) {global $filenum;        Use global variables to $dirnum;        Use global variables//To determine if the path is not a file if (Is_file ($url)) {$filenum = 1;        return $filenum;        Exit }//Gets all the sub-files under the directory $attr =glob ($url. "        /*");            Traverse all child files foreach ($attr as $v) {if (Is_file ($v)) {$filenum + +;            } else {$dirnum + +;            Num ($v); }        }}*///calculate only the number of files/*Echo Num ("./img"); function num ($url) {$filenum = 0;        Determines whether the given path is not a file if (Is_file ($url)) {$filenum = 1;        return $filenum;        Exit }//Gets all the sub-files under the directory $attr =glob ($url. "        /*");            Traverse all child files foreach ($attr as $v) {if (Is_file ($v)) {$filenum + +;            } else {$filenum = $filenum +num ($v); }} return $filenum;}*///Calculate Folder Size/*Echo Dirsize ("./img"); function Dirsize ($url) {//define variable store file size $size = 0;        Determine if the path given is not a file if (Is_file ($url)) {return filesize ($url);    Exit }//If it is a directory, fetch all files under the directory $attr = Glob ($url. "        /*");        Traverse Sub-file foreach ($attr as $v) {if (Is_file ($v)) {$size = $size + filesize ($v);        } else {$size = $size + dirsize ($v); }} return $size;}*///using directory Resource traversal$dir=Opendir("./img");//Open the Directory resource//echo Readdir ($DIR);//Read the file in the directory, perform a read one while($WJ=Readdir($dir)){    Echo $WJ." <br> ";}Closedir($dir);//Close the directory resource//rewindder ($DIR);//Reset the pointer//use READDIR () to read the first and second contents//First is. Represents the current directory//The second is: Represents a parent directory// When using directory resources to traverse a folder, be careful to exclude the first two?></body>View Code
<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHP//directory Operation//1. Create a directory (folder)//mkdir ("./test");//2. Deleting a directory (folder) can only delete empty folders//rmdir ("./test");//3. Moving directories (folders) is equivalent to cutting internal content and then moving/ Rename ("./test", ".. /test11 "); File Operation//touch ("./test.txt");//Create File//copy ("./test.txt", "... /test.txt "); Copy file//unlink ("./test.docx"); Delete file//echo file_get_contents ("./test.txt"); Get file contents: TXT type and page content//echo file_get_contents ("http://www.baidu.com"); Remotely get content//file_put_contents ("./test.txt", "Hello"); Write content to the file and empty the original content//readfile ("./test.txt"); Get the contents of the file and output//var_dump directly ("./test.txt"); Read the contents of the file, return the array, each element of the array corresponds to a line//to the contents of the file Operation//1. Open File resource//$fp = fopen ("./test.txt", "a");//Operation file//echo fgetc ($FP); Read one character at a time/*while (!feof ($FP))//Determine if the file reads to the end {echo fgetc ($fp);}*///Echo fgets ($FP);//read one line//echo fgets ($fp),//echo fread ($FP, 10);//Read specific length//$str = "Ni hao dhakhdak \ r \ n Akdhka";//$str = Iconv ("Utf-8", "gb2312", $str);//fwrite ($fp, $str);//2. Close File Resource//fclose ($FP);//world document Excel document from the Internet//job: File Manager// Find a directory (manage the files in this directory)//display the directory of all files//files at the top of the back of the upper button can return to the parent folder?></body>View Code

Focus

 // var_dump (FileType ("... /0529 ")); Returns the type of file: Directory dir file File//var_dump (Is_dir (". /11.php ")); Determine if the path is directory//var_dump (Is_file (". /11.php ")); Determine if the path is file//echo filesize (".. /11.php "); The file size, in bytes, of//var_dump (File_exists (". /12.php ")); Determine if the file exists//current path:./parent directory:. /Subordinate Directories: Directory/ROOT path:///If the root (/) path inside PHP represents: The root of the disk//if the root (/) path inside the Web page represents: the root of the server (www directory)//var_dump (PathInfo (". /0529/aa.php ")); Returns the directory information, formatted as an array//echo realpath (". /0529/aa.php "); Convert relative path to absolute path   // 1. Create a directory (folder)//mkdir ("./test");//2. Deleting a directory (folder) can only delete empty folders//rmdir ("./test");//3. Move directory (folder)//rename ("./test", ". /test11 "); Touch ("./test.txt");//Create File//copy ("./test.txt", ". /test.txt "); Copy file//unlink ("./test.docx"); Delete file//echo file_get_contents ("./test.txt"); Get file Contents: TXT page   file resource  
View Code

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.