ZetCode

PHP 文件系统函数

最后修改于 2025 年 2 月 16 日

在本文中,我们将介绍 PHP 中的文件系统函数。我们将处理文件和目录,确定文件权限和可用磁盘空间,以及读写文件。对于我们的例子,我们使用 PHP CLI。 PHP 教程 是 ZetCode 上关于 PHP 语言的简明教程。

PHP 拥有一组丰富的文件和目录操作函数。PHP 包含低级和高级文件系统函数。fopen 函数是低级函数的示例;它是类似 C 函数的薄包装。 file 函数是高级 PHP 文件系统函数的示例。

PHP 文件大小和类型

filesize 函数返回给定文件的大小。大小以字节为单位指定。

get_filesize.php
<?php

$filename = "fruits.txt";

$fsize = filesize($filename);

echo "The file size is: $fsize bytes\n";

在示例中,我们确定了 fruits.txt 文件的大小。该文件位于当前工作目录中,即与 PHP 脚本相同的目录中。

$ php get_filesize.php
The file size is: 40 bytes

fruits.txt 文件的大小为 40 字节。

filetype 函数获取文件的类型。可能返回的值有:fifochardirblocklinkfilesocketunknown

file_types.php
<?php

echo filetype("myfile") . PHP_EOL;
echo filetype(".") . PHP_EOL;
echo filetype("/dev/tty1") . PHP_EOL;
echo filetype("/var/run/cups/cups.sock") . PHP_EOL;

脚本确定四个文件的类型。

$ php file_types.php
file
dir
char
socket

这四个文件是常规文件、目录、字符设备和套接字。

PHP 检查文件是否存在

我们可能想处理一个不存在的文件。可以使用 file_exists 函数来防止这种情况。

file_existence.php
<?php

if ($argc != 2) {

    exit("Usage: file_existence.php filename\n");
}

$filename = $argv[1];

$r = file_exists($filename);

if (!$r) {

    exit("Cannot determine the size of the file; the file does not exist\n");
}

$fsize = filesize($filename);

echo "The file size is: $fsize bytes\n";

该脚本在计算给定文件的大小之前,会检查它是否存在。

if ($argc != 2) {

    exit("Usage: file_existence.php filename\n");
}

$argc 是一个特殊变量,包含传递给脚本的参数数量。我们期望两个参数:一个脚本名称和另一个作为参数传递的文件名。

$filename = $argv[1];

$argv 是传递给脚本的参数数组。我们获取第二个元素。

$r = file_exists($filename);

if (!$r) {

    exit("Cannot determine the size of the file; the file does not exist\n");
}

我们使用 file_exists 函数检查文件是否存在。如果它不存在,我们使用一条消息终止脚本。

$ php file_existence.php fruits.txt
The file size is: 40 bytes

这是 file_existence.php 脚本的示例输出。

在下面的示例中,我们创建一个新文件,删除它,并检查它是否存在。touch 函数设置文件的访问时间和修改时间。如果文件不存在,则创建它。unlink 函数删除文件。

file_existence2.php
<?php

$filename = "newfile.txt";

if (file_exists($filename)) {

    echo "The file $filename exists\n";

} else {

    echo "The file $filename does not exist\n";

    $r = touch($filename);

    if (!$r) {

        exit("Failed to touch $filename file\n");
    } else {

        echo "The file $filename has been created\n";
    }
}


$r = unlink($filename);

if ($r) {

    echo "The file $filename was deleted\n";
} else {

    exit("Failed to delete $filename file\n");
}

if (file_exists($filename)) {

    echo "The file $filename exists\n";
} else {

    echo "The file $filename does not exist\n";
}

在代码示例中,我们使用了所有三个函数:file_existstouchunlink

$r = touch($filename);

touch 函数用于创建一个名为 newfile.txt 的新文件。

if (!$r) {

    exit("Failed to touch $filename file\n");
} else {

    echo "The file $filename has been created\n";
}

如果 touch 函数失败,则会打印一条错误消息。许多 PHP 函数在失败时返回 false 值。

$r = unlink($filename);

unlink 函数删除该文件。

$ php file_existence2.php
The file newfile.txt does not exist
The file newfile.txt has been created
The file newfile.txt was deleted
The file newfile.txt does not exist

这是 file_existence2.php 的输出。

PHP 复制和重命名文件

copy 函数复制文件,rename 重命名文件。如果目标文件已存在,它将被覆盖。

