Blogger Information
Blog 43
fans 0
comment 0
visits 26785
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件上传与检测和目录遍历+2018年8月29日
Lee的博客
Original
586 people have browsed it

文件的上传:

实例

<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
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        if (isset($_FILES['upload'])){
            $allow = ['image/jpg','image/jpeg','image/png'];
            @mkdir('upload');
            if (in_array($_FILES['upload']['type'],$allow)){
                if (move_uploaded_file($_FILES['upload']['tmp_name'],"upload/{$_FILES['upload']['name']}")){
                    echo "<script>alert('文件上传成功')</script>";
                }
            }else{
                echo "<script>alert('仅允许上传jpg和png格式的图片')</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';
    }

运行实例 »

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



特码的,写了一整天,劳资终于抄出来了,日


实例

<?php
$fh = opendir('zuoye') or die('不能打开');
while(false != ($file = readdir($fh))){
    if ($file != "." && $file != ".."){
        echo $file;
    }
}
closedir($fh);
echo '<hr>';

$filearr = scandir('zuoye');
foreach ($filearr as $item) {
    if ($item != "." && $item != ".."){
        echo $item;
    }
}

运行实例 »

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


QQ截图20180829145333.png


文件上传,实在太夸张了,语句各种套叠,好复杂

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