本文实例讲述了PHP单文件上传原理及上传函数的封装操作。分享给大家供大家参考,具体如下:
表单:
0.php:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!doctype html>
< html >
< head >
< meta charset = "utf-8" >
< title >无标题文档</ title >
</ head >
< body >
< form action = "000.php" method = "post" enctype = "multipart/form-data" >
请选择您要上传的文件< input type = "file" name = "myfile" />
< input type = "submit" value = "上传文件" />
</ form >
</ body >
</ html >
|
单文件上传函数的封装:
00.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
<?php
//单文件上传函数的封装
//文件上传原理:将客户端的文件上传到服务器端,再将服务器端的临时文件移动到指定目录即可。
//文件的方向:客户端——>服务器(临时文件)——>指定目录,当文件进入服务器时它就是临时文件了,这时操作中要用临时文件的名称tmp_name。
//在客户端设置上传文件的限制(文件类型和大小)是不安全的,因为客户能通过源代码修改限制,所以在服务端这里设置限制。
//设置编码为UTF-8,以避免中文乱码
header( 'Content-Type:text/html;charset=utf-8' );
//通过$_FILES接收上传文件的信息
//$fileInfo = $_FILES['myFile'];
function uploadFile( $fileInfo , $uploadPath = 'uploads' , $flag =true, $allowExt = array ( 'jpeg' , 'jpg' , 'png' , 'gif' ), $maxSize = 2097152){
//判断错误号,只有为0或者是UPLOAD_ERR_OK,没有错误发生,上传成功
if ( $fileInfo [ 'error' ]>0){
//注意!错误信息没有5
switch ( $fileInfo [ 'error' ]){
case 1:
$mes = '上传文件超过了PHP配置文件中upload_max_filesize选项的值' ;
break ;
case 2:
$mes = '超过了HTML表单MAX_FILE_SIZE限制的大小' ;
break ;
case 3:
$mes = '文件部分被上传' ;
break ;
case 4:
$mes = '没有选择上传文件' ;
break ;
case 6:
$mes = '没有找到临时目录' ;
break ;
case 7:
$mes = '文件写入失败' ;
break ;
case 8:
$mes = '上传的文件被PHP扩展程序中断' ;
break ;
}
exit ( $mes );
return false;
}
$ext = pathinfo ( $fileInfo [ 'name' ],PATHINFO_EXTENSION);
//$allowExt=array('jpeg','jpg','png','gif');
//检测上传文件的类型
if (! in_array ( $ext , $allowExt )) {
exit ( '非法文件类型' );
}
//检测上传文的件大小是否符合规范
//$maxSize = 2097152;//2M
if ( $fileInfo [ 'size' ]> $maxSize ){
exit ( '上传文件过大' );
}
//检测图片是否为真实的图片类型
//$flag=true;
if ( $flag ){
if (! getimagesize ( $fileInfo [ 'tmp_name' ])){
exit ( '不是真实的图片类型' );
}
}
//检测是否是通过HTTP POST方式上传上来
if (! is_uploaded_file ( $fileInfo [ 'tmp_name' ] )) {
exit ( '文件不是通过HTTP POST方式上传上来的' );
}
//$uploadPath='uploads';
//如果没有这个文件夹,那么就创建一个
if (! file_exists ( $uploadPath )){
mkdir ( $uploadPath , 0777, true);
chmod ( $uploadPath , 0777 );
}
//新文件名唯一
$uniName = md5 ( uniqid( microtime(true),true) ). '.' . $ext ;
$destination = $uploadPath . '/' . $uniName ;
//@符号是为了不让客户看到错误信息
if (! @move_uploaded_file( $fileInfo [ 'tmp_name' ], $destination )){
exit ( '文件移动失败' );
}
//echo '文件上传成功';
//return array(
// 'newName'=>$destination,
// 'size'=>$fileInfo['size'],
// 'type'=>$fileInfo['type']
//);
return $destination ;
}
?>
|
服务端操作上传的文件:
000.php
1
2
3
4
5
6
7
8
|
<?php
header( 'content-type:text/html;charset=utf-8' );
include_once '00.php' ;
$fileInfo = $_FILES [ 'myfile' ];
$allowExt = array ( 'jpeg' , 'jpg' , 'png' , 'gif' , 'html' , 'txt' ); //修改允许上传文件的类型,为('jpeg','jpg','png','gif','html','txt'),也可以增加新的,如pdf,pptx等等
$newName =uploadFile( $fileInfo , 'imooc' ,false, $allowExt ); //修改上传保存的文件夹为本地的'imooc',如果没有这个文件夹,那么就创建一个;//'false'参数:不要检查上传的文件是否为真实的图片,因为要允许上传除开图片类型外的其他类型文件,如html、txt
echo $newName ;
?>
|
最后,PHP的文件上传还有更智能的,功能更健全的 PHP单文件、多个单文件、多文件上传函数的封装
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/ltx06/article/details/45542849