copy_file.php
<?php

$r = copy("myfile.txt", "myfile2.txt");

if ($r) {

    echo "Successfully copied file\n";
} else {

    exit("Failed to copy file\n");
}

该脚本复制一个文件。

rename_file.php
<?php

$r = rename("myfile2.txt", "myfile_back.txt");

if ($r) {

    echo "Successfully renamed file\n";
} else {

    exit("Failed to rename file\n");
}

在此脚本中,我们使用 rename 函数将 myfile2.txt 文件重命名为 myfile_back.txt

E_WARNING

某些文件系统函数在失败时会发出 E_WARNING。这是一个运行时警告(非致命错误)。脚本的执行不会停止。

PHP 在这方面不一致;并非所有文件系统函数都会发出此警告——大多数函数在失败时仅返回一个错误值。

custom_error_handler.php
<?php

set_error_handler("mywarning_handler", E_WARNING);

$r = unlink('image1.png');

if ($r) {

    echo "File successfully deleted\n";
}

function mywarning_handler($errno, $errstr) {

    echo "Failed to delete file\n";
    echo "$errstr: $errno\n";
}

在该脚本中,我们删除一个文件并提供一个自定义错误处理程序。

set_error_handler("mywarning_handler", E_WARNING);

自定义错误处理程序使用 set_error_handler 函数设置。

function mywarning_handler($errno, $errstr) {

    echo "Failed to delete file\n";
    echo "$errstr: $errno\n";
}

处理程序接收一个错误号和一个错误字符串作为参数。

$ php custom_error_handler.php
Failed to delete file
unlink(image1.png): No such file or directory: 2

当没有要删除的 image1.png 时,custom_error_handler.php 会给出此输出。

PHP 目录

dirname 函数返回父目录的路径。从 PHP 7 开始,我们可以提供一个可选的 levels 参数,该参数指定要上移的父目录的数量。

parent_directories.php
<?php

$home_dir = getenv("HOME");

echo dirname($home_dir). PHP_EOL;
echo dirname("/etc/") . PHP_EOL;
echo dirname(".") . PHP_EOL;
echo dirname("/usr/local/lib", 2) . PHP_EOL;

在该脚本中,我们打印四个目录的父目录。

$home_dir = getenv("HOME");

我们使用 getenv 函数获取当前用户的 home 目录。

echo dirname($home_dir). PHP_EOL;

这行打印用户的 home 目录的父目录。

echo dirname(".") . PHP_EOL;

在这里,我们打印当前工作目录的父目录。

echo dirname("/usr/local/lib", 2) . PHP_EOL;

在这行中,我们打印 /usr/local/lib 目录的第二个父目录。

$ php parent_directories.php
/home
/
.
/usr

这是 parent_directories.php 的输出。

getcwd 函数返回当前工作目录,chdir 函数将当前工作目录更改为新目录。

current_directory.php
<?php

$cd = getcwd();

echo "Current directory:" . $cd . PHP_EOL;

chdir("..");

$cd2 = getcwd();

echo "Current directory:" . $cd2 . PHP_EOL;

该脚本使用 getcmdchdir 函数。

$ php current_directory.php
Current directory:/home/janbodnar/prog/phpfiles
Current directory:/home/janbodnar/prog

PHP 列出目录

在以下五个示例中,我们列出了目录的内容。有几种方法可以执行此任务。

list_dir1.php
<?php

$folder = '/home/janbodnar/prog';
$fh = opendir($folder);

if ($fh === false) {

    exit("Cannot open directory\n");
}

while (false !== ($entry = readdir($fh))) {
    echo "$entry\n";
}

closedir($fh);

opendir 函数打开一个目录句柄。 readdir 函数从目录句柄中读取一个条目。该目录的句柄在脚本的末尾使用 closedir 函数关闭。

is_dir 函数判断文件名是否为目录,is_file 函数判断文件名是否为文件。

list_dir2.php
<?php

$folder = '/home/janbodnar/prog/';

$fh = opendir($folder);

if ($fh === false) {

    exit("Cannot open directory\n");
}

$dirs = [];
$files = [];

while (false !== ($entry = readdir($fh))) {

    if (is_dir($folder . '/' . $entry)) {

        array_push($dirs, $entry);
    }

    if (is_file($folder . '/' . $entry)) {

        array_push($files, $entry);
    }
}

echo "Directories:\n";

foreach ($dirs as $dr) {
    echo "$dr\n";
}

