php文件操作
完整源码直接看最后面的相关内容(师承b站后盾人)
文件上传到的位置
第一种方法:上传到你自己的web服务器上。大文件同时看的人多了后,带宽不够会很卡。
第二种方法:大文件数据放到服务商上,分布到不同的节点服务器上。
本例是用的第一种方法
配置相关(php.ini)
1
2
3
4
5
6
7 1file_uploads = On//文件上传允许的开关
2upload_tmp_dir ="c:/wamp64/tmp"//临时上传目录
3upload_max_filesize = 2M//最大允许上传大小
4post_max_size = 8M//post接受的最大大小
5max_file_uploads = 20//最大允许上传数量
6
7
_FILES的数据返回类型
1
2
3
4
5
6 1 <form action="upload.php" method="post" enctype="multipart/form-data">
2 <input type="file" name="up">
3 <button>提交</button>
4 </form>
5
6
1
2
3 1 print_r($_FILES);//upload.php里面的内容
2
3
返回的是一个数组,这个数组包括name,type,tmp_name,error,和size。
[up]指的是表单的name名
error的值表示的意思如下:
UPLOAD_ERR_OK
其值为 0,没有错误发生,文件上传成功。
UPLOAD_ERR_INI_SIZE
其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize选项限制的值。
UPLOAD_ERR_FORM_SIZE
其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
UPLOAD_ERR_PARTIAL
其值为 3,文件只有部分被上传。
UPLOAD_ERR_NO_FILE
其值为 4,没有文件被上传。
UPLOAD_ERR_NO_TMP_DIR
其值为 6,找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进。
UPLOAD_ERR_CANT_WRITE
其值为 7,文件写入失败。PHP 5.1.0 引进。
前台实现对文件上传大小的限制
1
2
3 1 <input type="hidden" name="MAX_FILE_SIZE" value="200">
2
3
在提交文件之前使用
单文件上传操作
1
2
3
4
5
6
7
8
9
10
11
12
13 1 function uploader(){
2 if(is_uploaded_file($_FILES['up']['tmp_name']))
3 {
4 $to='files/'.$_FILES['up']['name'];//移动到的路径
5 if(move_uploaded_file($_FILES['up']['tmp_name'],$to))//如果执行了移动文件操作
6 {
7 return $to;
8 }
9 }
10 return false;
11 }
12
13
move_uploaded_file(file,newloc)
file 必需。规定要移动的文件。
newloc 必需。规定文件的新位置。is_uploaded_file() 函数检查指定的文件是否是通过 HTTP POST 上传的。
如果文件是通过 HTTP POST 上传的,该函数返回 TRUE。
多文件上传
数据的整理
把之前的up改为数组
1
2
3
4
5 1 <input type="file" name="image[]">
2 <input type="file" name="image[]">
3 <input type="file" name="image[]">
4
5
这样输出的数据是这样的
我们用函数处理一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 1 private function format(){
2 $file=[];
3 foreach($_FILES as $field){
4 if(is_array($field['name'])){
5 foreach($field['name'] as $id=>$file)
6 {
7 $files[]=[
8 'name'=>$field['name'][$id],
9 'type'=>$field['type'][$id],
10 'error'=>$field['error'][$id],
11 'tmp_name'=>$field['tmp_name'][$id],
12 'size'=>$field['size'][$id],
13 ];
14 }
15 }else{
16 $files[]=$field;
17 }
18 }
19 return $files;
20 }
21
22
无论是多维数组还是单条纪录都能够以这样的形势输出
创建上传目录
1
2
3
4
5
6
7 1private function makedir(){
2 $path='uploads/'.date('y/m');
3 $this->dir=$path;
4 return is_dir($path) or mkdir($path,0755,true);
5 }
6
7
检测并把文件移动到我们想要的文件夹
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 1 public function make(){
2 $this->makedir();
3 $files = $this->format();
4 $saveFiles=[];
5 foreach($files as $file)
6 {
7 if($file['error']==0)
8 {
9 if(is_uploaded_file($file['tmp_name']))
10 {
11 $to =$this->dir.'/'.time().mt_rand(1,9999).'.'.pathinfo($file['name'])['extension'];
12 if(move_uploaded_file($file['tmp_name'],$to))
13 {
14 $saveFiles[]=[
15 'path'=> $to,
16 'size'=>$file['size'],
17 'name'=>$file['name'],
18 ];
19 }
20 }
21
22 }
23 }
24
25 return $saveFiles;
26
27 }
28
29
控制器使用这个php
1
2
3
4
5
6
7 1<?php
2include 'upload.php';
3$uploader=new uploader;
4$file= $uploader->make();
5?>
6
7
因为我们用$saveFiles[]保存了文件的路径大小和名字,为了方便我们对文件的使用,我们可以把这些数据放到数据库
这里不细讲PDO怎么操作数据库了
文件的完整源码如下
目录结构
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
7 <title>hello</title>
8</head>
9<body>
10 <form action="hcon.php" method="post" enctype="multipart/form-data">
11 <input type="file" name="image[]">
12 <input type="file" name="image[]">
13 <input type="file" name="image[]">
14 <button>提交</button>
15 </form>
16</body>
17</html>
18
19
upload.php
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 1<?php
2 class uploader{
3 protected $dir;
4 public function make(){
5 $this->makedir();
6 $files = $this->format();
7 $saveFiles=[];
8 foreach($files as $file)
9 {
10 if($file['error']==0)
11 {
12 if(is_uploaded_file($file['tmp_name']))
13 {
14 $to =$this->dir.'/'.time().mt_rand(1,9999).'.'.pathinfo($file['name'])['extension'];
15 if(move_uploaded_file($file['tmp_name'],$to))
16 {
17 $saveFiles[]=[
18 'path'=> $to,
19 'size'=>$file['size'],
20 'name'=>$file['name'],
21 ];
22 }
23 }
24
25 }
26 }
27
28 return $saveFiles;
29
30 }
31 //创建上传目录
32 private function makedir(){
33 $path='uploads/'.date('y/m');
34 $this->dir=$path;
35 return is_dir($path) or mkdir($path,0755,true);
36 }
37
38 private function format(){
39 $file=[];
40 foreach($_FILES as $field){
41 if(is_array($field['name'])){
42 foreach($field['name'] as $id=>$file)
43 {
44 $files[]=[
45 'name'=>$field['name'][$id],
46 'type'=>$field['type'][$id],
47 'error'=>$field['error'][$id],
48 'tmp_name'=>$field['tmp_name'][$id],
49 'size'=>$field['size'][$id],
50 ];
51 }
52 }else{
53 $files[]=$field;
54 }
55 }
56 return $files;
57 }
58 }
59?>
60
61
hcon.php
1
2
3
4
5
6
7 1<?php
2include 'upload.php';
3$uploader=new uploader;
4$file= $uploader->make();
5?>
6
7