Home  >  Article  >  Backend Development  >  PHP file and directory operations

PHP file and directory operations

不言
不言Original
2018-07-04 16:53:364818browse

This article mainly introduces the file and directory operations of PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

php file directory operations

  • Directory operation

    • ##is_dir ( $path ) determines whether the current path is a directory and returns a Boolean

    • opendir ( $path ) opens the path directory and returns the resource

    • readdir ( $handle ) reads the next file in the currently opened directory, and at the same time Move the pointer forward one bit and return a string (file/directory name)

    • closedir ( $handle ) Close the currently open directory and return a Boolean

    • getcwd () Get the current working directory

    • rmdir Delete the directory. Before deleting, you must first delete all files and directories in the directory

Code: List all files and file names in the specified directory

function traversal_dir($path, $deep = 0) {    
if (is_dir($path)) {        
$handle = opendir($path);        
while (($file = readdir($handle)) !== false) {            
if ($file == '.' || $file == '..') {               
 continue;
            }            
            echo str_repeat(&#39;-&#39;, 2 * $deep) . $file . &#39;</br>&#39;;            
            if (is_dir($path . &#39;/&#39; . $file)) {
                traversal_dir($path . &#39;/&#39; . $file, $deep + 1);
            }
        }
    }
}

traversal_dir(&#39;./&#39;);

  • File operations

    • is_file ($path): Determine whether the specified path is a file

    • ##file_exists ($path): Check whether the directory or file exists

      ##fopen ($file):
    • Open the file or URL and return the resource
    • fread (

      resource $handle , int $length ): Read the file, the length can be specified

    • ##fwrite ( resource $handle , string $string [, int $length ] ): Returns the size of the written string. If length is specified, when length bytes are written or After writing string, writing will stop, depending on which situation is encountered first.

    • fgets ( resource $handle [, int $length ] ): Read a line of text, length specifies the length of a line of text ##fclose (

      resource
    • $handle
    • ) : Close the file

      basename ($path): Returns the file name part of the specified path Returns String

    • dirname ($path) : Returns the directory name part of the specified path Returns string
    • Path part
    • Operation part
    • stat Get file information
    • ##Judgment part

    • #filesize ( $path ) Get the file size int

    • filetype ( $path ) gets the file type string (possible values: fifo, char, dir, block, link, file and unknown)

    • rename ( string $oldname

      ,
    • string

      $newname [, resource $context ] ) Rename or move Return Boolean

    • unlink ( $path ) 删除文件  返回布尔

    • file_get_contents 将整个文件读如一个字符串

    • file_put_contents 将一个字符串写入文件

  代码:每执行一次文件,向文件头部追加 Hello word

$path = &#39;./hello.txt&#39;;
if (!file_exists($path)) {    
$handle = fopen($path, &#39;w+&#39;);    
fwrite($handle, &#39;Hello word&#39; . &#39;\r\n&#39;);    
fclose($handle);
} else {    
$handle = fopen($path, &#39;r&#39;);    
$content = fread($handle, filesize($path));    
$content = &#39;Hello word \r\n&#39; . $content;    
fclose($handle);    
$handle = fopen($path, &#39;w&#39;);    
fwrite($handle, $content);    
fclose($handle);
}

代码:遍历删除文件夹及文件夹下所有文件

function traversal_delete_dir($path) {    
if (is_dir($path)) {        
$handle = opendir($path);        
while (($file = readdir($handle)) !== false) {            
if ($file == &#39;.&#39; || $file == &#39;..&#39;) {                
continue;
            }            
            if (is_dir($path . &#39;/&#39; . $file)) 
            {
                traversal_delete_dir($path . &#39;/&#39; . $file);
            } else {                
            if (unlink($path . &#39;/&#39; . $file))
             {                    
            echo &#39;删除文件&#39; . $file . &#39;成功&#39;;
                }
            }
        }        
        closedir($handle);        
        rmdir($path);
    }
}

traversal_delete_dir(&#39;./shop_api&#39;);

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

 php的双向队列代码

php生成xml数据的方法

The above is the detailed content of PHP file and directory operations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP two-way queue codeNext article:PHP two-way queue code