echo "Files:\n";

foreach ($files as $myfile) {
    echo "$myfile\n";
}

closedir($fh);

在第二个示例中,我们将条目分为子目录和文件。脚本首先打印子目录,然后打印所检查目录的文件。

if (is_dir($folder . '/' . $entry)) {

    array_push($dirs, $entry);
}

有必要向 is_dir 函数提供目录的完整路径。

glob 函数查找与模式匹配的路径名。

list_dir3.php
<?php

foreach (glob('/home/janbodnar/*', GLOB_ONLYDIR) as $dir) {

    echo "$dir\n";
}

使用 GLOB_ONLYDIR 标志,glob 函数仅返回与模式匹配的目录条目。

scandir 是一个高级函数,用于列出指定路径内的文件和目录。该函数返回目录中的文件和目录数组。

list_dir4.php
<?php

$files = scandir('.', SCANDIR_SORT_DESCENDING);

print_r($files);

该脚本打印当前工作目录的文件和子目录的数组。 SCANDIR_SORT_DESCENDING 标志按字母降序对条目进行排序。

在之前的示例中,我们仅列出了一个目录的内容;我们没有包含子目录的元素。使用 RecursiveDirectoryIteratorRecursiveIteratorIterator 类,我们可以使用递归轻松地遍历文件系统目录。换句话说,我们遍历所有子目录,直到列出目录树中的所有项目。

list_dir5.php
<?php

$folder = '/home/janbodnar/prog/';

$rdi = new RecursiveDirectoryIterator($folder);
$rii = new RecursiveIteratorIterator($rdi);

foreach ($rii as $filename) {

    echo "$filename\n";
}

该脚本打印给定目录在所有深度级别上的所有项目。

PHP 路径

路径是计算机文件的完全指定名称,包括文件在文件系统目录结构中的位置。realpath 函数返回一个规范的绝对路径名,basename 函数返回路径的尾部名称组件。

paths.php
<?php

echo realpath("myfile.txt") . PHP_EOL;

echo basename("/home/janbodnar/prog/phpfiles/myfile.txt") . PHP_EOL;
echo basename("/home/janbodnar/prog/phpfiles/myfile.txt", ".txt") . PHP_EOL;

该脚本使用 realpathbasename 函数。

echo basename("/home/janbodnar/prog/phpfiles/myfile.txt", ".txt") . PHP_EOL;

如果我们指定第二个参数,即后缀名,它也将从路径名中删除。

$ php paths.php
/home/janbodnar/prog/phpfiles/myfile.txt
myfile.txt
myfile

这是 paths.php 示例的输出。

pathinfo 函数返回有关文件路径的信息。

path_info.php
<?php

$path_parts = pathinfo('myfile.txt');

echo $path_parts['dirname'] . PHP_EOL;
echo $path_parts['basename'] . PHP_EOL;
echo $path_parts['extension'] . PHP_EOL;
echo $path_parts['filename'] . PHP_EOL;

该函数返回一个关联数组,其中包含以下元素:dirname、basename、extension(如果有)和 filename。

$ php path_info.php
.
myfile.txt
txt
myfile

PHP 创建文件

fopen 函数打开一个文件或 URL。该函数的第一个参数是文件名,第二个参数是我们打开窗口的模式。例如,'r' 模式仅用于读取,'w' 仅用于写入。如果我们以 'w' 模式打开一个文件,并且它不存在,则会创建它。可以在 fopen() 的 PHP 手册 中找到模式列表。

fopen 返回文件的句柄。这是一个用于操作文件的对象;例如,我们将其传递给 fwrite 函数以写入文件。

create_file.php
<?php

$filename = "names.txt";

if (file_exists($filename)) {

    exit("The file already exists\n");
}

$fh = fopen($filename, 'w');

if ($fh === false) {

    exit("Cannot create file\n");
}

echo "Successfully created file\n";

fclose($fh);

该示例创建了一个名为 names.txt 的新文件。

if (file_exists($filename)) {

    exit("The file already exists\n");
}

首先,我们检查该文件是否存在。

$fh = fopen('names.txt', 'w');

创建 names.txt 文件,并返回该文件的句柄。

fclose($fh);

我们使用 fclose 函数关闭文件句柄。

PHP 读取文件

在接下来的示例中,我们将读取文件内容。

fread 从句柄引用的文件指针中读取最多 length 字节。读取在已读取 length 字节或到达 EOF (文件结尾) 后立即停止。

