0.5.文件常用函数(2015.02.24)

xiaoxiao2021-02-28  36

1.basename — 返回路径中的文件名部分:

echo basename("/etc/sudoers.d", ".d");  //sudoersecho basename("/etc/passwd");  //passwd

2.dirname — 返回路径中的目录部分:

echo dirname("/etc/passwd");  //etc

3.rmdir — 删除目录,该目录必须是空的:

rmdir('examples');

4.unlink — 删除文件:

unlink('test.html');

5.rename — 重命名一个文件或目录:

rename("/tmp/tmp_file.txt""/home/user/login/docs/my_file.txt");

6.pathinfo — 返回文件路径的信息:

$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');echo $path_parts['dirname'];  //  /www/htdocs/incecho $path_parts['basename'];  //  lib.inc.phpecho $path_parts['extension'];  //  phpecho $path_parts['filename'];   //  lib.inc

7.filesize — 取得文件大小:

$filename = 'somefile.txt';echo filesize($filename);

8.filetype — 取得文件类型:

$filename = 'somefile.txt';echo filetype($filename);  // txtecho filetype('/etc/');        // dir

9.fopen — 打开文件或者 URL:

$handle = fopen("/home/rasmus/file.txt""r");

10.fread — 读取文件(可安全用于二进制文件):

$filename = "/usr/local/something.txt";$handle = fopen($filename, "r");$contents = fread($handlefilesize($filename));fclose($handle);

11.fwrite — 写入文件(可安全用于二进制文件):

$fp = fopen('data.txt', 'w');fwrite($fp, '1');fwrite($fp'23');fclose($fp);  //123

12.fclose — 关闭一个已打开的文件指针:

$handle = fopen('somefile.txt', 'r');fclose($handle);

13.feof — 测试文件指针是否到了文件结束的位置:

while (!feof($file)) {}

14.file_exists — 检查文件或目录是否存在:

$filename = '/path/to/foo.txt';if (file_exists($filename)) {    echo "The file $filename exists";}

15.is_dir — 判断给定文件名是否是一个目录:

echo is_dir('a_file.txt');

16.mkdir — 新建目录:

mkdir("/path/to/my/dir", 0700);

17.is_file — 判断给定文件名是否为一个正常的文件:

echo is_file('a_file.txt');

18.is_uploaded_file — 判断文件是否是通过 HTTP POST 上传的:

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {   echo "File uploaded successfully.";}

19.move_uploaded_file — 将上传的文件移动到新位置:

$uploads_dir = '/uploads';foreach ($_FILES["pictures"]["error"] as $key => $error) {    if ($error == UPLOAD_ERR_OK) {        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];        $name = $_FILES["pictures"]["name"][$key];        move_uploaded_file($tmp_name"$uploads_dir/$name");    }}

20.copy — 拷贝文件:

$file = 'example.txt';$newfile = 'example.txt.bak';if (!copy($file$newfile)) {    echo "failed to copy $file...\n";}

21.file_get_contents — 将整个文件读入一个字符串:

$homepage = file_get_contents('http://www.baidu.com/');echo $homepage;  //将会把百度输出来

curl— 可以模拟GET、POST等HTTP请求。功能比file_get_contents 强大,效率比file_get_contents 高四倍。

22.file_put_contents — 将一个字符串写入文件:

$file = 'people.txt';$current = file_put_contents($file,"hello world!");

23.file — 把整个文件读入一个数组中:

// 将一个文件读入数组。本例中通过 HTTP 从 URL 中取得 HTML 源文件。$lines = file('http://www.example.com/'); // 将一个文件输出。本例中通过 HTTP 从 URL 中取得 HTML 源文件。$strs = readfile('http://www.example.com/');echo $strs;

24.parse_ini_file — 解析一个配置文件:

$ini_array = parse_ini_file("sample.ini");print_r($ini_array);

25.获取文件时间:

fileatime — 取得文件的上次访问时间filectime — 取得文件的上次权限修改时间filemtime — 取得文件的上次内容修改时间

$filename = 'somefile.txt';echo date("Y-m-d H:i:s", fileatime($filename));echo date("Y-m-d H:i:s", filectime($filename));echo date("Y-m-d H:i:s", filemtime($filename));

26.realpath — 返回规范化的绝对路径名:

echo realpath('/windows/system32');  //C:\WINDOWS\System32

27.stat — 给出文件的信息:

$stat = stat('C:\php\php.exe');var_dump($stat);

28.disk_total_space — 返回一个目录的磁盘总大小:

// $df 包含 "/" 目录的磁盘大小$ds = disk_total_space("/");//在 Windows 下:$ds = disk_total_space("C:");

29.disk_free_space — 返回目录中的可用空间:

//在 Windows 下:$df_c = disk_free_space("C:");// $df 包含根目录下可用的字节数$df = disk_free_space("/");

转载请注明原文地址: https://www.6miu.com/read-2612707.html

最新回复(0)