PHP Common Technical text file operation and catalog operation Summary _php Example

Source: Internet
Author: User
Tags mkdir pow readable

Operation of basic files

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"); * * Declares a function, passing in the filename gets the file attribute * @param string $fileName file name */function Getfilepro ($fileName) {if!file_exists ($fileName
     ) {echo ' file does not exist <br/> ';
   Return
   }/* is a normal file/if (Is_file ($fileName)) {echo $fileName. ' is a file <br/> ';
   }/* is directory */if (Is_dir ($fileName)) {echo $fileName. ' is a directory '; /* Output file type/echo ' file type is: '. Getfiletype ($fileName). '
   <br/> '; /* File size, conversion unit */echo ' file size is: '. GetFileSize (FileSize ($fileName)). '
   <br/> ';
   /* file is readable/if (is_readable ($fileName)) {echo ' file readable <br/> ';
   }/* file is writable/if (is_writable ($fileName)) {echo ' file writable <br/> ';
   }/* file can execute/if (is_executable ($fileName)) {echo ' file executable <br/> '; Echo ' File creation time: '. Date (' Y m month J Day ', Filectime ($fileName)). '
   <br/> '; Echo ' File last modified: '. Date (' Y m month J Day ', Filemtime ($fileName)). '
   <br/> '; Echo ' File last Open time: '. Date (' Y-M-month J-Day ', Fileatime ($fileName)). '
 <br/> ';
   }/* 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 ' blocks ': $type. = ' block device file ';
     Case ' char ': $type. = ' character device file ';
     Case ' filo ': $type. = ' pipe file ';
     Case ' link ': $type. = ' symbolic link ';
     Case ' unknown ': $type. = ' Unknown file ';
 Default:} return $type;
     * * * Declaration of a function, return file size * @param int $bytes File Bytes/function getfilesize ($bytes) {if ($bytes >= pow (2,40)) {
     $return = 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 function, incoming test.txt file in Test directory */Getfilepro ('./test/div.html ');?>

Results:

Ii. operation of the directory

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

1, traverse the directory

 * * Traverse directory * @param string $dirName directory name/function Finddir ($dirName) {$num = 0;/* Statistics Number of sub files * * $dir _handle = Open Dir ($dirName);
   /* Open Directory//////////////////////////////* <table border= "0" align= "center" width= "0" cellspacing= "0" > "
   echo ' <caption> 

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))
     {/
       * excluding 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 current directory of the test directory * * * *
 $dir _size = Dirsize ('./test ');
 The echo './test directory file size is: '. Round ($dir _size/pow (1024,1), 2). ' KB ';

Results:

3, delete the directory

*
* Delete directory *
@param string $dirName directory name
 /function Deldir ($dirName)
 {
   /*php mkdir functions to create a directory * /
   if (file_exists ($dirName))
   {
     if ($dir _handle = @opendir ($dirName))
     {while
       ($fileName = Readdir ($dir _handle))
       {/
         * excludes 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 '
     ;
 }} /* Pass a copy of the test directory test1*/
 Echo deldir ('./test1 ');
 

Delete a successful message

4. Copy Directory

/* Copy directory * @param string $DIRSRC The original directory name * @param string $dirTo The target directory name/function Copydi
   R ($DIRSRC, $dirTo) {if (Is_file ($dirTo)) {echo ' target directory cannot be created ';/* target is not a/return;
   if (!file_exists ($dirTo)) {/* directory does not exist create/mkdir ($DIRTO);
       } if ($dir _handle = @opendir ($DIRSRC)) {while ($fileName = Readdir ($dir _handle)) {/* excluding 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 '); 

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.