关键词搜索

源码搜索 ×
×

PHP开发之简单上传功能的实现

发布2021-06-14浏览429次

详情内容

第1章 PHP开发之简单上传功能的实现

1.1 PHP实现文件上传之上传原理及实现

file.html:

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

    upload.php:

    <?php
    header("content-type:text/html;charset=utf-8");
    // 文件上传处理程序
    // $_FILES文件上传变量
    //echo "<pre>";
    //var_dump($_FILES);
    //exit();
    //echo "</pre>";
    // 处理上传文件
    $filename = $_FILES['myFile']['name'];
    $type = $_FILES['myFile']['type'];
    $tmp_name = $_FILES['myFile']['tmp_name'];
    $size = $_FILES['myFile']['size'];
    $error = $_FILES['myFile']['error'];
    // 将服务器上的临时文件移动到指定位置
    move_uploaded_file($tmp_name, "upload/".$filename); // 文件夹要提前创建好
    // 根据我们的错误查看或者直接报告给用户原因
    if ($error == 0) {
        echo "上传成功!!";
    } else {
        switch ($error) {
            case 1:
                echo "超过上传文件的最大值,请上传2M以下的文件!";
                break;
            case 2:
                echo "上传文件过多!";
                break;
            case 3:
                echo "文件未完成上传!";
                break;
            case 4:
                echo "未选择上传文件!";
                break;
            case 5:
                echo "上传文件为0!";
                break;
        }
    }
    ?>
    
      15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    1.2 PHP实现文件上传之上传限制

    file2.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>文件上传(客户端的限制)</title>
    </head>
    <body>
    <form action="upload2.php" method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="101321">
        请选择需要上传的文件:
        <input type="file" name="myFile" accept="image/jpeg,image/gif,text/html"><br>
        <input type="submit" value="上传">
    </form>
    </body>
    </html>
    
      15

    upload2.php:

    <?php
    header('content-type:text/html;charset=utf-8');
    // 接收文件,临时文件信息
    $file_info = $_FILES['myFile']; // 降维操作
    $file_name = $file_info['name'];
    $tmp_name = $file_info['tmp_name'];
    $size = $file_info['size'];
    $type = $file_info['type'];
    $error = $file_info['error'];
    
    // 服务器端设定限制
    $maxsize = 10485760; // 10M,10*1024*1024
    $allowExt = array('jpeg','jpg','png','gif'); // 允许上传的文件类型(扩展名)
    $ext = pathinfo($file_name, PATHINFO_EXTENSION); // 提取上传文件的扩展名
    // 目标存放文件夹
    $path = "uploads";
    if (!file_exists($path)) {
        // 若目录不存在,则创建
        mkdir($path, 0777, true); // 创建目录
        chmod($path, 0777); // 修改文件模式,所有人都有读、写、执行权限
    }
    // 得到唯一的文件名,防止因文件名相同而产生覆盖
    $uniName = md5(uniqid(microtime(true),true)).".$ext"; // md5加密,uniqid产生唯一id,microtime作前缀
    // 目标存放文件地址
    $destination = $path."/".$uniName;
    // 当文件上传成功时,存入临时文件夹,服务器开始判断
    if ($error===0) {
        if ($size > $maxsize) {
            exit("上传文件过大!");
        }
        if (!in_array($ext, $allowExt)) {
            exit("非法文件类型!");
        }
        if (!is_uploaded_file($tmp_name)) {
            exit("上传方式有误,请使用post方式!");
        }
        // 判断是否为真实图片(防止伪装成图片的病毒一类的
        if (!getimagesize($tmp_name)) {
            // getimagesize为true时返回数组,否则返回false
            exit("不是真正的图片类型!");
        }
        // move_uploaded_file($tmp_name, "upload/".$file_name);
        if (@move_uploaded_file($tmp_name, $destination)) {
            // @错误抑制符,不让用户看到警告
            echo "文件".$file_name."上传成功!";
        } else {
            echo "文件".$file_name."上传失败!";
        }
    } else {
        switch ($error) {
            case 1:
                echo "超过了上传文件的最大值,请上传2M以下的文件!";
                break;
            case 2:
                echo "上传文件过多,一次仅支持上传文件不得超过20个!";
                break;
            case 3:
                echo "文件并未完全上传,请再次尝试!";
                break;
            case 4:
                echo "未选择上传文件!";
                break;
            case 7:
                echo "没有临时文件夹!";
                break;
        }
    }
    
      15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    1.3 PHP实现文件上传之封装上传函数

    file3.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>文件上传(客户端的限制)</title>
    </head>
    <body>
    <form action="upload3.php" method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="101321">
        请选择需要上传的文件:
        <input type="file" name="myFile" accept="image/jpeg,image/gif,text/html"><br>
        <input type="submit" value="上传">
    </form>
    </body>
    </html>
    
      15

    file_upload.php:

    <?php
    function uploadFile ($file_info, $path, $maxsize, $allowExt) {
        header('content-type:text/html;charset=utf-8');
    // 接收文件,临时文件信息
        $file_name = $file_info['name'];
        $tmp_name = $file_info['tmp_name'];
        $size = $file_info['size'];
        $type = $file_info['type'];
        $error = $file_info['error'];
    
    // 服务器端设定限制
        $ext = pathinfo($file_name, PATHINFO_EXTENSION); // 提取上传文件的扩展名
    // 目标存放文件夹
        if (!file_exists($path)) {
            // 若目录不存在,则创建
            mkdir($path, 0777, true); // 创建目录
            chmod($path, 0777); // 修改文件模式,所有人都有读、写、执行权限
        }
    // 得到唯一的文件名,防止因文件名相同而产生覆盖
        $uniName = md5(uniqid(microtime(true),true)).".$ext"; // md5加密,uniqid产生唯一id,microtime作前缀
    // 目标存放文件地址
        $destination = $path."/".$uniName;
    // 当文件上传成功时,存入临时文件夹,服务器开始判断
        if ($error===0) {
            if ($size > $maxsize) {
                exit("上传文件过大!");
            }
            if (!in_array($ext, $allowExt)) {
                exit("非法文件类型!");
            }
            if (!is_uploaded_file($tmp_name)) {
                exit("上传方式有误,请使用post方式!");
            }
            // 判断是否为真实图片(防止伪装成图片的病毒一类的
            if (!getimagesize($tmp_name)) {
                // getimagesize为true时返回数组,否则返回false
                exit("不是真正的图片类型!");
            }
            // move_uploaded_file($tmp_name, "upload/".$file_name);
            if (@move_uploaded_file($tmp_name, $destination)) {
                // @错误抑制符,不让用户看到警告
                echo "文件".$file_name."上传成功!";
            } else {
                echo "文件".$file_name."上传失败!";
            }
        } else {
            switch ($error) {
                case 1:
                    echo "超过了上传文件的最大值,请上传2M以下的文件!";
                    break;
                case 2:
                    echo "上传文件过多,一次仅支持上传文件不得超过20个!";
                    break;
                case 3:
                    echo "文件并未完全上传,请再次尝试!";
                    break;
                case 4:
                    echo "未选择上传文件!";
                    break;
                case 7:
                    echo "没有临时文件夹!";
                    break;
            }
        }
        return $destination;
    }
    
      15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    upload3.php:

    <?php
    header('content-type:text/html;charset=utf-8');
    // 初始化相关变量
    $file_info = $_FILES['myFile']; // 降维操作
    $maxsize = 10485760; // 10M,10*1024*1024
    $allowExt = array('jpeg','jpg','png','gif'); // 允许上传的文件类型(扩展名)
    $path = "uploads";
    // 引入文件上传函数
    include_once 'file_upload.php';
    uploadFile($file_info, $path, $maxsize, $allowExt);
    ?>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.4 PHP实现文件上传之多文件上传

    file4.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>多文件上传</title>
    </head>
    <body>
    <form action="upload4.php" method="post" enctype="multipart/form-data">
        请选择需上传的文件:<br>
        <input type="file" name="myFile1"><br>
        请选择需上传的文件:<br>
        <input type="file" name="myFile2"><br>
        请选择需上传的文件:<br>
        <input type="file" name="myFile3"><br>
        请选择需上传的文件:<br>
        <input type="file" name="myFile4"><br>
        请选择需上传的文件:<br>
        <input type="file" name="myFile5"><br>
        请选择需上传的文件:<br>
        <input type="file" name="myFile6"><br>
        <input type="submit" value="上传">
    </form>
    </body>
    </html>
    
      15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    upload4.php:

    <?php
    header("content-type:text/html;charset=utf-8");
    include_once 'file_upload.php';
    // 初始化相关变量
    $maxsize = 10485760; // 10M,10*1024*1024
    $allowExt = array('jpeg','jpg','png','gif'); // 允许上传的文件类型(扩展名)
    $path = "uploads";
    foreach ($_FILES as $file_info) {
        $file[] = uploadFile($file_info, $path, $maxsize, $allowExt);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载