1.文件上传
代码还需要进一步封装下。。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
名称<input type="text" name='uname'><br>
图片1<input type='file' name='goods[]'><br>
图片2<input type='file' name='goods[]'><br>
图片3<input type='file' name='goods[]'><br>
<input type="submit" value="提交">
</form>
</body>
</html>
upload.php
<?php
header("content-type:text/html;charset=utf-8");
// array(1) { ["goods"]=> array(5) { ["name"]=> string(7) "111.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(27) "C:\Windows\temp\php330E.tmp" ["error"]=> int(0) ["size"]=> int(23847) } }
echo '<pre>';
var_dump($_FILES);
echo '<hr>';
$ret = uploadMultiFile($_FILES['goods']);
echo '<pre>';
var_dump($ret);
//多文件上传
function uploadMultiFile($file_infos)
{
foreach ($file_infos['name'] as $key => $value) {
$file_info['name'] = $file_infos['name'][$key];
$file_info['type'] = $file_infos['type'][$key];
$file_info['tmp_name'] = $file_infos['tmp_name'][$key];
$file_info['error'] = $file_infos['error'][$key];
$file_info['size'] = $file_infos['size'][$key];
$result[] = uploadFile($file_info);
}
return $result;
}
function uploadFile($file_info)
{
if ($file_info['error'] != 0) {
echo '上传发生错误';
return false;
}
$ext = strrchr($file_info['name'], '.');
$ext_list = array(
'.jpg',
'.png',
'.gif',
'.jpeg'
);
if (! in_array($ext, $ext_list)) {
echo "文件后缀名 {$ext} 错误";
return false;
}
$type = $file_info['type'];
$type_list = array(
'image/png',
'image/jpeg',
'image/gif'
);
if (! in_array($type, $type_list)) {
echo '文件后类型错误';
return false;
}
// php检测文件类型
$file_mime = new Finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_mime->file($file_info['tmp_name']);
if (! in_array($mime_type, $type_list)) {
echo 'php检测文件类型错误';
return false;
}
// 检测文件大小
$max_size = 2 * 1024 * 1024; // 1M
if ($file_info['size'] > $max_size) {
echo '文件过大';
return false;
}
//上传文件保存目录
$upload_path = './';
//根据时间生成子目录
$sub_dir = date('Ymdh') . '/';
if (! is_dir($upload_path . $sub_dir)) {
$boo = mkdir($upload_path . $sub_dir, 0755, true);
if (! $boo) {
echo "保存文件目录{$upload_path}{$sub_dir}创建失败!";
return false;
}
}
$prefix = 'goods_';
//文件保存的完整路径,文件名根据unique ID生成
$destination = $upload_path . $sub_dir . uniqid($prefix, true) . $ext;
// 检测是否http上传
if (! is_uploaded_file($file_info['tmp_name'])) {
echo "非HTTP上传文件!";
return false;
}
//将文件从临时目录移动到指定目录
if (move_uploaded_file($file_info['tmp_name'], $destination)) {
return $destination;
} else {
echo '移动文件错误';
return false;
}
}
2.文件下载
<?php
$file = "./data.txt";
//主题处理--附件
header("Content-Disposition:attachment;filename=".basename($file));
//文件类型
$fileinfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $fileinfo->file($file);
header("Content-type: $mime");
//文件大小
$fileszie = filesize($file);
header("Content-Length: $fileszie");
//读取文件
$handle = fopen($file, 'r');
while (!feof($handle)){
echo fgets($handle,1024);
}
本文介绍了一个简单的文件上传和下载的PHP实现方案。通过示例代码展示了如何使用PHP处理多文件上传,包括验证文件类型、大小及上传过程中的错误处理等,并实现了文件下载功能。
403

被折叠的 条评论
为什么被折叠?



