PHP: File

Source: Internet
Author: User

<?php files: Folders and files, folders can also be called directories
Determine the file type:
Var_dump (FileType ("./test")); //Gets the file type, if the file is returned to files, if the directory is returned dir, can be used to determine the file folder
Var_dump (Is_dir ("./test")); //Determine if the given path is not a directory
Var_dump (Is_file ("./aa.txt")); //Determine if the given path is not a file
To determine file properties:
echo Date ("Y-m-d h:i:s", Fileatime ("./aa.txt")); //Get the last access time of the file
echo Date ("Y-m-d h:i:s", Filectime ("./aa.txt")); //Get File creation time
echo Date ("Y-m-d h:i:s", Filemtime ("./aa.txt")); //Get File modification time

echo Date ("y-m-d h:i:s");
echo filesize ("./aa.txt");//Get File size
Echo file_exists ("./aa.txt");//Determine if the file exists, there is return 1, there is no return 0
File path
1. Current directory:./or do not write
2. Parent directory:. /
3. Subordinate Directory: folder name/
4. Root directory:/
root directory/two-layer meaning: If it is represented in a PHP program: Hard Drive root directory
If it is represented in the Web page: the root directory of the server (localhost inside www)

Do file upload (avatar, picture), save the path in the database must write absolute path

echo $_server[' Document_root ']; //Fetch the root directory of the server WWW, this and the Web directory splicing can be changed to the absolute directory of this tag

Echo basename ("./aa.txt"); //Get filename with suffix from path

Echo basename ("Aa.txt", ". txt"); //Returns a file name without a suffix
Echo dirname ("./test/bb.txt");//Returns the directory in the path


Var_dump (PathInfo ("./test/bb.txt"));//return file information as an array


Echo Realpath ("./test/bb.txt");//convert relative path to absolute path

Operation of the Directory

Var_dump (Glob ("./test/*"));//returns all files in the directory, * represents all


Var_dump (Glob ("./test/1/*.txt"));//Filter by suffix


Recursion: Traverse directory the number of files;

EchoShuliang ("./test");functionShuliang ($filename)//have a return value so you need a parameter {if(Is_dir($filename) //To determine if the file is not a directory, is to continue not to enter else if () {$attr=(Glob($filename." /*")); //Go here to prove that Dir returns all files in the directory in two-dimensional array mode $SL=0; foreach($attr  as $v)//traverse the two-dimensional array {if(Is_dir($v)///re-determine if the directory is not; $SL=$SL+shuliang ($v);            is the directory call itself and the value + $SL } Else            {                $SL++;        //Not a directory is a file, the number of files +1 } } return $SL;           //Loop End return $SL } Else if(Is_file($filename) //Not the directory is the file directly returned 1, because the input path is the path of a file so there is only one file so return 1 The current code is this meaning {return1; }    Else    {        return0; }}

Traverse the Directory The second way:
$dir =opendir ("./test"); //Open Directory resource, there is turn off, so be sure to remember to add close directory resources
echo Readdir ($dir), "<br/>";//Read files,. Represents the current directory
echo Readdir ($dir), "<br/>";//.. Parent Directory
echo Readdir ($dir), "<br/>";//the third one starts.

While loop
while ($v =readdir ($dir))//
{
if ($v = = "." | | $v = = "..") //Shaved root directory and parent directory
{

}
Else
{
echo $v. " <br/> ";
}
}
Rewinddir ();//Reset the directory resource pointer

Close Directory Resource
Closedir ($dir); Close Directory Resource

?>

PHP: File

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.