Use PHP file function to get file information instance Tutorial

Source: Internet
Author: User
Tags date execution file size file system file upload functions php file touch

The previous PHP directory function tutorial mainly introduced the directory (folder) Read, traversal method, but the most basic file management system involved in file information, file size read to check disk usage, but also need to get file modification or access time, delete or move the function of organizing files, These all need to interact with the file system through PHP file functions.

Introduction to basic PHP file functions

  The difference between PHP file Functions dirname () and basename ()

DirName () Gets the directory portion of the file directory path, and basename () gets the filename without the directory

How do I get basic file information through PHP file functions?

Before the introduction of the PHP directory to read the example, we just walk through the directory (folder) function Listsubdir based on the addition of PHP file information function. Listsubdir function Please refer to PHP Traversal directory (folder) instance.

1, add GetFileInfo function in the following position, output file information

1
2
3
4
5
6
7
?
if (Is_file ($newDir)) {
Echo $dirC. $subFile. " : File attributes <br/> ";

GetFileInfo ($newDir);
}
?>

2. Use php file function to get file information function instance code

1
2
3
4
5
6
7
8
9
Ten
One





            & nbsp      
      echo "File type:". FileType ($fileInfo). " <br/> ";
        echo "File size:". FileSize ($fileInfo). " <br/> ";

        echo "Recent access Time:". Date ("J F Y H:i", Fileatime ($fileInfo)). " <br/> ";
        echo "Last modified:". Date ("J F Y H:i", Filemtime ($fileInfo)). " <br/> ";
        ECHO is an executable file: ". (Is_executable ($fileInfo)? " Yes ": No"). " <br/> ";
        ECHO is a link: ". (Is_link ($fileInfo)? " Yes ": No"). " <br/> ";
        echo "readable:". (Is_readabLe ($fileInfo)? " Yes ": No"). " <br/> ";
        echo "is writable:". (Is_writable ($fileInfo)? " Yes ": No"). " <br/> ";
        echo "File absolute path": Realpath ($fileInfo);

        Clearstatcache ()
   }

Description

This code mainly through the PHP file function to obtain the following information: file type, file size, recent access, modified time, file readable writable properties.

Knowledge Points:

1. FileType function: Gets the file type, such as a function execution success, returns one of the following values, or false. Seven possible values: FIFO, Char, dir, block, link, file, unknown

2, FileSize function: To get the file size, calculated in bytes

3, Fileatime function: File recent access time, Filemtime function: File last modified time. These two PHP file functions return a timestamp similar to the UNIX timestamp, so they need to be formatted with the DATE function.

4, Is_executable, Is_link, is_readable, is_writable: These four PHP file functions to return whether the file is executable, link, whether readable, writable.

  Note : The php file state function is more time-consuming to run, so it will be automatically cached, and if you want to clear the cache, you can use the Clearstatcache () function to get the latest file information.

To say a digression, these PHP file functions to obtain the file information you can also be obtained through the stat ($file) function, which returns all the details of the file as an array, you can use the Print_r function output to see.

5, Realpath function: Returns the absolute path of the file, use this function when you need to find the exact location of the file on disk.

These functions correspond to file name parameters, if you want to get the file path and PHP code execution file is not in the same directory, please note that you need to take the specific file path.

How to create a file

  mode one : usually use PHP file action function fopen write mode to open the file, and then write the content and save, such a file is created. How does the php file write?

  mode two : Use the PHP file action function Touch to create, touch function has three parameters: created file name, access time, modified time, where access time and modification time is optional, the default value is the current system time. If the specified file does not exist, it is automatically established. The touch function allows you to modify the most recent access and modification time for creating files, both of which are UNIX timestamps, and you need to convert them using the Mktim function.

PHP file functions for moving and copying files

You can move files through the Rename function, copy a file can use the copy function, the two PHP file operation function is the difference is that the rename function is to move old files to the new directory, and the copy function as the name implies is to copy old files to the new directory, the source directory of files are still there.

How to delete files in PHP

In the previous PHP directory function article, I said that in the deletion of the directory must be empty directory, when there are files in the directory, we have to delete the file to delete the directory, then file deletion what method?

One way to do this is through the php file function unlink, and the other way is to execute system commands, such as the operation of the delete file under the Windows system.

The code instance for creating, moving, copying, and deleting files is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$fileDir = "Leapsoulcn/fileinfo.txt";

Touch ($fileDir, Mktime (12,55,55,4,10,2000), Mktime (12,55,55,4,10,2005));

Two ways to delete a file, please unregister a method when used
System ("Del leapsoulcn\fileinfo.txt");

Unlink ($fileDir);

Moving files
Rename ("Leapsoulcn/newfileinfo.txt", "leapsoulcn/php/fileinfo.txt");

Copying files
Copy ("Leapsoulcn/php/fileinfo.txt", "leapsoulcn/newfileinfo.txt");
?>

The use of PHP file functions to get file information, create, move, copy, delete the file method is introduced, combined with the introduction of the PHP file read and write tutorial, PHP directory function tutorial, php file Upload tutorial, should say that the basic PHP file function I have been fully involved, Very suitable for PHP beginners to learn to use.

  Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.







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.