Fileinfo 是一個(gè) PHP 庫,用于獲取文件的元數(shù)據(jù)信息,如文件類型、大小、創(chuàng)建時(shí)間等。要使用 Fileinfo 進(jìn)行文件操作,首先確保已經(jīng)安裝了這個(gè)擴(kuò)展。然后,你可以使用以下步驟進(jìn)行文件操作:
require_once 'vendor/autoload.php';
use phpDocumentor\FileInfo\FileInfo;
這里假設(shè)你已經(jīng)使用 Composer 安裝了 Fileinfo 庫。如果沒有安裝,可以使用以下命令安裝:
composer require phpdocumentor/fileinfo
$file = new FileInfo('path/to/your/file');
$type = $file->mimeType(); // 獲取文件類型
$size = $file->size(); // 獲取文件大小
$mtime = $file->lastModified(); // 獲取文件最后修改時(shí)間
if ($type == 'image/jpeg') {
echo 'This is a JPEG image.';
} elseif ($type == 'image/png') {
echo 'This is a PNG image.';
} else {
echo 'Unknown file type.';
}
if ($size < 1024) {
echo 'This file is less than 1KB.';
} elseif ($size < 1024 * 1024) {
echo 'This file is less than 1MB.';
} else {
echo 'This file is larger than 1MB.';
}
if ($mtime > time() - 7 * 24 * 60 * 60) {
echo 'This file was modified within the last week.';
} elseif ($mtime > time() - 30 * 24 * 60 * 60) {
echo 'This file was modified within the last month.';
} else {
echo 'This file was modified more than a month ago.';
}
注意:以上示例中的文件路徑需要替換為實(shí)際文件的路徑。