PHP File Basics

Source: Internet
Author: User

Files in PHP refer to Files and folders (directories)

I. Determining file types

1.filetype ("./aa.txt");//To determine the file type, if the file is returned files, if it is a directory, return dir

2.is_dir ("./test");//Determines whether the given path is not a directory, is the word return true, otherwise false

3.is_file ("./aa.txt");//Determine if the given path is not a file

Two. Get the properties of a file

1.echo date ("Y-m-d h:i:s", Fileatime ("./aa.txt"));//Gets the last access time of the file plus date is the format time

2.echo date ("y-m-d h:i: S", Filectime ("./aa.txt"));//Get File creation time

3.echo date ("Y-m-d h:i:s", Filemtime ("./aa.txt"));//Get File modification time

4.echo filesize ("./aa.txt");//Get the size of the file

5.var _dump (File_exists ("./aa.txt"));//Determine if a file exists, returns True, does not exist return false

Three. File path

1. Current path (directory):./or do not write

2. Parent directory:. /

3. Subordinate Directory: folder name/

4. Root directory:/

root directory/Two layer meaning: if/in PHP Program: Hard disk root directory. If/on the Web page: represents the root directory of the server (we are WWW)

5. Do file upload (avatar, product image), save the path of the data must write absolute path

6. $_server[' Document_toot '; The root directory of the zone server www

Four: Get the path

1. BaseName ("./aa.txt");//Get the file name from the path

2.basename ("./aa.txt", ". txt");//Returns a file name without a suffix

3.dirname ("./test/bb.txt");//Returns the directory in the path

4.pathinfo ("./test/bb.txt");//Return file information (array)

5.realpath ("./test/bb.txt");//convert relative path to absolute path (cannot go from path)

Four. Operation of the Directory

Traverse Directory

Glob ("./ueditor/*");//returns all files under Ueditor (directory)

Glob ("./ueditor/*.js");//Can be filtered by suffix

Get the number of files in a folder

<?PHPEchoShuliang ("./0524");functionShuliang ($filename){    if(Is_dir($filename))    {        $attr=Glob($filename." /*"); $SL= 0; foreach($sttr  as $v)        {            if(Is_dir($v))            {                $SL=$SL+shuliang ($v); }            Else            {                Echo $SL++; }                     }        return $SL; }            Else if(Is_file($filename))                {            return1; }        Else        {            return0; }    }?>

Mode two: Open Directory resources you can look up and you can go down.

//Open Directory Resource$dir=Opendir("./liucheng");//open Folder//readdir ($dir);//Read the file, pointing to the next one at a time. The point represents the current directory, ... Representative Injury and catalogue while($v=Readdir($dir)){    if($v= "."||"..")    {            }    Else    {        Echo $v." <br> "; }}Rewinddir($dir);//Reset A pointer to a directory resourceClosedir($dir);//Close Folder?>

PHP File Basics

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.