read_file.php
<?php

$fh = fopen('balzac.txt', 'r');

if ($fh === false) {

    exit("Cannot open file for reading\n");
}

while (!feof($fh)) {

    $chunk = fread($fh, 1024);

    if ($chunk === false) {

        exit("Cannot read from file\n");
    }

    echo $chunk;
}

fclose($fh);

该示例使用 fread 函数读取整个文件,并将其输出到控制台。

while (!feof($fh)) {

    $chunk = fread($fh, 1024);

    if ($chunk === false) {

        exit("Cannot read from file\n");
    }

    echo $chunk;
}

feof 测试文件指针上的文件结尾。 fread 以 1 KB 块读取文件,直到到达 EOF。

$ php read_file.php balzac.txt
Honoré de Balzac, (born Honoré Balzac, 20 May 1799 – 18 August 1850)
was a French novelist and playwright. His magnum opus was a sequence
of short stories and novels collectively entitled La Comédie Humaine,
which presents a panorama of French life in the years after the 1815
Fall of Napoleon Bonaparte.

这是 read_file.php 示例的输出。

在第二个示例中,我们使用了 fgets 函数,该函数从文件句柄中读取一行。

read_file2.php
<?php

$fh = fopen('balzac.txt', 'r');

if ($fh === false) {

    exit("Cannot open file for reading\n");
}

while (!feof($fh)) {

    $line = fgets($fh);

    if ($line === false) {

        exit("Cannot read from file\n");
    }

    echo $line;
}

fclose($fh);

该示例逐行读取 balzac.txt 文件的内容。

file 是一个高级函数,它将整个文件读入一个数组。

read_file3.php
<?php

$lines = file('balzac.txt');

if ($lines === false) {

    exit("Cannot read file\n");
}

foreach ($lines as $line) {

    echo $line;
}

在此示例中,我们使用 file 函数一次读取整个文件。我们使用 foreach 循环遍历返回的数组。

file_get_contents 是另一个高级函数,它将整个文件读入一个字符串。

read_file4.php
<?php

$content = file_get_contents('balzac.txt');

if ($content === false) {

    exit("Cannot read file\n");
}

echo "$content";

该示例使用 file_get_contents 函数一次读取整个文件。它以字符串的形式返回数据。

PHP 读取格式化数据

fscanf 函数根据格式解析文件中的输入。每次调用 fscanf 时,都会从文件中读取一行。

$ cat items.txt
coins 5
pens 6
chairs 12
books 20

我们将解析 items.txt 文件。

read_formatted_data.php
<?php

$fh = fopen("items.txt", "r");

if ($fh === false) {

    exit("Cannot read file\n");
}

while ($data = fscanf($fh, "%s %d")) {

    list($item, $quantity) = $data;
    echo "$item: $quantity\n";
}

fclose($fh);

fscanf 函数使用格式说明符来读取字符串和数字。

PHP 读取网页

PHP 文件系统函数也可以用于读取网页。

read_page.php
<?php

$ph = fopen("https://webcode.me", "r");

if ($ph === false) {

    exit("Failed to open stream to URL\n");
}

while (!feof($ph)) {

    $buf = fread($ph, 1024);

    if ($buf === false) {

        exit("Cannot read page\n");
    }

    echo $buf;
}

fclose($ph);

我们从一个小网站 wecode.me 读取一个页面。

$ph = fopen("http://webcode.me", "r");

使用 fopen 函数,我们打开一个网页的句柄。

while (!feof($ph)) {

    $buf = fread($ph, 1024);

    if ($buf === false) {

        exit("Cannot read page\n");
    }

    echo $buf;
}

我们读取网页直到它的结尾;feof 用于测试网页的结尾。页面使用 fread 函数以 1 KB 块读取。

$ php read_page.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My html page</title>
</head>
<body>

    <p>
        Today is a beautiful day. We go swimming and fishing.
    </p>

    <p>
         Hello there. How are you?
    </p>

</body>
</html>

这是 read_page.php 脚本的输出。

以下示例使用高级函数读取同一网页。

read_page2.php
<?php

$page = file_get_contents('http://webcode.me');

if ($page === false) {

    exit("Cannot read page\n");
}

echo $page;

file_get_contents 一次读取整个网页。

fgetss 函数从文件句柄中读取一行并去除 HTML 标签。

read_page3.php
<?php

$ph = fopen("http://webcode.me", "r");

