php文件操作
完整源码直接看最后面的相关内容(师承b站后盾人)
文件上传到的位置
第一种方法:上传到你自己的web服务器上。大文件同时看的人多了后,带宽不够会很卡。
第二种方法:大文件数据放到服务商上,分布到不同的节点服务器上。
本例是用的第一种方法
配置相关(php.ini)
file_uploads = On//文件上传允许的开关 upload_tmp_dir ="c:/wamp64/tmp"//临时上传目录 upload_max_filesize = 2M//最大允许上传大小 post_max_size = 8M//post接受的最大大小 max_file_uploads = 20//最大允许上传数量
_FILES的数据返回类型
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="up"> <button>提交</button> </form>
print_r($_FILES);//upload.php里面的内容
返回的是一个数组,这个数组包括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 引进。
前台实现对文件上传大小的限制
<input type="hidden" name="MAX_FILE_SIZE" value="200">
在提交文件之前使用
单文件上传操作
function uploader(){ if(is_uploaded_file($_FILES['up']['tmp_name'])) { $to='files/'.$_FILES['up']['name'];//移动到的路径 if(move_uploaded_file($_FILES['up']['tmp_name'],$to))//如果执行了移动文件操作 { return $to; } } return false; }
move_uploaded_file(file,newloc)
file 必需。规定要移动的文件。
newloc 必需。规定文件的新位置。
is_uploaded_file() 函数检查指定的文件是否是通过 HTTP POST 上传的。
如果文件是通过 HTTP POST 上传的,该函数返回 TRUE。
多文件上传
数据的整理
把之前的up改为数组
<input type="file" name="image[]"> <input type="file" name="image[]"> <input type="file" name="image[]">
这样输出的数据是这样的
我们用函数处理一下
private function format(){ $file=[]; foreach($_FILES as $field){ if(is_array($field['name'])){ foreach($field['name'] as $id=>$file) { $files[]=[ 'name'=>$field['name'][$id], 'type'=>$field['type'][$id], 'error'=>$field['error'][$id], 'tmp_name'=>$field['tmp_name'][$id], 'size'=>$field['size'][$id], ]; } }else{ $files[]=$field; } } return $files; }
无论是多维数组还是单条纪录都能够以这样的形势输出
创建上传目录
private function makedir(){ $path='uploads/'.date('y/m'); $this->dir=$path; return is_dir($path) or mkdir($path,0755,true); }
检测并把文件移动到我们想要的文件夹
public function make(){ $this->makedir(); $files = $this->format(); $saveFiles=[]; foreach($files as $file) { if($file['error']==0) { if(is_uploaded_file($file['tmp_name'])) { $to =$this->dir.'/'.time().mt_rand(1,9999).'.'.pathinfo($file['name'])['extension']; if(move_uploaded_file($file['tmp_name'],$to)) { $saveFiles[]=[ 'path'=> $to, 'size'=>$file['size'], 'name'=>$file['name'], ]; } } } } return $saveFiles; }
控制器使用这个php
<?php include 'upload.php'; $uploader=new uploader; $file= $uploader->make(); ?>
因为我们用$saveFiles[]保存了文件的路径大小和名字,为了方便我们对文件的使用,我们可以把这些数据放到数据库
这里不细讲PDO怎么操作数据库了
文件的完整源码如下
目录结构
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>hello</title> </head> <body> <form action="hcon.php" method="post" enctype="multipart/form-data"> <input type="file" name="image[]"> <input type="file" name="image[]"> <input type="file" name="image[]"> <button>提交</button> </form> </body> </html>
upload.php
<?php class uploader{ protected $dir; public function make(){ $this->makedir(); $files = $this->format(); $saveFiles=[]; foreach($files as $file) { if($file['error']==0) { if(is_uploaded_file($file['tmp_name'])) { $to =$this->dir.'/'.time().mt_rand(1,9999).'.'.pathinfo($file['name'])['extension']; if(move_uploaded_file($file['tmp_name'],$to)) { $saveFiles[]=[ 'path'=> $to, 'size'=>$file['size'], 'name'=>$file['name'], ]; } } } } return $saveFiles; } //创建上传目录 private function makedir(){ $path='uploads/'.date('y/m'); $this->dir=$path; return is_dir($path) or mkdir($path,0755,true); } private function format(){ $file=[]; foreach($_FILES as $field){ if(is_array($field['name'])){ foreach($field['name'] as $id=>$file) { $files[]=[ 'name'=>$field['name'][$id], 'type'=>$field['type'][$id], 'error'=>$field['error'][$id], 'tmp_name'=>$field['tmp_name'][$id], 'size'=>$field['size'][$id], ]; } }else{ $files[]=$field; } } return $files; } } ?>
hcon.php
<?php include 'upload.php'; $uploader=new uploader; $file= $uploader->make(); ?>