Table of Contents
rmdir
file_put_contents
file_get_contents
unlink
rename
copy
Examples and Comments
Note
Home Backend Development PHP Tutorial Detailed introduction to php file processing functions (with examples)

Detailed introduction to php file processing functions (with examples)

Jan 10, 2019 am 11:31 AM
php

This article brings you a detailed introduction to PHP file processing functions (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

PHP can easily operate directories and files, including creating, reading, modifying, deleting, etc.

mkdir

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
Try to create a new directory specified by pathname.

mkdir can create a directory specified by pathname. The default mode is 0777, which is ignored under windows and returns false on failure.

<?php
mkdir(&#39;./test&#39;);// 在当前目录创建 test 目录
Copy after login

rmdir

bool rmdir ( string $dirname [, resource $context ] )
Try to delete the directory specified by dirname. The directory must be empty and must have appropriate permissions. Failure will generate an E_WARNING level error.

As shown above, rmdir can delete the directory. It should be noted that the directory must be empty and must have permissions . If it fails, false will be returned. Example

<?php
rmdir(&#39;./test&#39;);// 删除当前目录下的 test 目录。
Copy after login

file_put_contents

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
has the same function as calling fopen(), fwrite() and fclose() in sequence.

file_put_contents Write data into the filename file. If there is no such file, create it. If it fails, it returns false. If it succeeds, it returns the number of bytes written. Example

<?php
file_put_contents(&#39;./test.txt&#39;, date(&#39;Y-m-d H:i:s&#39;));// 在当前目录创建 test.txt,并且写入数据
file_put_contents(&#39;./test.txt&#39;, date(&#39;Y-m-d H:i:s&#39;), FILE_APPEND);// 在 test.txt 文件中,追加数据
Copy after login

file_get_contents

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )
Same as file(), except file_get_contents() reads the file into a string. The content with a length of maxlen will be read starting from the position specified by the parameter offset.

file_get_cntents reads the content in filename and returns a string. If it fails, it returns false. Example

<?php
echo file_get_contents(&#39;./test.txt&#39;);// 输出当前目录下 test.txt 的内容
Copy after login
bool unlink ( string $filename [, resource $context ] )
Delete filename. Similar to Unix C's unlink() function. An E_WARNING level error is generated when an error occurs.

unlink delete filename file, also need to pay attention to permissions. Example

<?php
unlink(&#39;./test.txt&#39;);// 删除当前目录的 test.txt
Copy after login

rename

bool rename ( string $oldname , string $newname [, resource $context ] )
Try to rename oldname to newname.

rename can not only rename files, but also move files. If it fails, it returns false. Example

<?php
rename(&#39;./test.txt&#39;, &#39;./test1.txt&#39;);// 当前目录下的 test.txt 重命名为 test1.txt
rename(&#39;./test1.txt&#39;, &#39;./test/test.txt&#39;);// 将当前目录下的 test1.txt, 移动到 test下
Copy after login

copy

bool copy ( string $source , string $dest [, resource $context ] )
Copy files from source to dest.

As shown above, false is returned on failure. Examples

<?php
copy(&#39;./test/test.txt&#39;, &#39;./test1.txt&#39;);// test 目录下的test.txt, 拷贝到当前目录
Copy after login

Examples and Comments

Here are a few examples that will be used at work or in interviews.

<?php

/**
 * 遍历目录下所有文件
 * @param $path 
 */
function getAllFiles($path)
{
    if (!is_dir($path)) {
        exit(&#39;错误&#39;);
    }
    echo &#39;<ul>';
    foreach (scandir($path) as $line) {
        if ($line == '.' || $line == '..') {
            continue;
        }

        if (is_dir($path . '/' . $line)) {
            getAllFiles($path . '/' . $line);
        }
        echo '<li>' . $path . '/' . $line . '</li>';
    }
    echo '</ul>';
}

/**
 * 删除目录下所有文件
 * @param $path
 */
function delAllFile($path)
{
    if (!is_dir($path)) {
        exit('目录不存在');
    }

    $dir = opendir($path);
    while ($filename = readdir($dir)) {
        if ($filename != "." && $filename != "..") {
            $file = $path . "/" . $filename;
            if (is_dir($file)) {
                delAllFile($file);
            } else {
                unlink($file);
            }
        }
    }
    closedir($dir);
    rmdir($path);
}
Copy after login

Note

  • The recursive parameter in mkdir de can create directories nested;

  • the flags parameter in file_put_contents , can be combined, refer to the link for details;

  • file_put_contents may also return a non-Boolean value equivalent to false, use === to judge;

  • file_get_contents You can also open a URL to get web page content;

  • file_get_contents If you want to open a URL with special characters (for example, spaces), you need to use urlencode( ) URL encoding;

  • copy If the target file already exists, it will be overwritten;

The above is the detailed content of Detailed introduction to php file processing functions (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles