Blogger Information
Blog 39
fans 0
comment 0
visits 34669
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件上传 4月19日作业
美丽城堡
Original
624 people have browsed it

实例

<!--
    1.文件上传的请求类型必须是POST
    2.允许的数据类型必须是:multipart/form-data
    3.如果想在当前的页面处理上传的话,action可能会有三种写法:
    1).最懒的方式: 当前脚本名
    2). 最死的方法: 当前脚本名
    3).最酷的方法:
-->
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post" enctype="multipart/form-data">

    <input type ="hidden" name="MAX_FILE_SIZE" value="54288j">
    <fieldset>
        <legend align="center">文件上传</legend>
        <p>
            <strong>选择文件:</strong>
            <input type="file" name="upload">
        </p>
        <p align="center">
            <button type="submit" name="submit">上传</button>
        </p>
    </fieldset>
</form>
<?php
    echo '<hr>long time no see..';

    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        //1.检测是否有文件被上传
        if(isset($_FILES['upload'])){
//            2.设置一下允许上传的类型
            $allow = ['image/jpg','image/jpeg','image/png'];
//            3.判断用户上传的文件是否正确
            if(in_array($_FILES['upload']['type'],$allow)){
//                将用户文件上传到指定的临时目录: move_uploaded_file()
                if(move_uploaded_file($_FILES['upload']['tmp_name'],"upload/{$FILES['upload']['name']}")){
                    //上传成功

                }else{

                }
            }
            if($_FILES['upload']['error']>0){
                echo '<p>错误的原因有:</p>'
                switch($_FILES['upload']['error']){
                    case 1:
                        echo '文件太大';
                        break;
                    case 2:
                        echo '文件超过表单中的设置大小';
                        break;
                    case 3:
                        echo '文件只有一部分上传了';
                        break;
                    case 4:
                        echo '没有文件被上传';
                        break;
                    case 6:
                        echo '临时文件夹没有';
                        break;
                    case 8:
                        echo '上传意外中止';
                        break;
                    default:
                        echo '..';
                }
            }
        }
    }

    ?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post