How to use PHP file function to obtain file information

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags access based basic code copy create create a file date

This article mainly describes how to use PHP file functions to obtain file information. First, let's take a look at the basic introduction to PHP file functions

The difference between the dirname () and basename () PHP file functions

dirname () gets the directory portion of the file directory path and basename () gets the file name without the directory

How to get the file basic information through PHP file function?

Before introduced the PHP directory read instance, we only need to traverse the directory (folder) function listSubDir based on the PHP file information function can be. listSubDir function Please refer to the PHP traversal directory (folder) instance.

1, add getFileInfo function in the following location, the output file information

<?
if (is_file ($ newDir)) {
echo $ dirC. $ subFile. ": file attribute";

getFileInfo ($ newDir);
}
?>

2, use the PHP file function to obtain file information function instance code


function getFileInfo ($ fileInfo)
{
echo "file directory information:";

echo "file type:". filetype ($ fileInfo). "";
echo "file size:". filesize ($ fileInfo). "";

echo "Last accessed:". date ("j FYH: i", fileatime ($ fileInfo)). "";
echo "Last Modified:". date ("j FYH: i", filemtime ($ fileInfo)). "";
echo "is an executable file:". (is_executable ($ fileInfo)? "is": "no"). "";
echo "is a link:". (is_link ($ fileInfo)? "is": "no"). "";
echo "is readable:". (is_readable ($ fileInfo)? "is": "no"). "";
echo "is writable:". (is_writable ($ fileInfo)? "is": "no"). "";
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, modify time, file readable and writable properties.


Knowledge point:

1, filetype function: Get the file type, such as the successful implementation of the function returns one of the following values, otherwise it returns False. Seven possible values: fifo, char, dir, block, link, file, unknown

2, filesize function: Get the file size, calculated in bytes

3, fileatime function: the file last access time, filemtime function: the file last modified time. The time stamps returned by these two PHP file functions are similar to UNIX timestamps, so they need to be formatted with the Date function.

4, is_executable, is_link, is_readable, is_writable: These four PHP file functions respectively return the file is executable, whether the link is readable, is writable.

Note: PHP file status functions are time-consuming to run, so they are automatically cached; if you want to clear the cache, you can use the clearstatcache () function to get the latest file information.
To put it in a nutshell, the file information obtained by these PHP file functions is also available via the stat ($ file) function, which returns all the details of the file as an array. You can use the output of the print_r function to view it.

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

These functions correspond to the file name parameter, 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 bring the specific file path.

How to create a file

Method One: Usually use the PHP file manipulation function Fopen to write mode to open the file, and then write the content and save, so a file is created. How to write PHP files?

Method two: use the PHP file operation function touch create, touch function has three parameters: create a file name, access time, modify the time, including access time and modification time is optional, the default value is the current system time. If the specified file does not exist, it will be automatically created. Through the touch function you can modify the recent access to create the file and modify the time, these two parameters are UNIX timestamp, you need to convert through the mktim function.

PHP file function to move and copy files

Move files can be renamed function, copy the file can use the copy function, the difference between these two PHP file manipulation functions is that the rename function is to move the old file to a new directory, and the copy function as the name suggests is to copy the old file to a new directory , The source directory of the file is still there.

PHP delete files

In the previous PHP directory function article, I said that the directory must be empty when you delete the directory, when the directory has files, we must delete the file to delete the directory, then delete the file What is the way?

One way is through unlink PHP file function, another method is through the system function to perform system commands, such as the implementation of the delete file in the WINDOWS system.

The following is an example of code for creating, moving, copying and deleting files:


<? php
$ fileDir = "leapsoulcn / fileinfo.txt";

touch ($ fileDir, mktime (12,55,55,4,10,2000), mktime (12,55,55,4,10,2005));

// Delete the file in two ways, please use a cancellation method
system ("del leapsoulcnfileinfo.txt");

unlink ($ fileDir);

// Move the file
rename ("leapsoulcn / newfileinfo.txt", "leapsoulcn / php / fileinfo.txt");

// copy the file
copy ("leapsoulcn / php / fileinfo.txt", "leapsoulcn / newfileinfo.txt");
?>

This tutorial is very suitable for PHP beginners to learn to use.

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.