PHP commonly used file operation function reference table_PHP tutorial

WBOY
Release: 2016-07-20 11:02:23
Original
916 people have browsed it

Keep reading and you’ll discover something new!

Table a

Function

Description

Example

filesize

($file)

This function returns the size of a file in bytes. It is used to calculate how much space a file occupies on disk.

// get file owner and group
echo "uid: " . fileowner('myfile.html');
echo "gid: " . filegroup('myfile.html');
?>

fileatime

($file)

filemtime

($file)

These functions return the last time a file was accessed and modified respectively, and are used to find out whether a file has been modified after a specific date.

// get file access/modification times
echo "last accessed on: " . date("d-m-y", fileatime('myfile.html'));

echo "last modified on: " . date("d-m-y", filemtime('myfile.html'));
?>

fileperms

($file)

This function returns a file permission, use it to check whether the file is readable, writable or executable.

// get permissions in octal format
echo "file permissions: " . sprintf('%0', fileperms('myfile.html'));
?>

filetype

($file)

This function returns the "type" of the file - whether it is a connection, directory, attribute or block device, or a regular file. Use it to verify the nature of a file before performing an operation.

// get file type
echo "file type: " . filetype('myfile.html');
?>

stat($file)

This is a "catch-all" function that returns detailed statistics of a file, including its owner and group, size, last modification time, and number of index nodes. If you need comprehensive file statistics in a single function call, use this function instead of those listed previously.

// get file statistics
print_r(stat('myfile.html'));
?>

realpath

($file)

This function converts a relative file path to an absolute file path. This function is used when you need to find out the exact location of a file on the disk.

// get absolute path
// returns "/tmp/myfile.html"
echo "file path: " . realpath("./cook/book/http://www.cnblogs.com/myfile.html");
?>

basename

($file)

dirname

($file)

Given a complete file path, these functions break it into its component parts and return the file name and directory respectively.

// split directory and file names
// returns "/usr/local/bin"
echo "directory: " . dirname("/usr/local/bin/php");

// returns "php"
echo "file: " . basename("/usr/local/bin/php");
?>

file($file)

This function reads the contents of a file into an array. Each element in the array represents a line of file. This function is used to read the contents of the file into a variable for further processing.

// read file contents
$lines = file('myfile.html');

// print line by line
for($x=1; $x<=sizeof($lines); $x++) {
?echo "line $x: " . $lines[$x-1] . "n";
}
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445360.htmlTechArticleKeep reading and you will discover some new content! Table a Function Description Example filesize ($file) This function returns the size of a file in bytes. It is used to calculate the magnetic content of a file...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!