Blogger Information
Blog 38
fans 0
comment 0
visits 30681
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件上传与检测目录遍历总结——2018-8-30 17:36:0
图图的博客
Original
713 people have browsed it

文件上传与检测

实例

<?php
header("content-type:text/html;charset=utf-8");
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/30
 * Time: 15:58
 */
//删除文件
//@unlink('test.txt') or die('deleted');
//复制文件
//copy('test1.txt','../0827/test1.txt') or die('fail');
//移动操作,两个参数在不用的目录下
//rename('test1.txt','../0827/test.txt') or die ('fail');
//创建目录
//mkdir('admin');
//rename('test.txt','admin/1.txt');
//删除目录(只能删除空目录,先删除文件在删除目录)
//@unlink('admin/1.txt') or die('deleted');
//rmdir('admin');?>

<form action="" method="POST" enctype="multipart/form-data">
    <fieldset>
        <legend>upload</legend>
        <input type="file" name="file">
    </fieldset>
    <p align="center"><button type="submit" name="submit" >upload</button></p>
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    //检测是否有文件上传
    if(isset($_FILES['file'])){
        //设置允许上传的问价类型
        $allow = ['image/jpg','image/jpeg', 'image/png'];
        if(in_array($_FILES['file']['type'],$allow)){
            if(move_uploaded_file($_FILES['file']['tmp_name'], "upload/{$_FILES['file']['name']}")){
               echo "<script>alert('上传成功')</script>";
            }else{
                echo "<script>alert('支持的格式为:jpg/jpeg/png')</script>";
            }
        }

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

        switch ($_FILES['file']['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['file']['tmp_name']) && is_file($_FILES['file']['tmp_name'])) {
            unlink($_FILES['upload']['tmp_name']);
        }
    }
} else{
    exit('请求类型错误');
}

运行实例 »

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



运行实例 »

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

目录遍历

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/30
 * Time: 15:37
 */
//opendir()打开目录返回资源
//readdir()读取目录
//closedir()关闭当前目录
$dir = opendir('../0827');
//获取的资源不为空就显示

while (($file=readdir($dir) )!= false){
    //不显示.和..
    if($file != '.' && $file !='..'){
        print nl2br($file."\n");
    }
}
closedir($dir);

echo '<hr>';
/**
 * 第二种方式:将目录内容保存到数组中进行遍历
 * 第一步:scandir($dir)将目录转数组中保存
 * 第二步:遍历目录数组
 */
$dirArr = scandir('../0827');
foreach($dirArr as $file){
    //不显示.和..
    if($file != '.' && $file !='..'){
        print nl2br($file."\n");
    }
}
echo '<hr>';

运行实例 »

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


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