Blogger Information
Blog 34
fans 1
comment 0
visits 23088
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP基础6作业08-28
theYon的博客
Original
733 people have browsed it

PHP基础6

主要知识概述

1)文件操作

2)目录遍历操作

3)文件上传

代码

// 目录遍历
// method-1
$dir = opendir('../0826') or die('fail to open');
while(($file = readdir($dir)) != false) {
    if($file != '.' && $file != '..') {
        print $file."<br>";
    }
    
}
closedir($dir);
echo '<hr>';

// method-2
$dirArr = scandir('../0826/');
foreach($dirArr as $file) {
    if($file != '.' && $file != '..') {
        echo $file.'<br>';
    }
}

运行结果

TIM截图20180830211242.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<!--    <form action="demo3.php" method="get">
        <label for="upload">上传</label>
        <input type="file" id="upload" name="upload">
        <p>
            <button>上传</button>
        </p>
    </form> -->

    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data">
        <!-- <input type="hidden" name="MAX_FILE_SIZE" value="542488" > -->
        <!-- <label for="upload">上传</label> -->
        <h2>文件上传</h2>
        <input type="file" name="upload">
        <p>
            <button type="submit" name="submit">上传</button>
        </p>
    </form>
</body>
</html>

<?php


// 判断是否post请求
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $file = $_FILES['upload'];
    // 检测文件是否被上传
    if(isset($file)) {
        // 文件类型设置
        $allow = ['image/jpg','image/jpeg','image/png'];
        if(in_array($file['type'],$allow)) {
            // 将文件移动到临时目录
            if (move_uploaded_file($file['tmp_name'], "images/{$file['name']}")){
                echo "<script>alert('文件上传成功')</script>";
            }
        } else {
            echo "<script>alert('仅允许上传jpg和png格式的图片')</script>";
        }
    }
    // 错误上传处理
    $error = $file['error'];
    if($error > 0){
        switch($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 '系统未知错误';
        }
    }
} else{
    echo 'none';
}

?>

运行结果

TIM截图20180830211355.png

TIM截图20180830211411.png

TIM截图20180830211434.png

总结

    通过这次课程,难度也稍微加大了,同时也能感受到PHP其中的简便,因此,重复练习十分重要,主要是它的语法功能封装得很强大(个人认为),所以要多看手册

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