php多文件上传类下载,[PHP文件上传下载]⑤--多文件上传

b7ac85fc572a

Paste_Image.png

test.php

请选择您要上传的文件:

请选择您要上传的文件:

请选择您要上传的文件:

请选择您要上传的文件:

common.func.php

/**

* 得到文件扩展名

* @param $filename

* @return string

*/

function getExt($filename)

{

return strtolower(pathinfo($filename, PATHINFO_EXTENSION));

}

/**

* 产生唯一字符串

* @return string

*/

function getUniName()

{

return md5(uniqid(microtime(true), true));

}

upload.func.php

/**

* 构建上传文件信息

* @return mixed

*/

function getFiles()

{

$i = 0;

foreach ($_FILES as $file) {

if (is_string($file['name'])) {

//单文件

$files[$i] = $file;

$i++;

} else if (is_array($file['name'])) {

foreach ($file['name'] as $key => $val) {

$files[$i]['name'] = $file['name'][$key];

$files[$i]['type'] = $file['type'][$key];

$files[$i]['tmp_name'] = $file['tmp_name'][$key];

$files[$i]['error'] = $file['error'][$key];

$files[$i]['size'] = $file['size'][$key];

$i++;

}

}

}

return $files;

}

function uploadFile($fileInfo, $path = './uploads', $flag = true,

$maxSize = 1048576, $allowExt = array('jpeg', 'jpg', 'gif', 'png'))

{

$res = null;

//判断错误号

if ($fileInfo['error'] === UPLOAD_ERR_OK) {

if ($fileInfo['size'] > $maxSize) {

$res['mes'] = $fileInfo['name'] . '上传文件过大';

}

$ext = getExt($fileInfo['name']);

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

$res['mes'] = $fileInfo['name'] . '非法文件类型';

}

if ($flag && !$res['mes']) {

if (!getimagesize($fileInfo['tmp_name'])) {

$res['mes'] = $fileInfo['name'] . "不是真实图片类型";

}

}

if (!is_uploaded_file($fileInfo['tmp_name'])) {

$res['mes'] = $fileInfo['name'] . "文件不是通过HTTP POST方式上传来的";

}

if ($res['mes']) {

return $res['mes'];

}

if (!file_exists($path)) {

mkdir($path, 0777, true);

chmod($path, 0777);

}

$uniName = getUniName();

$destination = $path . '/' . $uniName . '.' . $ext;

if (move_uploaded_file($fileInfo['tmp_name'], $destination)) {

$res['mes'] = $fileInfo['name'] . "文件移动失败";

}

$res['mes'] = $fileInfo['name'] . '上传成功';

$res['dest'] = $destination;

return $res;

} else {

switch ($fileInfo['error']) {

case 1:

$res['mes'] = "上传文件超过了PHP配置文件中upload_max_file";

break;

case 2:

$res['mes'] = "超过了表单MAX_FILE_SIZE";

break;

case 3:

$res['mes'] = "文件部分被上传";

break;

case 4:

$res['mes'] = "没有选择上传文件";

break;

case 6:

$res['mes'] = "没有找到临时目录";

break;

case 7:

case 8:

$res['mes'] = "系统错误";

break;

}

return $res['mes'];

}

}

doAction.php

//foreach ($_FILES as $fileInfo) {

// $files[] = uploadFile($fileInfo);

//}其中有文件不符合类型会exit (file1 file2 file3 file4)

//var_dump($_FILES);

header('content-type:text/html;charset=utf-8');

require_once 'upload.func.php';

require 'common.func.php';

$files = getFiles();

//var_dump($files);

foreach ($files as $fileInfo) {

$res = uploadFile($fileInfo);

echo $res['mes'], "
";

$uploadFiles[] = $res['dest'];

}

$uploadFiles = array_values(array_filter($uploadFiles));

var_dump($uploadFiles);

?>

b7ac85fc572a

Paste_Image.png

b7ac85fc572a

Paste_Image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值