PHP 单文件上传 only code
2015-05-05 23:19
411 查看
//封装单文件函数
$fileInfo = $_FILES ['myFile'];
//1.判断错误号
function uploadFile($fileInfo,$uploadPath = 'uploads',$flag = true,$allowExt = array ('jpeg', 'jpg', 'png', 'gif' ),$maxSize = 2097152) {
if ($fileInfo ['error'] > 0) {
switch ($fileInfo ['error']) {
case 1 :
$mes = '文件上传过大';
break;
case 2 :
$mes = '超过表单大小';
break;
case 3 :
$mes = '文件部分上传';
break;
case 4 :
$mes = '没有选择上传文件';
break;
case 6 :
$mes = '没有找到目录文件';
break;
case 7 :
case 8 :
$mes = '系统错误';
break;
}
exit ();
}
//2.检测文件上传类型
$ext = pathinfo ( $fileInfo ['name'], PATHINFO_EXTENSION );
//$allowExt = array ('jpeg', 'jpg', 'png', 'gif' );
if (! in_array ( $ext, $allowExt )) {
exit ( '文件非法' );
}
//3.检测文件大小
//$maxSize = 2097152;
if ($fileInfo ['szie'] > $maxSize) {
exit ( '文件过大' );
}
//检测图片是否真实
//$flag = true;
if($flag){
if(!getimagesize($fileInfo['tmp_name'])){
exit('文件格式不正确');
}
}
//4.是不是通过http post上传的
if (! is_uploaded_file ( $fileInfo ['tmp_name'] )) {
exit ( '文件不是通过https 文件上传的' );
}
//$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 ( '文件移动失败' );
}
return array(
'newName'=>$destination,
'size'=>$fileInfo['size'],
'type'=>$fileInfo['type']
);
}
//参考慕课网
$fileInfo = $_FILES ['myFile'];
//1.判断错误号
function uploadFile($fileInfo,$uploadPath = 'uploads',$flag = true,$allowExt = array ('jpeg', 'jpg', 'png', 'gif' ),$maxSize = 2097152) {
if ($fileInfo ['error'] > 0) {
switch ($fileInfo ['error']) {
case 1 :
$mes = '文件上传过大';
break;
case 2 :
$mes = '超过表单大小';
break;
case 3 :
$mes = '文件部分上传';
break;
case 4 :
$mes = '没有选择上传文件';
break;
case 6 :
$mes = '没有找到目录文件';
break;
case 7 :
case 8 :
$mes = '系统错误';
break;
}
exit ();
}
//2.检测文件上传类型
$ext = pathinfo ( $fileInfo ['name'], PATHINFO_EXTENSION );
//$allowExt = array ('jpeg', 'jpg', 'png', 'gif' );
if (! in_array ( $ext, $allowExt )) {
exit ( '文件非法' );
}
//3.检测文件大小
//$maxSize = 2097152;
if ($fileInfo ['szie'] > $maxSize) {
exit ( '文件过大' );
}
//检测图片是否真实
//$flag = true;
if($flag){
if(!getimagesize($fileInfo['tmp_name'])){
exit('文件格式不正确');
}
}
//4.是不是通过http post上传的
if (! is_uploaded_file ( $fileInfo ['tmp_name'] )) {
exit ( '文件不是通过https 文件上传的' );
}
//$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 ( '文件移动失败' );
}
return array(
'newName'=>$destination,
'size'=>$fileInfo['size'],
'type'=>$fileInfo['type']
);
}
//参考慕课网
相关文章推荐
- 用PHP实现文件上传
- PHP fastcgi模式大文件上传500错误
- php 文件和表单内容一起上传
- php文件上传
- PHP 文件操作2 上传下载
- 发布两上PHP类(邮件发传及文件上传)
- 利用discuz实现PHP大文件上传应用实例代码
- PHP 文件上传工具类
- PHP使用CURL上传|下载文件
- PHP上传文件参考配置大文件上传
- php文件上传
- PHP创建多级目录并上传文件
- PHP多文件上传个人理解总结 [转]
- PHP文件上传
- php之文件上传
- 修改PHP文件上传大小限制
- PHP任意文件上传漏洞CVE-2015-2348浅析
- PHP上传文件问题小议(一)无权限创建文件目录
- PHP之上传文件
- nginx+php 修改文件上传大小限制