PHP Common Technical text file operation and directory operation summary, _php Tutorial

Source: Internet
Author: User

PHP Common Technical text of the file operation and directory operation summary,


First, the operation of the basic file

The basic operation of the file is: file Judgment, catalogue judgment, file size, reading and writing judgment, existence judgment and file time, etc.

<?php header ("Content-type", "text/html;charset=utf-8");      /* Declare a function, pass in the filename to get the file attribute * @param string $fileName file name */function Getfilepro ($fileName) {if (!file_exists ($fileName)) { Echo ' file does not exist
'; Return }/* is a normal file */if (Is_file ($fileName)) {echo $fileName. ' is a file
'; }/* is the directory */if (Is_dir ($fileName)) {echo $fileName. ' is a directory '; }/* The type of output file */echo ' file type is: '. Getfiletype ($fileName). '
'; /* File size, conversion unit */echo ' file size is: '. GetFileSize (FileSize ($fileName)). '
'; /* File is readable */if (is_readable ($fileName)) {echo ' file is readable
'; }/* File is writable */if (is_writable ($fileName)) {echo ' file is writable
'; }/* files can be executed */if (is_executable ($fileName)) {echo ' file executable
'; The echo ' file was created: '. Date (' Y year M-month J-Day ', Filectime ($fileName)). '
'; Echo ' File last modified: '. Date (' Y year M-month J-Day ', Filemtime ($fileName)). '
'; Echo ' File last opened: '. Date (' Y year M-month J-Day ', Fileatime ($fileName)). '
'; }/* Declares a function that returns the file type * @param string $fileName file name */function Getfiletype ($fileName) {$type = '; Switch (filetype ($fileName)) {case ' file ': $type. = ' normal file '; Case ' dir ': $type. = ' directory file '; Case ' block ': $type. = ' block device file '; Case ' char ': $type. = ' character device file '; Case ' filo ': $type. = ' piping file '; Case ' link ': $type. = ' symbolic link '; Case ' unknown ': $type. = ' Unknown file '; Default:} return $type; }/* Declares a function that returns the file size * @param int $bytes File bytes */function GetFileSize ($bytes) {if ($bytes >= pow (2,40)) {$retur n = Round ($bytes/pow (1024,4), 2); $suffix = ' TB '; } else if ($bytes >= pow (2,30)) {$return = round ($bytes/pow (1024,3), 2); $suffix = ' GB '; } else if ($bytes >= pow (2,20)) {$return = round ($bytes/pow (1024,2), 2); $suffix = ' MB '; } else if ($bytes >= pow (2,10)) {$return = round ($bytes/pow (1024,1), 2); $suffix = ' KB '; } else {$return = $bytes; $suffix = 'B '; } return $return. " ". $suffix;} /* Call the function, pass in the Test.txt file under the test directory */Getfilepro ('./test/div.html ');? >

Results:

Second, the operation of the directory

Directory operations are: Traverse directory, delete, copy, size statistics, etc.

1. Traverse Directory

/* * Traverse directory * @param string $dirName directory name */function Finddir ($dirName) {$num = 0;/* Statistics Sub-file number */$dir _handle = Opendir ($dirN AME); /* Open Directory */* Output Directory file */echo '
 
 
  
  '; Echo '
  
  '; Echo '
  
   '; while ($file = Readdir ($dir _handle)) {$dirFile = $dirName. ' /'. $file; $bgcolor = $num ++%2==0? ' #ffffff ': ' #cccccc '; Echo ' 
   
     '; Echo ' 
    '; Echo ' 
    '; Echo ' 
    '; Echo ' 
    '; Echo ' 
   '; } echo " 
  

Directory '. $dirName. ' Files

FileName File size file type modification time
'. $file. ''. FileSize ($dirFile). ''. FileType ($dirFile). ''. Date (' y/n/t ', Filemtime ($dirFile)). '
"; Closedir ($dir _handle); /* Close directory */echo "in". $dirName."In the catalogue, there". $num. 'Sub-file '; }/* Pass the test directory under the current directory */Finddir ('./test ');

Results

2. Statistics Directory Size

/* Statistics Directory Size * @param string $dirName directory name * @return double */function dirsize ($dirName) {   $dir _size = 0;   if ($dir _handle = @opendir ($dirName))   {while     ($fileName = Readdir ($dir _handle))     {/       * exclude two special directories *       / if ($fileName! = '. ' && $fileName! = ' ... ')       {         $subFile = $dirName. ' /'. $fileName;         if (Is_file ($subFile))         {           $dir _size + = FileSize ($subFile);         }         if (Is_dir ($subFile))         {           $dir _size + = dirsize ($subFile)         ;     }} Closedir ($dir _handle);     return $dir _size;   } }/* Pass the test directory under the current directory */$dir _size = Dirsize ('./test '); The echo './test directory file size is: '. Round ($dir _size/pow (1024,1), 2). ' KB ';

Results:

3. Delete Directory

/** Delete directory * @param string $dirName directory name */function Deldir ($dirName) {   /*php mkdir functions can create the directory */   if (file_exists ($ DirName)   {     if ($dir _handle = @opendir ($dirName))     {while       ($fileName = Readdir ($dir _handle))       {         /* Exclude two special directories *         /if ($fileName! = '. ' && $fileName! = ' ... ')         {           $subFile = $dirName. ' /'. $fileName;           if (Is_file ($subFile))           {             unlink ($subFile);           }           if (Is_dir ($subFile))           {             deldir ($subFile);}}       }       Closedir ($dir _handle);       RmDir ($dirName);       return $dirName. ' Directory has been deleted ';     }   

Delete the prompt message for success

4. Copy Directory

/* * Copy directory * @param string $DIRSRC The original directory name * @param string $dirTo The target directory name */function Copydir ($DIRSRC, $dirTo) {   if (Is_file ($dir To)   {     echo ' target directory cannot be created '; */target is not a */     return;   }   if (!file_exists ($dirTo))   {/     * directory does not exist then create *     /mkdir ($dirTo);   }   if ($dir _handle = @opendir ($DIRSRC))   {while     ($fileName = Readdir ($dir _handle))     {/       * exclude two special directories */       if ($fileName! = '. ' && $fileName! = ' ... ')       {         $subSrcFile = $dirSrc. ' /'. $fileName;         $subToFile = $dirTo. ' /'. $fileName;         if (Is_file ($subSrcFile))         {           copy ($subSrcFile, $subToFile);         }         if (Is_dir ($subSrcFile))         {           copydir ($subSrcFile, $subToFile);}}     }     Closedir ($dir _handle);     return $DIRSRC. ' Directory has been copied to '. $dirTo. ' directory ';   } } Echo Copydir ('./test ', '.. /testcopy ');



http://www.bkjia.com/PHPjc/885647.html www.bkjia.com true http://www.bkjia.com/PHPjc/885647.html techarticle PHP Common Technical text file operation and directory operations summary, a basic file operation of the basic operation of files are: file judgment, directory judgment, file size, reading and writing judgment, existence ...

  • 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.