在 Windows 中,斜线(/)和反斜线()都可以用作目录分隔符。在其它环境下是斜线(/)。
文件与目录
文件与目录
判断普通文件和目录
is_file( $filename ) : bool —— 判断给定文件名是否为一个普通文件
is_dir ( $filename ) : bool —— 判断给定文件名是都是一个目录,也就是文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17我的文件目录 D:PHPStudyphpstudy_proWWWPHPdemo1 . 表示当前目录下的文件 ./ 表示当前目录下的文件 ../ 表示当前文件的目录的父目录文件 / 表示文件的根目录,是当前文件的盘符,也就是 D盘 <?php var_dump( is_file('/msdia80.dll'));//true var_dump( is_dir('/PHPstudy'));// ?> //相对路径 <?php var_dump( is_file('../Img/t.jpg'));//true var_dump( is_dir('../Img'));//true ?>
文件的属性
1. file_exists( $filename ) : bool —— 检查文件和目录是否存在
1
2
3
4
5
6<?php //检查文件和目录是否存在 var_dump(file_exists("../../index.html"));//false var_dump(file_exists("../../index.php"));//true ?>
2. filesize( $filename ) : int —— 获取普通文件的大小,单位为字节
1
2
3
4
5
6
7
8
9
10
11
12<?php //获取普通文件的大小,单位为字节 if(file_exists("7")) { echo filesize("7")."<br>";//0 echo filesize("7/7-1.php");//5733 B } else{ echo filesize(__FILE__);//计算本文件的大小 } ?>
3. is_readable( $filename ) : bool—— 判断给定文件名是否可读取
1
2
3
4
5<?php //判断给定文件名是否可读取 var_dump(is_readable(__FILE__));//true ?>
4. is_writable( $filename ) : bool —— 判断给定文件名是否可以写入
1
2
3
4
5<?php //判断给定文件名是否可以写入 var_dump(is_writable(__FILE__));//true ?>
5. filectime( $filename ) : int —— 获取文件创建时间
1
2
3
4
5
6
7
8
9<?php // 获取文件创建时间 if(file_exists("7")) {//创建时间 echo date("Y-m-d H:i:s",filectime("7"))."<br>";//2020-04-09 21:23:59 echo date("Y-m-d H:i:s",filectime("7/7-1.php"));//2020-04-09 11:43:11 } ?>
6. filemtime( $filename ) : int —— 获取文件修改时间
1
2
3
4
5
6
7
8
9<?php // 获取文件修改时间 if(file_exists("7")) { echo date("Y-m-d H:i:s",filemtime("7"))."<br>";//2020-04-09 21:24:17 echo date("Y-m-d H:i:s",filemtime("7/7-1.php"));//2020-04-09 22:13:53 } ?>
7. fileatime( $filename ) : int —— 取得文件上次访问的时间
1
2
3
4
5
6
7
8
9<?php //取得文件上次访问的时间 if(file_exists("7")) {//上一次修改时间,即最近一次修改时间 echo date("Y-m-d H:i:s",fileatime("7"))."<br>";//2020-04-09 21:24:17 echo date("Y-m-d H:i:s",fileatime("7/7-1.php"));//2020-04-09 22:13:53 } ?>
8. stat( $filename ) : array —— 获取文件大部分属性值
数字下标 | 关联键名(自 PHP 4.0.6) | 说明 |
---|---|---|
0 | dev | device number - 设备名 |
1 | ino | inode number - inode 号码 |
2 | mode | inode protection mode - inode 保护模式 |
3 | nlink | number of links - 被连接数目 |
4 | uid | userid of owner - 所有者的用户 id |
5 | gid | groupid of owner- 所有者的组 id |
6 | rdev | device type, if inode device * - 设备类型,如果是 inode 设备的话 |
7 | size | size in bytes - 文件大小的字节数 |
8 | atime | time of last access (unix timestamp) - 上次访问时间(Unix 时间戳) |
9 | mtime | time of last modification (unix timestamp) - 上次修改时间(Unix 时间戳) |
10 | ctime | time of last change (unix timestamp) - 上次改变时间(Unix 时间戳) |
11 | blksize | blocksize of filesystem IO * - 文件系统 IO 的块大小 |
12 | blocks | number of blocks allocated - 所占据块的数目 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24//数组分为两部分,前十三组为数字索引,后十三组为字符串索引,对应的值相等 <?php //获取文件大部分属性值 $arr = stat("C:/Windows/System32/drivers/etc/hosts"); echo count($arr)."<br>";//26 foreach( $arr as $key => $val) { echo $key." —— ".$val."<br>"; } // 0 dev —— 2 // 1 ino —— 0 // 2 mode —— 33206 // 3 nlink —— 1 // 4 uid —— 0 // 5 gid —— 0 // 6 rdev —— 2 // 7 size —— 1064 // 8 atime —— 1586490940 // 9 mtime —— 1586490940 // 10 ctime —— 1536996696 // 11 blksize —— -1 // 12 blocks —— -1 ?>
9. lstat( $filename ) : array — 给出一个文件或符号连接的信息
lstat() 和 stat() 相同,只除了它会返回符号连接的状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23<?php //给出一个文件或符号连接的信息 $arr = lstat("C:/Windows/System32/drivers/etc/hosts"); echo count($arr)."<br>";//26 foreach( $arr as $key => $val) { echo $key." —— ".$val."<br>"; } // 0 dev —— 2 // 1 ino —— 0 // 2 mode —— 33206 // 3 nlink —— 1 // 4 uid —— 0 // 5 gid —— 0 // 6 rdev —— 2 // 7 size —— 1064 // 8 atime —— 1586490940 // 9 mtime —— 1586490940 // 10 ctime —— 1536996696 // 11 blksize —— -1 // 12 blocks —— -1 ?>
目录的基本操作
1. basename( $path, [ $suffix ] ) : string—— 返回路径中文件名部分
- $suffix 可选,若文件名以 $suffix 结尾,就会去掉
1
2
3
4
5
6
7
8
9<?php //返回路径中文件名部分 if(file_exists("7")) { echo basename("7/7-1.php");//7-1.php echo basename("7/7-1.php",".php");//7-1 } ?>
2. dirname( $path ) : string —— 返回路径中的目录部分
1
2
3
4
5
6
7<?php //返回路径中的目录部分 echo dirname("7/7-1.php");//7 echo dirname("C:/Windows/System32/drivers/etc/hosts");//C:/Windows/System32/drivers/etc echo dirname(dirname("C:/Windows/System32/drivers/etc/hosts"));//C:/Windows/System32/drivers ?>
3. pathinfo() —— 返回路径的信息
可以看出返回值有四个参数
- diranme —— 文件的父目录
- basename —— 文件名
- extension —— 文件后缀名
- filename —— 文件名
如果是目录(文件夹),则没有后缀名参数extension
1
2
3
4
5
6
7<?php print_r(pathinfo("C:/Windows"));//Array ( [dirname] => C: [basename] => Windows [filename] => Windows ) print_r(pathinfo("7/7-1.php"));//Array ( [dirname] => 7 [basename] => 7-1.php [extension] => php [filename] => 7-1 ) print_r(pathinfo(__FILE__)); // [dirname] => D:PHPStudyphpstudy_proWWWPHPdemo1 [basename] => 8-2.php [extension] => php [filename] => 8-2 ) ?>
4. opendir ( p a t h , [ path ,[ path,[context] ) : resource —— 打开目录句柄
可用于之后的 closedir(),readdir() 和 rewinddir() 调用中。
1
2
3resource 表示 资源 $context —— resource类型
5. readdir ([ $dir_handle ] ) : string —— 从目录句柄中读取条目,返回目录中下一个文件的文件名
1
2
3
4
5$dir_handle —— resource类型 // 一条一条的读取对应目录的文件 // 前两条命令为string(1) "." string(2) ".." // 当到达最后没有文件的时候,返回false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31<?php $dir = opendir("../Img"); var_dump(readdir($dir));//string(1) ".", .表示当前目录 echo "<br>"; var_dump(readdir($dir));//string(2) ".." , .. 表示父目录 echo "<br>"; var_dump(readdir($dir));//string(11) "Captcha.png" echo "<br>"; var_dump(readdir($dir));//string(19) "Lobster-Regular.ttf" echo "<br>"; var_dump(readdir($dir));//string(11) "segoesc.ttf" echo "<br>"; var_dump(readdir($dir));//string(12) "segoescb.ttf" echo "<br>"; var_dump(readdir($dir));//string(5) "t.jpg" echo "<br>"; var_dump(readdir($dir));//bool(false) echo "<br>"; ?> <?php $dir = opendir("C:/Windows"); var_dump(readdir($dir));//string(1) ".", .表示当前目录 echo "<br>"; var_dump(readdir($dir));//string(2) ".." , .. 表示父目录 echo "<br>"; var_dump(readdir($dir));//string(19) "AceXGame_sys_23.dll" echo "<br>"; var_dump(readdir($dir));//sstring(6) "addins" echo "<br>"; ?>
6. rewinddir ( $dir_handle ) : void —— 倒回目录句柄
1
2
3$dir_handle —— resource类型 //使用此函数之后会重新开始读取目录文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<?php $dir = opendir("../Img"); var_dump(readdir($dir));//string(1) ".", .表示当前目录 echo "<br>"; var_dump(readdir($dir));//string(2) ".." , .. 表示父目录 echo "<br>"; var_dump(readdir($dir));//string(11) "Captcha.png" echo "<br>"; var_dump(readdir($dir));//string(19) "Lobster-Regular.ttf" echo "<br>"; rewinddir($dir); var_dump(readdir($dir));//string(1) ".", .表示当前目录 echo "<br>"; var_dump(readdir($dir));//string(2) ".." , .. 表示父目录 echo "<br>"; var_dump(readdir($dir));//string(11) "Captcha.png" echo "<br>"; ?>
7. closedir ([ $dir_handle ] ) : void —— 关闭目录句柄
1
2$dir_handle —— resource类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14<?php $dir = opendir("../Img"); var_dump(readdir($dir));//string(1) ".", .表示当前目录 echo "<br>"; var_dump(readdir($dir));//string(2) ".." , .. 表示父目录 echo "<br>"; var_dump(readdir($dir));//string(11) "Captcha.png" echo "<br>"; closedir($dir); // var_dump(readdir($dir));//报错 ?>
8. mkdir( d i r n a m e ; [ dirname;[ dirname;[mode],[ r e c u r s i v e ] , [ recursive],[ recursive],[content]):bool —— 新建目录
1
2
3
4
5$dirname —— 文件目录 $mode —— 设置文件权限,默认权限0777,可读可写 —— int $recursive —— 取值true 子目录嵌套也就是创建多层目录,默认false $content —— resource类型
1
2
3
4
5
6<?php var_dump( mkdir("../demo2")) ; var_dump( mkdir("../demo3/first",0777,true)) ; var_dump( mkdir("../demo3/second",0777,true)) ; ?>
9. rmdir ( $dirname ,[ $context ] ) : bool —— 删除指定的空目录
1
2$content —— resource类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27<?php //删除目录下的空目录 function removeAllFile($parent_file,$true=false){ // $true 是否删除自身 if(!is_dir($parent_file)) { return 0; } $dir = opendir($parent_file); $i=0; while($str = readdir($dir)) { if ( $i>1 && $str != false) { if(rmdir("{$parent_file}"."/"."{$str}")){ echo "remove:{$str}"."<br>"; } } ++$i; } if($true) { rmdir($parent_file); } } removeAllFile("../demo3",true); ?>
10. scandir ( $directory,[ $sorting_order ],[ $context ] ) : array—— 列出指定路径中的文件和目录
1
2
3
4$directory —— 浏览的目录 $sorting_order —— 文件的排序,默认排序是按照字母升序排列,若为 1 ,则为降序排列,—— int $context —— 资源类型
用这个删除文件就方便了多了
1
2
3
4
5
6
7
8<?php for ($i=1; $i < 5; $i++) { var_dump( mkdir("../demo4/adio_{$i}",0777,true)) ; } print_r(scandir("../demo4")); //Array ( [0] => . [1] => .. [2] => adio_1 [3] => adio_2 [4] => adio_3 [5] => adio_4 ) ?>
文件的基本操作
1. fopen() —— 打开文件或者 URL
fopen()
1
2
3
4
5
6fopen ( $filename , $mode ,[$use_include_path = false], [ $context ]] ) : resource $filename —— 目录 $mode —— 访问类型 $use_include_path —— 如果也需要在 include_path 中搜寻文件的话,可以将可选的第三个参数 use_include_path 设为 '1' 或 TRUE。 $context —— 资源resource类型
mode | 说明 |
---|---|
‘r’ | 只读方式打开,将文件指针指向文件头。 |
‘r+’ | 读写方式打开,将文件指针指向文件头。 |
‘w’ | 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。 |
‘w+’ | 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。 |
‘a’ | 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 |
‘a+’ | 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 |
‘x’ | 创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。这和给 底层的 open(2) 系统调用指定 O_EXCL |
‘x+’ | 创建并以读写方式打开,其他的行为和 ‘x’ 一样。 |
2. fread() —— 读取文件
1
2
3
4fread ( $handle , $length ) : string $handle —— 打开的资源resource类型文件,——resource $length —— 要读取length长度个字符
1
2
3
4
5
6<?php $file = fopen("../Img/index.txt","r"); echo(fread($file,1024));//1111 222 33 466565 //返回值为读取的字符串 ?>
3. fgets() —— 从文件指针中读取一行
1
2
3
4fgets ( $handle,[ $length ] ) : string $handle —— 打开的资源resource类型文件,——resource $length —— 每一行读取 $length - 1 个字符,然后多余的会调到下一行
1
2
3
4
5<?php $file = fopen("../Img/index.txt","r"); echo fgets($file,10); ?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<?php //文件内容 // 1111 // 222 // 33 // 4 $file = fopen("../Img/index.txt","r"); echo fgets($file); echo "<br>"; echo fgets($file,3); echo "<br>"; echo fgets($file); echo "<br>"; echo fgets($file); // 1111 // 22 // 2 // 33 ?>
4. feof() —— 测试文件指针是否到了文件结束的位置
1
2
3feof ($handle ) : bool $handle —— 打开的资源resource类型文件,——resource
1
2
3
4
5
6
7
8
9<?php $file = fopen("../Img/index.txt","r"); echo(fread($file,4));//1111 var_dump(feof($file));//false echo(fread($file,1024));//222 33 466565 var_dump(feof($file));//true ?>
5. fwrite() —— 写入文件(可安全用于二进制文件)
1
2
3
4
5
6
7fwrite ( $handle , $string, [int $length ] ) : int $handle —— 打开的资源resource类型文件,——resource $string —— 要写入的字符串 $length —— 写入length长度的字符,超过此长度就不会写入了 返回值: —— 字符串长度
1
2
3
4
5<?php $file = fopen("../Img/index.txt","a"); echo(fwrite($file,"66565"));//返回字符串长度——5 ?>
6. rewind() —— 倒回文件指针的位置
1
2
3
4rewind ( resource $handle ) : bool 将 handle 的文件位置指针设为文件流的开头。
1
2
3
4
5
6
7
8<?php $file = fopen("../Img/index.txt","r"); echo(fread($file,4));//1111 var_dump(feof($file));//false rewind($file); echo(fread($file,1024));//1111 222 33 466565 ?>
7. flock() —— 轻便的咨询文件锁定
设置锁定,防止多人同时修改,造成堵塞
1
2
3
4
5
6
7
8flock ( $handle , $operation , [&$wouldblock] ) : bool $handle —— 打开的资源resource类型文件,——resource operation —— 取值: LOCK_SH取得共享锁定(读取的程序)。 LOCK_EX 取得独占锁定(写入的程序。 LOCK_UN 释放锁定(无论共享或独占)。 &$wouldblock —— Windows 上不支持
当修改完成,记得解锁,还是使用函数,修改第二个参数
8. fclose() —— 关闭一个已打开的文件指针
1
2fclose ( $handle ) : bool
1
2
3
4
5
6
7<?php $file = fopen("../Img/index.txt","r"); echo(fread($file,4));//1111 fclose($file); echo(fread($file,1024));//报错,因为文件已经关闭 ?>
9. fseek() — 在文件指针中定位
1
2
3
4
5
6
7fseek ( $handle , $offset, [ $whence = SEEK_SET ] ) : int $offset —— 偏移量。 $whence —— 取值: SEEK_SET - 设定位置等于 offset 字节。 SEEK_CUR - 设定位置为当前位置加上 offset。 SEEK_END - 设定位置为文件尾加上 offset。
1
2
3
4
5
6
7
8<?php $file = fopen("../Img/index.txt","r"); fseek($file,1); echo(fread($file,3));//111,此时光标在第一行结尾 fseek($file,2,SEEK_CUR);//因为存在换行符,rn两个字符 echo(fread($file,3));//222 ?>
10. file() —— 把整个文件读入一个数组中
1
2
3
4
5
6
7
8
9file ( $filename ,[$flags = 0], [ $context ] ) : array $filename —— 文件的路径。 $flags: 取值: FILE_USE_INCLUDE_PATH —— 在 include_path 中查找文件。 FILE_IGNORE_NEW_LINES——在数组每个元素的末尾不要添加换行符 FILE_SKIP_EMPTY_LINES——跳过空行 $context —— 资源resource类型
1
2
3
4
5
6
7
8
9
10
11
12//测试感觉flags的参数没什么用 <?php //修改了index,添加了一空行 print_r(file("../Img/index.txt"));//Array ( [0] => 1111 [1] => [2] => 222 [3] => 33 [4] => 466565 ) echo "<br>"; print_R(file("../Img/index.txt",FILE_USE_INCLUDE_PATH));//Array ( [0] => 1111 [1] => [2] => 222 [3] => 33 [4] => 466565 ) echo "<br>"; print_r(file("../Img/index.txt",FILE_IGNORE_NEW_LINES));//Array ( [0] => 1111 [1] => [2] => 222 [3] => 33 [4] => 466565 ) echo "<br>"; print_r(file("../Img/index.txt",FILE_SKIP_EMPTY_LINES));//Array ( [0] => 1111 [1] => [2] => 222 [3] => 33 [4] => 466565 ) ?>
11. copy() —— 拷贝文件
1
2
3
4
5
6copy ( $source , $dest ,[ $context ] ) : bool $source —— 源文件 $dest —— 目标路径,如果目标文件已存在,将会被覆盖 $context —— 资源resource类型
1
2
3
4<?php var_dump(copy("../Img/index.txt","../Img/this.txt"));//true ?>
12. unlink() —— 删除文件
1
2
3
4unlink ( $filename , [ $context ] ) : bool $filename —— 文件的路径。 $context —— 资源resource类型
1
2
3
4<?php var_dump(unlink("../Img/this.txt"));//true ?>
13. file_get_contents() —— 将整个文件读入一个字符串
1
2
3
4
5
6
7
8file_get_contents ( $filename ,[ $use_include_path = false], [ $context], [ $offset = -1], [$maxlen ] ) : string $filename —— 文件的路径。 use_include_path —— 从PHP 5开始,FILE_USE_INCLUDE_PATH可用于触发包含路径include path搜索。 $context —— 资源resource类型,可以用 NULL 来忽略。 $offset —— 从原始流开始读取的偏移量。 $maxlen —— 读取的最大数据长度。默认设置为读取直到到达文件末尾。
1
2
3
4
5<?php echo(file_get_contents("../Img/that.txt",false,NULL));//1111 222 33 466565 echo(file_get_contents("../Img/that.txt",false,NULL,4,5));//222,注意换行由两个字符组成,rn ?>
14. file_put_contents() —— 将字符串写入文件中
1
2
3
4
5
6
7
8
9
10
11file_put_contents ( $filename , $data ,[ $flags = 0], [ $context ] ) : int $filename —— 文件的路径 $data —— 要写入的数据 flags flags 的值可以是 以下 flag 使用 OR (|) 运算符进行的组合。 FILE_USE_INCLUDE_PATH 在 include 目录里搜索 $filename。 FILE_APPEND 如果文件 $filename 已经存在,追加数据而不是覆盖。 LOCK_EX 在写入时获得一个独占锁。 //返回值为字符串的长度
1
2
3
4
5<?php $str = "MyNameIsYz"; echo(file_put_contents("../Img/this.txt",$str,FILE_APPEND));//10 ?>
15. rename() —— 重命名一个文件或目录
1
2rename ( $oldname , $newname [ $context ] ) : bool
1
2
3
4
5<?php var_dump(rename("../Img/that.txt","../Img/this.txt"));//true var_dump(rename("../Img/this.txt","../Img/that.txt"));//true ?>
16. readfile() —— 读入一个文件,并写入到输出缓冲
1
2
3
4
5
6readfile ( $filename, [ $use_include_path = false], [ $context ] ) : int filename —— 要读取的文件名。 use_include_path 想要在 include_path 中搜索文件,可使用这个可选的第二个参数,设为 TRUE。
1
2
3
4
5<?php $file = fopen("../Img/index.txt","a"); readfile("../Img/index.txt");//1111 222 33 466565 ?>
17. ftruncate() — 将文件截断到给定的长度
1
2
3
4
5
6
7ftruncate ( $handle , $size ) : bool $handle —— 文件指针。 $size —— 截断到的大小。 如果 $size 大于文件,则文件将扩展为空字节。
1
2
3
4
5
6//文件内容会为$size大小 <?php $file = fopen("../Img/index.txt","a"); ftruncate($file,5);//1111 ?>
18. ftell — 返回文件指针读/写的位置
1
2ftell ( resource $handle ) : int
1
2
3
4
5
6
7
8//请注意Window中的换行——rn <?php $file = fopen("../Img/index.txt","r"); echo fread($file,6);//1111 echo ftell($file);//6 echo fread($file,5);//222 ?>
19. fflush() — 将缓冲内容输出到文件
1
2fflush ( resource $handle ) : bool
文件指针必须是有效的,必须指向由 fopen() 或 fsockopen() 成功打开的文件(并还未由 fclose() 关闭)。
1
2
3
4
5
6
7
8<?php $file = fopen("../Img/index.txt","a"); fwrite($file, 'MyNameIsYz'); var_dump(fflush($file)); ftruncate($file, ftell($file)); fclose($file); ?>
缓存机制:
当进行文件操作,修改内容,之前的内容会计入缓存机制,然后当调用时就会使用
文件的上传
上传文件的过程不需要管理,是自动的,上传的文件会存放到临时目录中,实际操作就是将该目录中的文件转移到我们需要的地方
HTML标签上传设置
form标签设置
(1) method 设置为 post
(2) enctype 设置为 multipart/form-data
(3) form表单中设置隐藏类型的input,其中name设置为MAX_FILE_SIZE,value值设置为需要限制的上传文件的大小(单位:字节)
1
2
3
4
5
6
7
8<form action="" method="post" enctype="multipart/form-data"> <input type="file" name="myfile" id="myfile"> <input type="submit" name="submit" value="开始上传" id="submitFile"> </form> //注意 name 会在后期的上传中使用 //$_POST['submit'] 此处是用来 sumbit //$_FILES["myfile"]["name"] 此处的 myfile
配置文件
php.ini 配置文件
php.ini 配置 :https://www.php.net/manual/zh/ini.php
集成环境直接搜索文件,然后进行操作
- PHPstudy 应该是在 phpstudy_proExtensionsphp 目录下
- Wampserver 应该在 此软件目录 Apachebin 下
配置项 | 默认值 | 描述 |
---|---|---|
file_uploads | ON | 确定服务器上的PHP脚本是否可以接受HTTP文件上传 |
memory_limit | 8M | 设置脚本可以分配的最大内存量,防止失控的脚本独占服务武器 |
upload_max_filesize | 2M | 限制PHP处理上传文件的最大值,此值必须小于post_max_size |
post_max_size | 8M | 限制通过POST方法可以接受的信息需大量 |
upload_tmp_dir | NULL | 上传文件存放的临时路径,可以是绝对路径,此目录对于拥有此服务器进程的用户必须是可写的,如果未指定,PHP将使用系统的默认值。 |
根据各人需要进行修改:
注意:
post_max_size 要大于 upload_max_filesize
我的设置:
1
2
3
4
5
6
7
8有些默认值感觉也可以,不要改的 file_uploads = On memory_limit=256M upload_max_filesize=100M post_max_size=200M upload_tmp_dir = ""
$_FILES 多维数组
用于存储各种与上传文件有关的信息
值 | 描述 |
---|---|
$_FILES[ ‘file’ ][ ‘name’ ] | 客户端机器文件的原名称,包含扩展名 |
$_FILES[ ‘file’ ][ ‘size’ ] | 已上传文件的大小,单位字节 |
$_FILES[ ‘file’ ][ ‘tmp_name’ ] | 文件上传之后,在服务器端存储的临时文件名 |
$_FILES[ ‘file’ ][ ‘error’ ] | 文件上传时产生的错误,0 —— 表示没有发生任何错误,上传成功 。1 —— 表示长传的文件大小超出了在PHP配置文件中upload_max_filesize 选项限制的值 。 2 —— 表示上传文件大小超出HTMl表单找那个 MAX_FILE_SIZE 选项所指定的值 |
$_FILES[ ‘file’ ][ ‘type’ ] | 获取客户端上传文件的MIME类型 |
MIME类型 规定了各种文件格式的类型,每种MIME类型都是由 " / " 分隔 的主类型 和 子类型组成
当文件上传成功之后,不会显示在upload_tmp_dir设置的 目录里,因为上传的文件被销毁,该文件是临时文件,当刚上传的时候,可以看到闪一下就没了
函数处理
- is_uploaded_file() —— 判断 指定的文件是否通过HTTP POST 上传
1
2
3
4
5
6
7is_uploaded_file ( string $filename ):布尔 filename —— 正在检查文件名。 函数is_uploaded_file()需要一个$ _FILES ['file'] ['tmp_name']之类的参数 客户端计算机$ _FILES ['file'] ['name']上载文件的名称不行。
- move_uploaded_file() —— 文件上传后,首先会存储于服务器的临时目录中,使用此函数将文件移动到新位置
1
2
3
4
5
6move_uploaded_file ( string $filename , string $destination ):布尔 filename —— 上传文件的文件名。 destination —— 移动文件的目的地。
例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>文件上传</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="myfile" id="myfile"> <input type="submit" name="submit" value="开始上传" id="submitFile"> </form> </body> </html> <?php header("Content-type:text/html;charset=utf-8;"); if(isset($_POST['submit'])){//判断是否上传文件 if( is_uploaded_file($_FILES["myfile"]["tmp_name"]) )//判断文件是否是通过HTTP POST上传 { $file = pathinfo($_FILES["myfile"]["name"]);//保存文件信息 $str_ext = $file["extension"];//获取文件扩展 $new_file = date("YmdHis"); $str_file = $new_file.".".$str_ext;//拼接新的文件名 if(move_uploaded_file($_FILES["myfile"]["tmp_name"],"D:PHPStudyphpstudy_proNewFile\{$str_file}")) //保存上传的文件,此处要注意 —— 存在转义问题,会造成一些问题 { echo "上传成功"; } else { echo "上传失败"; } } else { exit("可能有攻击,停止上传"); } } else{ echo "上传失败"; } ?>
文件的下载
浏览器无法打开,就会产生下载
发送指定的文件MIME类型的头文件
1
2header("Content-type: MIME 类型");
File Information 扩展
获取文件MIME类型, PHP 扩展,PHP版本低,可能需要下载
finfo_open() —— 创建一个 fileinfo 资源
1
2
3
4
5
6
7finfo_open ([ int $options= FILEINFO_NONE ],[ string $magic_file=“” ]):resource options —— 一个 Fileinfo 常量 或多个 Fileinfo 常量 进行逻辑或运算。 magic_file —— 魔数数据库文件名称, 通常是 /path/to/magic.mime。 如果未指定,则使用 MAGIC 环境变量。 如果未指定此环境变量, 则使用 PHP 绑定的魔数数据库。 传入 NULL 或者空字符串,等同于使用默认值。
一个 Fileinfo 常量 : https://www.php.net/manual/zh/fileinfo.constants.php
1
2$file_finfo = finfo_open(FILEINFO_MIME_TYPE);//返回文件的 mime 编码
finfo_file() —— 返回一个文件的信息
1
2
3
4
5
6
7
8
9finfo_file ( resource $finfo , string $file_name = NULL ,[ int $options = FILEINFO_NONE ], [ resource $context = NULL ] ) : string $finfo —— finfo_open() 函数所返回的 fileinfo 资源。 file_name —— 要检查的文件名。 options —— 一个 Fileinfo 常量 或多个 Fileinfo 常量 进行逻辑或运算。 $context —— 资源resource类型 返回 file_name 参数指定的文件信息。 发生错误时返回 FALSE 。
1
2
3
4
5$file = "D:PHPStudyphpstudy_proNewFiledownload太阳能作业.zip"; $file_finfo = finfo_open(); var_dump(finfo_file($file_finfo,$file,FILEINFO_MIME_TYPE));//string(15) "application/zip" //返回zip文件的MIME编码
finfo_close() — 关闭 fileinfo 资源
1
2finfo_close ( resource $finfo ) : bool
1
2finfo_close($file_finfo);
finfo_buffer() —— 返回一个字符串缓冲区的信息
1
2
3
4
5
6
7
8
9finfo_buffer ( resource $finfo , string $string = NULL [int $options = FILEINFO_NONE], [ resource $context = NULL ] ) : string $finfo —— finfo_open() 函数所返回的 fileinfo 资源。 $string —— 要检查的文件内容。 options —— 一个 Fileinfo 常量 或多个 Fileinfo 常量 进行逻辑或运算。 $context —— 资源resource类型 返回 string 参数所指定内容的类型描述。 发生错误时返回 FALSE 。
1
2var_dump(finfo_buffer($file_finfo,$file,FILEINFO_MIME_TYPE));//string(10) "text/plain"
指定下载文件的描述
1
2header("Content-Disposition:attachment;filename=文件名称")
指定下载文件的大小
1
2header("Content-length:文件大小");
读取文件内容至输出缓冲区
1
2readfile();
例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<?php //在读取文件之前进行伪装模拟,然后显示到浏览器,供用户下载 function download($file = NULL){ if($file == NULL){ $file = "D:PHPStudyphpstudy_proNewFiledownload太阳能作业.zip"; } $arr = pathinfo($file);//获取文件信息 $file_finfo = finfo_open();//打开finfo文件 $FileMIME = finfo_file($file_finfo,$file,FILEINFO_MIME_TYPE);//获取文件类型对应的MIME信息 header("Content-type:{$FileMIME}"); finfo_close($file_finfo);//关闭finfo文件 $file_name = $arr["basename"]; header("COntent-Disposition:attachement;filename=".$file_name);//备注文件信息 header("Content-Length:".filesize($file));//指定文件大小 readfile($file);//输出文件 return readfile($file); } download(); ?>
最后
以上就是舒心盼望最近收集整理的关于PHP基础 —— 文件与目录的全部内容,更多相关PHP基础内容请搜索靠谱客的其他文章。
发表评论 取消回复