php之文件上传和下载

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);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值