if ($ph === false) {

    exit("Failed to open stream to URL\n");
}

while (!feof($ph)) {

    $buf = fgetss($ph, 1024);

    if ($buf === false) {

        exit("Cannot read from page\n");
    }

    echo trim($buf);
}

fclose($ph);

我们读取 webcode.me 网页的内容并删除其 HTML 标签。

echo trim($buf);

trim 函数删除前导和尾随空格。

$ php read_page3.php
My html pageToday is a beautiful day. We go swimming and fishing.Hello there. How are you?

这是示例的输出;输出包含页面的标题和正文中的文本。

PHP 写入文件

fwrite 函数将字符串写入由文件句柄引用的文件。

write_file.php
<?php

$fh = fopen('names.txt', 'w');

if ($fh === false) {

    exit("Cannot open file\n");
}

$r = fwrite($fh, 'Jane' . PHP_EOL);
check_retval($r);

$r = fwrite($fh, 'Lucy' . PHP_EOL);
check_retval($r);

$r = fwrite($fh, 'Mark' . PHP_EOL);
check_retval($r);

$r = fwrite($fh, 'Lubos' . PHP_EOL);
check_retval($r);

fclose($fh);

function check_retval($val) {

    if ($val === false) {

        exit("Cannot write to file\n");
    }
}

我们以写入模式打开 names.txt 并向其中写入四行。

$fh = fopen('names.txt', 'w');

fopen 函数以写入模式打开文件。如果该文件不存在,则会自动创建它。

$r = fwrite($fh, 'Jane' . PHP_EOL);

使用 fwrite 函数,我们向文件写入一行。该函数将文件句柄作为其第一个参数。

$ php write_file.php
$ cat names.txt
Jane
Lucy
Mark
Lubos

我们写入 names.txt 文件并检查其内容。

我们可以使用高级的 file_put_contents 方法一次将字符串写入文件。

write_file2.php
<?php

$filename = "names.txt";

$buf = file_get_contents($filename);

if ($buf === false) {

    exit("Cannot get file contents\n");
}

$buf .= "John\nPaul\nRobert\n";

$r = file_put_contents($filename, $buf);

if ($r === false) {

    exit("Cannot write to file\n");
}

在该示例中,我们使用 file_get_contents 函数读取 names.txt 文件的内容,并使用 file_put_contents 函数附加新字符串。

PHP 可读、可写、可执行文件

is_readableis_writableis_executable 函数检查文件是否可读、可写和可执行。

rwe.php
<?php

$filename = "myfile.txt";

echo get_current_user() . PHP_EOL;

if (is_readable($filename)) {

    echo "The file can be read\n";
} else {

    echo "Cannot read file\n";
}

if (is_writable($filename)) {

    echo "The file can be written to\n";
} else {

    echo "Cannot write to file\n";
}

if (is_executable($filename)) {

    echo "The file can be executed\n";
} else {

    echo "Cannot execute file\n";
}

我们对 myfile.txt 文件运行这三个函数。该脚本检查当前用户的这些属性。

$ php rwe.php
janbodnar
The file can be read
The file can be written to
Cannot execute file

该文件对于 janbodnar 用户可以读取和写入,但不能执行。

PHP 文件时间

Linux 上有三种文件时间:最后访问时间、最后更改时间和最后修改时间。以下 PHP 函数确定这些时间:fileatimefilectimefilemtime

file_times.php
<?php

$filename = "myfile.txt";

$atime =  fileatime($filename);
$ctime =  filectime($filename);
$mtime =  filemtime($filename);

echo date("F d, Y H:i:s\n", $atime);
echo date("F d, Y H:i:s\n", $ctime);
echo date("F d, Y H:i:s\n", $mtime);

该脚本打印 myfile.txt 文件的文件时间。

$ php file_times.php
April 20, 2016 17:52:54
April 20, 2016 17:53:33
April 20, 2016 17:52:29

这是 file_times.php 脚本的示例输出。

PHP 文件权限

文件系统对不同用户和用户组的文件强制执行权限。fileperms 函数获取文件权限;它将文件的权限作为数字模式返回。

file_permissions.php
<?php

$perms = fileperms("myfile.txt");

echo decoct($perms & 0777) . PHP_EOL;

$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ?
            (($perms & 0x0800) ? 's' : 'x' ) :
            (($perms & 0x0800) ? 'S' : '-'));

