filetype()
filetype($filepath);获取文件的类型;返回的是文件的类型
filepath
文件的路径fifo
,char
,dir
,block
,link
,file
和 unknown
FALSE
<?php
$file_path = 'sort.php';
$dir_path = '../0809';
var_dump(filetype($file_path)); // file
var_dump(filetype($dir_path)); // dir
复制代码
D:\php20190701\php_code\0810\filetype.php:6:string 'file' (length=4)
D:\php20190701\php_code\0810\filetype.php:7:string 'dir' (length=3)
复制代码
filesize()
filesize($filename);获取文件的大小;返回的是字节数;
FALSE
并生成一条 E_WARNING 级的错误<?php
$file_path = 'sort.php';
var_dump(filesize($file_path)); // 101字节
复制代码
filectime()
filectime($filename);获取文件的建立时间;返回的是时间戳;
FALSE
filemtime()
filemtime($filename);获取文件的修改时间;返回的是时间戳;
FALSE
fileatime()
fileatime($filename);获取文件的最后访问时间;返回的是时间戳;
FALSE
<?php
$file_path = 'sort.php';
var_dump(date('Y-m-d H:i:s',filectime($file_path)));
var_dump(date('Y-m-d H:i:s',filemtime($file_path)));
var_dump(date('Y-m-d H:i:s',fileatime($file_path)));
复制代码
D:\php20190701\php_code\0810\filetype.php:5:string '2019-08-10 10:52:53' (length=19)
D:\php20190701\php_code\0810\filetype.php:6:string '2019-08-10 11:15:33' (length=19)
D:\php20190701\php_code\0810\filetype.php:7:string '2019-08-10 10:52:53' (length=19)
复制代码
is_readable()
is_readable($filename);检测文件是否可读;返回布尔值;
TRUE
,不然返回 FALSE
is_writable()/is_writeable()
is_writable($filename)/is_writeable($filename);检测文件是否可写;返回布尔值;
TRUE
filename
要检查的文件名称<?php
$file_path = 'sort.php';
var_dump(is_readable($file_path));
var_dump(is_writable($file_path));
var_dump(is_writeable($file_path));
复制代码
D:\php20190701\php_code\0810\filetype.php:5:boolean true
D:\php20190701\php_code\0810\filetype.php:6:boolean true
D:\php20190701\php_code\0810\filetype.php:7:boolean true
复制代码
is_executable()
is_executable($filename);检测文件是否可执行;返回布尔值;
TRUE
,错误时返回FALSE
FALSE
表示文件不可执行;filename
文件的路径<?php
$file_path = "C:\Users\Administrator\AppData\Local\Programs\Microsoft VS Code\Code.exe";
var_dump(is_executable($file_path)); // true
复制代码
is_file()
is_file($filename);检测是否为文件;返回布尔值;
TRUE
,不然返回 FALSE
<?php
$file_path = 'sort.php';
$dir_path = '../0809';
var_dump(is_file($file_path)); // true
var_dump(is_file($dir_path)); // false
var_dump(is_dir($dir_path)); // true
复制代码
pathinfo()
pathinfo($filename);获取文件路径相关信息;返回一个关联数组;
echo pathinfo($filename,PATHINFO_EXTENSION);
-------获取文件扩展名__FILE__
表示文件的完整路径和文件名<?php
$file_path = 'sort.php';
var_dump(pathinfo($file_path)); // true
复制代码
D:\php20190701\php_code\0810\filetype.php:5:
array (size=4)
'dirname' => string '.' (length=1)
'basename' => string 'sort.php' (length=8)
'extension' => string 'php' (length=3)
'filename' => string 'sort' (length=4)
复制代码
dirname()
dirname($path);返回文件中的路径部分;
<?php
$file_path = 'C:\Users\Administrator\Downloads\Firefox Setup 69.0b11.exe';
$file_path2 = 'C:/Users/Administrator/Downloads/Firefox Setup 69.0b11.exe';
var_dump(dirname($file_path));
var_dump(dirname($file_path2));
复制代码
D:\php20190701\php_code\0810\filetype.php:6:string 'C:\Users\Administrator\Downloads' (length=32)
D:\php20190701\php_code\0810\filetype.php:7:string 'C:/Users/Administrator/Downloads' (length=32)
复制代码
basename()
basename($filename,$suffix);返回路径中文件名部分;
<?php
$file_path = 'C:\Users\Administrator\Downloads\Firefox Setup 69.0b11.exe';
var_dump(basename($file_path));
复制代码
D:\php20190701\php_code\0810\filetype.php:6:string 'Firefox Setup 69.0b11.exe' (length=25)
复制代码
<?php
$file_path = 'C:\Users\Administrator\Downloads\Firefox Setup 69.0b11.exe';
var_dump(basename($file_path,".exe")); // 'Firefox Setup 69.0b11'
复制代码
file_exists()
file_exists($filename);检测文件或者目录是否存在;返回布尔值;
TRUE
,不然返回 FALSE
<?php
$file_path = "sort.php";
$dir_path = "../0809";
$dir_path2 = "../080911";
var_dump(file_exists($file_path)); // true
var_dump(file_exists($dir_path)); // true
var_dump(file_exists($dir_path2)); // false
复制代码
touch()
touch($filename,$time,$atime); 设定文件的访问和修改时间,若是文件不存在,则会建立文件;返回布尔值
TRUE
, 或者在失败时返回 FALSE
unlink()
unlink($filename,$context);删除文件;返回布尔值;
TRUE
, 或者在失败时返回 FALSE
<?php
$file_path = "sort.php";
var_dump(unlink($file_path)); // true
复制代码
rename()
rename($oldname,$newname,$path);重命名或者剪切一个文件或目录;返回布尔值;
oldname
重命名为 newname
TRUE
, 或者在失败时返回 FALSE
<?php
$old_name = "sort.php";
$new_name = "sort2.php";
var_dump(rename($old_name,$new_name)); // true, 会把sort.php删掉, 新增一个sort2.php
复制代码
<?php
$old_name = "sort2.php";
$new_name = "../sort2.php";
var_dump(rename($old_name,$new_name)); // true, 实现剪切的效果
复制代码
copy()
copy($filename,$path);拷贝一个文件;返回布尔值;
TRUE
, 或者在失败时返回 FALSE
allow_url_fopen=On
<?php
$old_name = "sort.php";
$new_name = "sort2.php";
var_dump(copy($old_name,$new_name)); // true
复制代码
<?php
$old_name = "http://pic68.nipic.com/file/20150601/2692994_151045402000_2.jpg";
$new_name = "1.jpg";
var_dump(copy($old_name,$new_name)); // 下载
复制代码
fopen()
fopen(filename,mode)php
<?php
$file_path = "test.txt";
var_dump(fopen($file_path, 'r')); // resource
复制代码
fread()
string fread ( resource
length )html
fread() 函数读取打开的文件。数组
函数会在到达指定长度或读到文件末尾(EOF)时(以先到者为准),中止运行。bash
该函数返回读取的字符串,若是失败则返回 FALSE。函数
<?php
$file_path = "test.txt";
$file = fopen($file_path, 'r');
var_dump(fread($file,99)); // hello world !
复制代码
<?php
$file_path = "test.txt";
$file = fopen($file_path, 'r');
var_dump(filesize($file_path));
var_dump(fread($file,filesize($file_path))); // hello world !
复制代码
fwrite()|fput()
fwrite(file,string[,length])url
<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
复制代码
fclose()
fclose($file)spa
fclose() 函数关闭打开的文件。debug
该函数若是成功则返回 TRUE,若是失败则返回 FALSE。指针
<?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
复制代码
fgetc()
fgetc(file)code
<?php
$file = fopen("test.txt","r");
for ($i=0; $i < filesize('test.txt'); $i++) {
echo fgetc($file);
}
while (!feof($file)) {
echo fgetc($file);
}
复制代码
fgets()
fgets(file[,length])
复制代码
<?php
$file = fopen("test.txt","r");
while (!feof($file)) {
echo fgets($file);
}
复制代码
fgetss()
fgetss(file[,length[,tags]])
复制代码
fgetcsv()
fgetcsv(file[,length[,separator[,enclosure]]])
复制代码
<?php
$file = fopen("index.csv","r");
while ($arr = fgetcsv($file)) {
var_dump($arr);
}
fclose($file);
复制代码
fputcsv()
fputcsv(file,fields[,seperator[,enclosure]])
复制代码
<?php
$file = fopen("index.csv","r+");
$arr = [
'hello',
'world',
'!!!!'
];
var_dump(fputcsv($file, $arr));
复制代码
file_get_contents()
extension=php_openssl.dll
<?php
$str = file_get_contents("index.csv");
var_dump($str);
复制代码
file_put_contents()
<?php
file_put_contents('baidu.html', file_get_contents('https://www.baidu.com'));
复制代码
file()
readfile()
<?php
$str = readfile('test.txt');
var_dump($str);
复制代码
parse_ini_file()
<?php
var_dump(parse_ini_file("C:\phpStudy\PHPTutorial\php\php-7.2.1-nts\php.ini"));
复制代码
parse_ini_string()
<?php
$ini_string = ' xdebug.profiler_output_dir="C:\phpStudy\PHPTutorial\tmp\xdebug" xdebug.trace_output_dir="C:\phpStudy\PHPTutorial\tmp\xdebug" zend_extension="C:\phpStudy\PHPTutorial\php\php-7.2.1-nts\ext\php_xdebug-2.8.0beta1-7.2-vc15-nts.dll" xdebug.remote_enable = 1 xdebug.remote_autostart = 1 ';
var_dump(parse_ini_string($ini_string));
复制代码
highlight_string()
highlight_file()
<?php
var_dump(highlight_file('test.php'));
复制代码