PHP directory and file Operation ____php

Source: Internet
Author: User

a file function library

1, the ReadFile () function reads the file and writes it to the cache.

Create a new 01.txt, write something

PHP ReadFile


Create a PHP file


<?php
Echo ReadFile ("01.txt");
? >

2, fopen open the file

The first parameter of the fopen () function represents the file to manipulate, and the second parameter represents the mode in which the file is opened.


<?php
$file = fopen ("01.txt", "R") or Die ("An error occurred.") ");
Echo fread ($file, FileSize ("01.txt"));
Fclose ($file);
? >

Mode Description
R Open the file as read-only. The file pointer begins at the beginning of the file.
W Open the file as write-only. Delete the contents of the file or create a new file if it does not exist. The file pointer begins at the beginning of the file.
A Open the file as write-only. Existing data in the file will be preserved. The file pointer starts at the end of the file. Create a new file if the file does not exist.
X Create a new file as write-only. Returns false and error if the file already exists.
r+ Opens the file as read/write, and the file pointer begins at the beginning of the file.
w+ Open the file for read/write. Delete the contents of the file or create a new file if it does not exist. The file pointer begins at the beginning of the file.
A + Open the file for read/write. The data already in the file will be preserved. The file pointer starts at the end of the file. Creates a new file if it does not exist.
x+ Create a new file for read/write. Returns false and error if the file already exists.


3, Fread () read the file

Fread () The first parameter represents the file to be read (the pointer to fopen ()), and the second parameter sets the maximum number of bytes to be read.


4, fclose () Close the file

Close files to save computer resources


5, fgets () read a single file

When the fgets () function is invoked, the file pointer moves to the next line


6, feof () traversing the unknown length of the data is useful.


7, the Fget () function is used to read but characters from a file.

The file pointer is automatically moved down


8, Fwirte () write file

The first parameter represents the file to write, and the second parameter represents the content to be written.

9, file_get_contents () the entire file read into the string.


second, the directory operation

1. Directory reading, Opendir (), Readdir (), Closedir ()


<?php
$dir = Opendir ("filedir/");
while ($filelist = Readdir ($dir)) {
	echo $filelist. "<br/>";
}
Closedir ($dir);
? >

DirName ($path) return to the File directory section


BaseName ($path) return file name section


Disk_free_space ($path) returns the size of the partition on which the file resides


2.php Copy a directory operation

<?php
function Copydir ($sourceDir, $destDir)
{
    if (!is_dir ($sourceDir)) {return
        false;
    }

    if (!is_dir ($destDir)) {
        if (!mkdir ($destDir)) {return
            false;
        }
    }

    $dir = Opendir ($sourceDir);
    if (! $dir) {return
        false;
    }

    while (False!== ($file =readdir ($dir)) {
        if ($file!= '. ' && $file!= ' ... ') {
            if (is_dir $sourceDir. ' /'. $file)) {
                if (!copydir ($sourceDir.) /'. $file, $destDir. ' /'. $file)) {return
                    false;
                } 
            } else {
                if (!copy $sourceDir. ' /'. $file, $destDir. ' /'. $file)) {return
                    false;
    }}}} Closedir ($dir);
    return true;
}

Copydir ('./test1 ', './test2 ');


3.php Delete a directory

<?php
function Deldir ($dir)
{
    $DH = Opendir ($dir);
    while ($file = Readdir ($DH))
    {
        if ($file!= '. ' && $file!= ' ... ') {
            $fullpath = $dir. '/'. $file;
            if (!is_dir ($fullpath)) {
                unlink ($fullpath);
            } else {
                deldir ($fullpath);
    }}} Closedir ($DH);

    if (RmDir ($dir)) {return
        true;
    } 

    return false;
}

Deldir ('./test2 ');








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.