Blogger Information
Blog 32
fans 0
comment 0
visits 21388
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP实现文件上传-2018年4月22日21:10:17
inhylei
Original
646 people have browsed it

$_FILES是一个二维数组,一维是当前的文件上件控件的名称,就是name属性值

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data" method="post">
    <fieldset>
        <legend>文件上传</legend>
        <input type="hidden" name="MAX_FILE_SIZE" value="500000">
        <p><input type="file" name="upload"></p>
        <p><button>提交</button></p>

    </fieldset>

</form>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js
"></script>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    if (isset($_FILES['upload'])){
        $arrow = ['image/png','image/gif','image/jpeg'];
//        echo  print_r($_FILES['upload']['type']);
        if(in_array($_FILES['upload']['type'],$arrow)){
            if (move_uploaded_file($_FILES['upload']['tmp_name'],"upload/{$_FILES['upload']['name']}")){
                echo "<script>

$('p:last').after('<span>').next().text('上传成功').css('color','green')
  </script>";
            }
        }else{
            echo "<script>
$('p:last').after('<span>').next().text('图片类型错误,仅支持jpeg,png,gif').css('color','red')


</script>";
        }


    }
    if ($_FILES['upload']['error'] > 0 ) {
        echo '<p>错误原因是:<strong>';

        switch ($_FILES['upload']['error']) {
            case 1:
                echo '文件超过了php.ini配置中设置的大小';
                break;
            case 2:
                echo '文件超过了表单中常量设置的大小';
                break;
            case 3:
                echo '仅有部分文件被上传';
                break;
            case 4:
                echo '没有文件被上传';
                break;
            case 6:
                echo '没有可用的临时文件夹';
                break;
            case 7:
                echo '磁盘已满,写入失败';
                break;
            case 8:
                echo '上传意外中止';
                break;

            default:
                echo '系统未知错误';
                break;
        }

        echo '</strong></p>';
        //保险起见,最好把创建的临时文件删除,当然系统也会在结束会话时自动清空
        if (file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])) {
            unlink($_FILES['upload']['tmp_name']);
        }
    }

}else{
    echo '1';
}



?>

总结  系统变量: $_FILES是一个二维数组,一维是当前的文件上件控件的名称,就是name属性值
二维是它的当前属性,最重要的有以下几个:
$_FILES['file']['name']: 文件原始名称
$_FILES['file']['type']: 文件类型
$_FILES['file']['size']: 上传的文件大小
$_FILES['file']['tmp_name']: 服务器上的临时文件夹
$_FILES['file']['error']: 上传错误代码

$_SERVER['PHP_SELF']:当前php脚本

enctype="multipart/form-data" :允许通过表单上传文件

method:请求类型必须是POST

最严格的写法:$_SERVER['PHP_SELF']:建议实际工作中用htmlspecialchars()进行防跨域攻击处理

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