Blogger Information
Blog 35
fans 0
comment 0
visits 32667
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
遍历文件目录—文件上传与检测作业-2018-9-3
THPHP
Original
715 people have browsed it

1、目录遍历:

实例

<?php
//目录遍历
$file = scandir('../PHP/');
foreach($file as $f){
    if($f != '.' && $f != '..' && $f != '.idea'){
        var_export($f);
    }
}

运行实例 »

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

2、文件上传与检测:

实例

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype="multipart/form-data">
    <!-- 用隐藏域设置允许上传的文件大小,仅考参考 -->
    <input type="hidden" name="MAX_FILE_SIZE" value="542488">
    <fieldset>
        <legend align="center">文件上传</legend>
        <p><strong>选择文件:</strong>
            <input type="file" name="upload"></p>
    </fieldset>
    <p align="center"><button type="submit" name="submit" >上传</button></p>
</form>

<?php
    //检测请求类型是否POST,如果不是应该提示用户类型不对
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
      // 判断检测文件是被上传
        if (isset($_FILES['upload'])) {
            echo '<pre>';
            // 设置文件上传的类型
            $allow = ['image/jpg','image/jpeg', 'image/png'];
            // 文件类型检测
            if (in_array($_FILES['upload']['type'], $allow)) {
                // 临时文件名
                $fix = $_FILES['upload']['tmp_name'];
                // 目标文件名
                $tem = $_FILES['upload']['name'];
               if(move_uploaded_file($fix,"upload/$tem")){
                   echo "<script>alert('上传成功')</script>";
               }else{
                   echo "<script>alert('上传失败')</script>";
               }
            }
        }
    }
?>

运行实例 »

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


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