Blogger Information
Blog 33
fans 0
comment 2
visits 37342
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件操作,目录操作,魔鬼变量,系统变量2018/8/28
cxw的博客
Original
946 people have browsed it

通过今天的学习,我懂得了,和对文件,目录的查询和创立,以及根据系统变量$FILES来判断文件上传类型,下面是我的代码:

1,文件上传与检测

实例

<!--accept设置上传文件类型(form 属性值有固定内容,不可自定义)

文件扩展名可以单独设置,也不可以不加限制。
-->
<meta charset="UTF-8">
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" METHOD="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" VALUE="4194304" >
    <fieldset>
        <legend>文件上传</legend>
        <p>选择文件上传<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']))
    {
        //判断文件选择类型
        $allowType=['image/jpg','image/jpeg', 'image/png'];
        if(in_array($_FILES['upload']['type'],$allowType))
        {
            //将文件移动到临时目录
           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  "错误的原因是:";
        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;
        }
    }
}

运行实例 »

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


2,目录遍历

实例

<meta charset="UTF-8">
<?php
/**
 * 扫描读取文本目录
 */

//1,打开目录
//
//接收打开目录失败或成功返回的状态false or true
$result=opendir('../0828') or  die('打开失败');

//
while(false!=($file=readdir($result)))
{
    if($file!='.'&&$file!='..')
    {
        echo  $file.'<br>';
    }

}
//关掉打开的目录
closedir($result);

//第二种 使用candir($dir)函数

//用数组来接收打开目录返回的数据
$arr=scandir('../0828');
foreach($arr as $value)
{
    if($value!='.'&&$value!='..')
    {
        echo  $value.'<br>';
    }
}

运行实例 »

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


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