您的位置:首页 > 编程语言 > PHP开发

PHP单文件上传(含封装函数)

2017-08-26 21:46 645 查看
1,demo.PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form action="error2.php" method="post" enctype="multipart/form-data">
<span>请上传您的文件:</span>
<input type="file" name="myFile"/><br>
<input type="submit" value="上传文件" /><br>
</form>

</body>
</html>

2,
error2.php


<?php
header('content-type:text/html;charset=utf-8');
//接收数据
$fileInfo=$_FILES['myFile'];
$maxSize=2097152;//允许的最大值,2M多少字节1024*1024*2
$allowExt=array('jpeg','jpg','png','gif','wbmp');
$flag=true;//检测是否是真实图片类型

//1,判断错误号
if($fileInfo['error']==0){

//2,判断上传文件的大小
if($fileInfo['size']>$maxSize){

exit('上传文件过大');
}

//3,判断上传文件的类型
//方法一:取到扩展名
$ext=strtolower(end(explode('.', $fileInfo['name'])));
//方法二:取到扩展名
$ext=pathinfo($fileInfo['name'],PATHINFO_EXTENSION);

if(!in_array($ext, $allowExt)){

exit('非法文件类型');
}

//4,判断文件是否是通过HTTP POST方式上传的
if(!is_uploaded_file($fileInfo['tmp_name'])){

exit('文件不是通过HTTP POST方式上传的合法文件');

}

//5,检测是否为真实的图片类型  $flag
if($flag){
if(getimagesize($fileInfo['tmp_name'])){

exit('不是真正的图片类型');
}
}

//6,文件所在的位置
$path='uploads';

//7,指定的目录不存在的情况下,创建
if(
12750
!file_exists($path)){
mkdir($path,0777,true);
chmod($path,0777);
}
//8,确保文件名唯一
$uriName=md5(uniqid(microtime(true),true)).'.'.$ext;
$destination=$path.'/'.$uriName;

//上传
if(move_uploaded_file($fileInfo['tmp_name'],$destination)){
echo '文件上传成功';
}else{
echo '文件上传失败';
}
}else{
switch ($error) {
case 1:
echo '上传文件超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2:
echo '超过了表单MAX_FTLE_SIZE限制的大小';
break;
case 3:
echo '文件部分被上传';
break;
case 4:
echo '没有选择上传文件';
break;
case 6:
echo '没有找到临时目录';
break;
case 7:
case 8:
echo '系统错误';
break;
}
}


3,封装单文件上传函数

<?php
/*
*@封装单文件上传的函数
*/
// $fileInfo=$_FILES['myFile'];

function uploadFile($fileInfo,$allowExt=array('jpeg','jpg','png','gif','wbmp'),$maxSize=2097152,$flag=true, $uploadPath='uploads',$ext=pathinfo($fileInfo['name'],PATHINFO_EXTENSION)){
if($fileInfo['error']>0){
switch ($error) {
case 1:
$msg= '上传文件超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2:
$msg= '超过了表单MAX_FTLE_SIZE限制的大小';
break;
case 3:
$msg='文件部分被上传';
break;
case 4:
$msg= '没有选择上传文件';
break;
case 6:
$msg= '没有找到临时目录';
break;
case 7:
case 8:
$msg= '系统错误';
break;
}
exit($msg);
}

//1,检测非法文件类型
// $ext=pathinfo($fileInfo['name'],PATHINFO_EXTENSION);
// $allowExt=array('jpeg','jpg','png','gif','wbmp');
if(!is_array($allowExt)){

exit('不是数组类型');
}

if(!in_array($ext, $allowExt)){

exit('非法文件类型');
}

//2,判断上传文件的大小
// $maxSize=2097152;//2M
if($fileInfo['size']>$maxSize){

exit('上传文件过大');
}
// 检测是否为真实的图片类型  $flag
if($flag){
if(getimagesize($fileInfo['tmp_name'])){

exit('不是真正的图片类型');
}
}
// 3,判断文件是否是通过HTTP POST方式上传的
if(!is_uploaded_file($fileInfo['tmp_name'])){

exit('文件不是通过HTTP POST方式上传的合法文件');

}
//4,上传文件
// $uploadPath='uploads';
if(!file_exists($uploadPath)){
mkdir($uploadPath,0777,true);
chmod($uploadPath,0777);
}

$uriName=md5(uniqid(microtime(true),true)).'.'.$ext;
$destination=$uploadPath.'/'.$uriName;

if(move_uploaded_file($fileInfo['tmp_name'],$destination)){
exit('文件上传成功');
}else{
exit('文件上传失败');
}

//返回的信息
return array(
'newName'=>$destination,
'size'=>$fileInfo['size'],
'type'=>$fileInfo['type']

);
}

4,使用封装函数

<?php
header('content-type:text/html;charset=utf-8');
include_once 'upload.class.php';
$fileInfo=$_FILES['myFile'];
$allowExt=array('jpeg','jpg');
//传递参数
$newName=uploadFile($fileInfo,'qw','false',$allowExt);
echo $newName;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: