PHP--File operations Miscellaneous

Source: Internet
Author: User
Tags glob

<title>php– File Operations Miscellaneous</title> php– File Operations Miscellaneous Table of Contents
    • Create a file
    • Generate a unique name
    • mkdir Creating a Directory
    • Traverse Directory
Create a file

Functions used: basename, file_exists, Touch

function CreateFile($filename) {// Verify that the file name is legal    $pattern="/[\/, \*, <>, \?, \|] /";// * Add no ' \ ' does not affect, because [] inside only-and ^ have special meaning    if(Preg_match ($pattern, BaseName ($filename))) {return ' Illegal file name '; }Else{// do you have a file with the same name        if(File_exists ($filename)) {return ' file exists '; }Else{if(Touch ($filename)) {// chmod o+w Write permission to directory                return ' Create success '; }Else{return ' Create failed '; }        }    }}
Generate a unique name
$uniName = MD5 (Uniqid (Microtime (True), true));
mkdir Creating a Directory
$ Path = dirname (__file__). '/upload/'; if (!file_exists ($path)) {    mkdir ($pathtrue);   True indicates that the meaning of mkdir-p can be created recursively    chmod ($path, 0777);         Although the above set permissions, but Umask has affected }
Traverse Directory

Opendir, Readdir, Is_file, Is_dir, Closedir

function readdirectory($Path) {if($Path[Strlen ($Path)-1] = ='/') {// remove the last slash        $Path= substr ($Path, 0,-1); }$Handle= Opendir ($Path)or  die(' could not open file '); while(($Item= Readdir ($Handle)) !==false) {if($Item=="."||$Item=="..") {Continue; }if(Is_file ($Path.'/'.$Item)) {// Is_file            $arr[' file '][] =$Item; }if(Is_dir ($Path.'/'.$Item)) {// Is_dir            $arr[' dir '][] =$Item; }} closedir ($Handle);return $arr;}

scandirReturns the file in the current directoryls

$ files1 = Scandir ($dir); $ files2 = Scandir ($dir, 1);
Array (    [0] = =.    [1] = =    . [2] = = bar.php    [3] = Foo.txt    [4] = Somedir) Array (    [0] = Somedir    [1] = Foo.txt
   [2] = bar.php    [3] = =.    [4] = =.)

Glob, only the current directory is listed with the wildcard character

foreach (Glob ("*.txt"as$filename) {    echo$filename ' <br> ';}

Funclist.txt
Funcsummary.txt
Quickref.txt

PHP--File operations Miscellaneous

Related Article

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.