Blogger Information
Blog 39
fans 0
comment 0
visits 30862
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
dir目录的遍历,文件上传与错误检查小案例 2018年8月28日 22:05
南通税企通马主任的博客
Original
600 people have browsed it

作业一 : 目录遍历方式

实例

<meta charset="UTF-8">
<?php

echo '<h2>目录遍历方式</h2>','<hr>';

//1.opendir(),打开目录句柄;
$text = opendir('../0828') or die ('打开失败');

//2.readdir(),从目录句柄中读取条目
//用户字符串方式遍历出目录下面的文件
while (false != ($dir = readdir($text)))
{
    if ($dir != "." && $dir != '..'){
        echo $dir."\n";
    }
}
//3.closedir()关闭目录句柄
closedir($text);
echo '<hr>';

//4.scandir()列出指定路径中的文件和目录
//既然scan出的是目录+文件,拿就是一个数组啦,就用数组来遍历
$arr1 = scandir('..\0828');
echo var_export($arr1),'<hr>';
foreach ($arr1 as  $dir)
{
    if ($dir !="." && $dir !="..")
    {
    echo $dir."\n";
    }
};

?>

运行实例 »

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


作业二 : 文件上传

实例

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
    <style>
        body form fieldset {
            border-color: blue;
        }
        legend {
            text-align: center;
            color: red;
        }
        span {
            font-size: 12px;
            margin-right: 5px;
        }
        button {
            margin-top: 3px;
            margin-left: 85px;
        }
    </style>
</head>
<body>
<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>文件上传</legend>
        <span>选择文件:</span><input type="file" name="upload">
    </fieldset>
    <button type="submit" name="submit">上传</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_FILES['upload'])) {
        $allow = ['image/jpe','image/png','image/jpeg'];
        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('文件格式不正确')</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 '';}
};


?>

</body>
</html>

运行实例 »

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

总结 : 这次作业有点赶 , 公司事情比较多 , 基本保持了进度把 ...

Correction status:Uncorrected

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