PHP Common File Operation method

Source: Internet
Author: User

<?phpheader ("Content-type: text/html; charset=utf-8"); ########## #init ################ $file = '/ Users/m/sites/demo/test.php '; $dir  = '/users/m/sites/demo '; $deldir  = '/users/m/sites/demo/js '; ######### # #文件属性操作 ################/* file properties get *///getfilepro ($file);/* File path related *///getpathinfo ($file);/* Disk size *///echo  Tosize (Disk_total_space ('/')); ########## #文件目录操作 ################/* directory Traversal *///globdir ($DIR);/* Directory Traversal *///foreachdir ($ DIR);/* Delete file or folder *///deldir ($DELDIR);/* Move   rename *///rename ($deldir, '/users/m/sites/demo/js-marvin ');/* Copy directory *// /copydir ('/users/m/sites/demo/js ', '/users/m/sites/demo/js-back '); ########## #文件创建删除 ################/* Creating a directory */// mkdir (' Marvin ', 0644);/* Delete empty directory *///rmdir (' Marvin ');/* Delete file *///unlink (' Marvin ');/* Create Empty file *///touch (' Marvin.txt '); # # # # # ###### #文件打开方式 ################/* Read */////$handle =fopen ("{$_server[' document_root ']}/marvin.txt", ' r ');//$handle = fopen (' http://www.baidu.com ', ' r ');/* Write *///$handle =fopen ("Ftp://user:[email protected]/marvim.txt", ' w ');/*  t file files  b binary file &nBSP; default is t * r     read-only open   file pointer to file header  * r+    read/write mode open   file pointer to file header    Keep original file contents   write from scratch replace  * w     write open      file pointer to the file header, non-existent files are worn pieces  , clear the original file contents  * w+    Read and write mode open   file pointer to file header, nonexistent file to wear the pieces   Clear Original file content  * a     write open   file pointer to tail, count starting from 0   not present to create  * a+     read/write mode open, file pointer to tail, count starting from 0   Not present create  * x     create and open as write, file pointer pointing to file header if file exists , the failure  * x+    create and open read-write, the file pointer to the file header, if the file exists, fails  * *//* close *///fclose ($handle); # # # ####### #文件写入方式   must be written to open ################//$handle =fopen (' marvin.txt ', ' r+ ');//fwrite ($handle, ' worldaaaa '); ######### #文件读 ################//$handle =fopen (' Marvin.txt ', ' R '),//while (!feof ($handle))       //determine if there is data    data return False    No data return True//    echo fgetc ($handle); &NBSP;//FGETC: read one character at a time  Read a pointer move once       fgets: read one line at a time//echo fread ($handle, FileSize (' marvin.txt '));   //The second parameter is the file length//while (!feof ($handle))//    echo fread ($handle, 1024); ####### # # # #文件指针操作 ################/* Move pointer position *///fseek ($handle, 2);               //fseek ($handle, -20,seek_end);/* Initializes the pointer *///rewind ($handle);/* Returns the current position of the pointer *///echo  ftell ($handle);/* returns pointer for content *///echo fgets ($handle), ########## #不用打开文件   file read/write ################//file_ Put_contents (' Marvin.txt ', "Handsome handsome \", file_append);//Append:file_append    Lock: Lock_ex//echo file_get _contents (' Marvin.txt ');   //remote file can read//readfile (' http://www.baidu.com ');//Read remote file//$FILEARR  =  File (' Marvin.txt ');//split by row ########## #文件锁 ################//read lock:  lock_sh//write Lock:  lock_ex//release Lock:  lock_un Prevent clogging:   lock_sh+lock_nb     lock_ex+lock_nb//flock ($handle, LOCK_EX);// Flock ($handle, lock_un);Function getfilepro ($file) {    if (file_exists ($file))     {         echo  ' file type is   '. FileType ($file). ' <br> ';        echo  ' file Size '. Tosize (FileSize ($file)). ' <br> ';         if (is_readable ($file))              echo  "file readable <br>";         if (Is_writable ($file))              echo  "file writable <br>";         if (Is_executable ($file))             echo  "File executable <br>";         echo  Create time  . Date (' Y-m-d h:i:s ', Filectime ($file)). ' <br> ';        echo "Access time  ". Date (' Y-m-d h:i:s ', Fileatime ($file)). ' <br> ';        echo  ' modify time  . Date (' Y-m-d h:i:s ', Filemtime ($file)). ' <br> ';    }else    {         echo  ' file does not exist ';     }}function getpathinfo ($file) {     echo  ' file name is    '. basename ($file, '. php '). ' <br> ';//The second parameter defaults to null   file name  test    echo  ' file directory location '. DirName ($file). ' <br> ';//file directory location/users/m/sites/demo    var_dump (PathInfo ($file));} Function globdir ($dir) {    // var_dump (Glob ($dir. '/*.php '));      $it  = new directoryiterator ("glob://{$dir}./*.php");     foreach ($it  as  $f)     {        printf ("%s: %. 1fk\n ",  $f->getfilename (),  $f->getsize ()/1024);        echo  ' <br> ';     }}function foreachdir ($dir, $leve =0, $mark =true) {    static    $static _dir;    if (!is_dir ($dir)  && !is_file ($dir))          return false;    if (Is_file ($dir))      {        echo  "File: ". $dir. ' <br> ';         return true;    }    if   ($mark)          $static _dir = rtrim ($dir, '/');      $dirDesource  = opendir ($dir);     readdir ($dirDesource);//  .    readdir ($dirDesource);//      while ($filename =readdir ($dirDesource)) &NBSP;&NBSP;&NBSP;&NBsp {         $dir  = trim ($dir);          $dir  = rtrim ($dir, '/');         $file   =  $dir. '/' $filename;         if (Is_dir ($file)) {             if ($dir = = $static _dir )                   $leve =0;             echo str_repeat ('---', $leve). " Catalog:  ". $file. ' <br> ';             foreachdir ($file, + + $leve, FALSE);         }        else             echo str_repeat ('---', $leve). " File:  ". $file. ' <br> ';  &Nbsp;  }    closedir ($dirDesource);} Function deldir ($dir) {     $dir  = rtrim ($dir, '/');     if (!file_exists ($dir))         die (' File not present ');     if (IS _file ($dir))         unlink ($dir);     $dirResource  = opendir ($dir);     readdir ($dirResource);     readdir ($ Dirresource)     while ($filename =readdir ($dirResource))     {          $filename = $dir. '/'. $filename;         if (Is_dir ($filename))              Deldir ($filename);        else             unlink ($filename);    &nbSP;}     closedir ($dirResource);     rmdir ($dir);} Function copydir ($SRC, $DST) {     $src =rtrim ($src, '/');     $DST = RTrim ($DST, '/');     if (File_exists ($DST))     {         if (!is_dir ($DST))              die ('   destination file exists, not directory, cannot copy ')  ;    }else         mkdir ($DST);     $dirResource  = opendir ($SRC);     readdir ($dirResource);     readdir ($dirResource);     while ($file = Readdir ($dirResource))     {         $srcfile = $src. ' /'. $file;         $dstfile = $dst. ' /'. $file;         if (Is_dir ($srcfile)) &nbsP;           copydir ($srcfile, $dstfile);         else             copy ($srcfile, $dstfile);     }    closedir ($dirResource);} /* * 1tb=10**10 gb=10**20 mb=10**30kb=10**40b=8*10**40bit * * */function  tosize ($size) {     $units = ';     $s = ';     if ($size  > pow (2,40))     {         $s =$ Size/pow (2,40);         $units = ' TB ';     }elseif   ($size  > pow (2,30))     {          $s = $size/pow (2,30);         $units = ' GB ';     } elseif  ($size  > pow(2,20))     {         $s = $size/pow (2,20);          $units = ' MB ';    }elseif  ($size  > pow (2,10) )     {         $s = $size/pow (2,10);          $units = ' KB ';    }else    {          $s = $size;         $units = ' Bypes ';    }     $s =round ($s, 2);    return  $s. $units;}


PHP Common File Operation method

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.