$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ?
            (($perms & 0x0400) ? 's' : 'x' ) :
            (($perms & 0x0400) ? 'S' : '-'));

$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ?
            (($perms & 0x0200) ? 't' : 'x' ) :
            (($perms & 0x0200) ? 'T' : '-'));

echo "$info\n";

该脚本确定 myfile.txt 的文件权限。权限以 Unix 样式打印到控制台。

echo decoct($perms & 0777) . PHP_EOL;

Unix 上的权限传统上以八进制表示法编写。 decoct 函数将十进制表示法转换为八进制。

$info .= (($perms & 0x0100) ? 'r' : '-');

在此行中,我们检查文件权限是否允许文件所有者读取它。

$ php file_permissions.php
660
rw-rw----

这是 file_permissions.php 脚本的示例输出。

可以使用 chmod 函数更改文件权限。

file_permissions2.php
<?php

$perms1 = fileperms("myfile.txt");

echo decoct($perms1 & 0777) . PHP_EOL;

$r = chmod("myfile", 0660);

if ($r) {

    echo "File mode successfully changed\n";
} else {

    exit("Failed to change file mode\n");
}

$perms2 = fileperms("myfile");

echo decoct($perms2 & 0777) . PHP_EOL;


该脚本更改了 myfile.txt 文件的权限。

$r = chmod("myfile", 0660);

chmod 函数在其第二个参数中接受权限作为八进制值。八进制值以 0 开头。

$ php file_permissions2.php
664
File mode successfully changed
660

文件的权限从 664 更改为 660。

PHP CSV 文件格式

fgetcsv 函数从 CSV(逗号分隔值)文件中读取一行;它返回一个包含读取字段的索引数组。 fputcsv 函数将一行格式化为 CSV 并将其写入文件。

csv_output.php
<?php

$nums = [1, 2, 5, 3, 2, 6, 4, 2, 4,
    8, 7, 3, 8, 5, 4, 3];

$fh = fopen('numbers.csv', 'w');

if ($fh === false) {

    exit("Failed to open file\n");
}

$r = fputcsv($fh, $nums);

if ($r === false) {

    exit("Failed to write values\n");
}

echo "The values have been successfully written\n";

fclose($fh);

该脚本将数组中的数字写入 CSV 文件。

$ php csv_output.php
The values have been successfully written
$ cat numbers.csv
1,2,5,3,2,6,4,2,4,8,7,3,8,5,4,3

我们运行脚本并检查文件内容。

在下面的示例中,我们从 CSV 文件中读取数据。

csv_input.php
<?php

$fh = fopen('numbers.csv', 'r');

if ($fh === false) {

    exit("Failed to open file\n");
}

while (($data = fgetcsv($fh)) !== false) {

    $num = count($data);

    for ($i=0; $i < $num; $i++) {

        echo "$data[$i] ";
    }
}

echo "\n";

fclose($fh);

该脚本使用 fgetcsvnumbers.csv 文件中读取值,并将它们打印到控制台。

$ php csv_input.php
1 2 5 3 2 6 4 2 4 8 7 3 8 5 4 3

这是 csv_input.php 脚本的输出。

PHP 磁盘空间

disk_total_space 函数以字节为单位返回文件系统或磁盘分区的总大小,disk_total_space 函数以字节为单位返回文件系统或磁盘分区的可用空间。

disk_space.php
<?php

const BYTES_IN_GIGABYTE = 1073741824;

$total_space_bytes = disk_total_space("/");

if ($total_space_bytes === false) {

    exit("The disk_total_space() failed\n");
}

$free_space_bytes = disk_free_space("/");

if ($free_space_bytes === false) {

    exit("The disk_free_space() failed\n");
}

$total_space_gb = floor($total_space_bytes / BYTES_IN_GIGABYTE);
$free_space_gb = floor($free_space_bytes / BYTES_IN_GIGABYTE);

echo "Total space: $total_space_gb GB\n";
echo "Free space: $free_space_gb GB\n";

该脚本计算根分区上的总空间和可用空间。空间被转换为千兆字节。

$ php disk_space.php
Total space: 289 GB
Free space: 50 GB

来源

PHP 文件系统函数 - PHP 手册

在本文中,我们介绍了 PHP 文件系统函数。

作者

我的名字是 Jan Bodnar,我是一个充满激情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直在撰写编程文章。 迄今为止,我撰写了 1,400 多篇文章和 8 本电子书。 我拥有超过十年的编程教学经验。

列出所有 PHP 教程。