Week 5 —— File

Lab5.1 - File Uploads in PHP

写一个上传图片文件的PHP网页。如果文件不符合规范则提示错误信息。

如图:
在这里插入图片描述
在这里插入图片描述
如果文件过大
在这里插入图片描述
如果不是图片
在这里插入图片描述
等其他错误提示

index.php

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <link rel="stylesheet" href="css/styles.css">
        <form action="upload.php" method="post" enctype="multipart/form-data" >
            <fieldset>             
                <legend>Test File Upload</legend>             
                <p>                 
                    <label for="image">Upload image:</label>                 
                    <input type="file" name="image" >             
                </p>             
                <p>                 
                    <input type="submit" name="upload" value="Upload">             
                </p>              
            </fieldset> 
        </form>
        <?php
        // put your code here
        
        ?>

    </body>
</html>

upload.php

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        $errors = [];
        /*$_FILES = [
          "image" => ["name" => "Desert.jpg",
          "type" => "image/jpeg",
          "tmp_name" => "D:\xampp\tmp\php4884.tmp",
          "error" => 0,
          "size" => 845941]
          ]; */
        $filename = $_FILES["image"]["name"];
        $temp_file = $_FILES["image"]["tmp_name"];
        $type = $_FILES["image"]["type"];
        $size = $_FILES["image"]["size"];
        $errorLevel = $_FILES["image"]["error"];

        $error_messages = [
            "Upload successful",
            "File exceeds maximum upload size specified by default",
            "File exceeds size specified by MAX_FILE_SIZE",
            "File only partially uploaded",
            "Form submitted with no file specified",
            "",
            "No temporary folder",
            "Cannot write file to disk",
            "File type is not permitted"
        ];

        $destination = 'D:/xampp/htdocs/week6/upload_test/';
        $target_file = $destination . $filename;
        $max = 3000000;

        if ($errorLevel > 0) {
            // Set the error message to the errors array        
            $errors["image"] = $error_messages[$errorLevel];
            echo $errors["image"];
        } else {
            if (file_exists($temp_file)) {
                $size = $_FILES["image"]["size"];
                if ($size <= $max) {
                    $permitted = ["gif", "jpg", "jpeg","png"]; 
                    $ext = pathinfo($filename, PATHINFO_EXTENSION);
                    if (in_array($ext, $permitted)) {
                        move_uploaded_file($temp_file, $target_file);
                        echo"The file $filename has been uploaded.";
                    } else {
                        echo "$filename type is not permitted";
                    }
                } else {
                    $errors["image"] = "$filename is too big – upload failed";
                    echo $errors["image"];
                }
            } else {
                $errors["image"] = "File upload has failed";
                echo $errors["image"];
            }
        }
        ?>
    </body>
</html>

Lab5.2 - File Processing in PHP

读写文件,导入导出csv和json文件信息。

Slides Exercise :
index.php

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $f4 = fopen("nba.txt", "r");
        $content = fread($f4, filesize("nba.txt"));
        echo "Use fread() function:</br>";
        echo $content;
        fclose($f4);
        echo "</br></br>";
        
        $f5 = fopen("nba.txt", "r");
        echo "Use fgets() function:</br>";
        while (!feof($f5))
        {
            $line = fgets($f5);//读取一行
            echo "$line </br>";
        }
        fclose($f5);
        echo "</br>";
        
        $f6 = fopen("nba.txt", "r");
        $content = fread($f6, filesize("nba.txt"));
        $lines = explode("\n", $content);
        echo "Use explode()function:</br>";
        foreach ($lines as $line)
        {
            echo "$line</br>";
        }
        fclose($f6);
        echo"</br>";
        
        $line1 = "The quick brown fox jumps over the lazy dog\n";
        $line2 = "The rain in spain lays mainly on the plain\n";
        $f8 = fopen("data8.txt", "w");
        fwrite($f8, $line1);
        fwrite($f8, $line2);
        fclose($f8);
        
        $f7 = fopen("data7.csv", "r");
        echo "fgetcsv() function:</br>";
        while(!feof($f7))
        {
            $data = fgetcsv($f7);//读取一行并以数组形式返回
            print_r($data);
            echo '</br>';
        }
        fclose($f7);
        echo"</br>";
        
        $line = ['John','Smith','[email protected]', '041234566'];
        $f9 = fopen("data9.csv", "w");
        fputcsv($f9, $line);
        fclose($f9);
        
        $records = [['John','Smith','[email protected]', '041234566'],['Susan','Jones','[email protected]', '0412355678']];
        $f10 = fopen("data10.csv", "w");
        foreach ($records as $record)
        {
            $line = [];
            foreach ($record as $key => $value)
            {
                $line[] = $value;
            }
            fputcsv($f10, $line);
        }
        fclose($f10);
        
        $dbconfig = [
            "DSN" => "mysql:host=localhost;dbname=ebookingsdb",
            "USERNAME" => "root",
            "PASSWORD" => ""
        ];
        $json_data = json_encode($dbconfig);
        $f = fopen("dbconfig.json","w");
        fwrite($f, $json_data);
        fclose($f);
        
        echo"Import json data:</br>";
        $f = fopen("dbconfig.json", "r");
        $content = fread($f, filesize("dbconfig.json"));
        fclose($f);
        $json_data = json_decode($content);
        $dsn = $json_data->DSN;
        $username = $json_data->USERNAME;
        $password = $json_data->PASSWORD;
        echo "DSN:$dsn USERNAME:$username PASSWORD:$password";
        ?>
    </body>
</html>

运行结果:
在这里插入图片描述
在这里插入图片描述
Challenge Exercise 1 :
将一个联合数组的key和value分开导入一个csv文件里。

exportcsv.php

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $post = [
            "firstname" => "John",
            "lastname" => "Smith",
            "email" => "[email protected]",
            "mobile" => "0412345678",
            "dob" => "01/01/1970"
        ];
        foreach ($post as $key => $value)
        {
            $heading[] = $key;
            $line [] = $value;
        }

        $f1 = fopen("post.csv", "w");
        fputcsv($f1, $heading);
        fputcsv($f1, $line);
        fclose($f1);
        echo"post.csv created successfully";
        ?>
    </body>
</html>

运行结果:
在这里插入图片描述
Challenge Exercise 2
上述操作的逆过程,将csv里的信息整到一个联合数组里

importcsv.php

方法一:foreach方法

扫描二维码关注公众号,回复: 11153078 查看本文章
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $f2 = fopen("post.csv", "r");
        $keys = fgetcsv($f2);
        $values = fgetcsv($f2);
        $post = [];
        foreach ($values as $key=>$value)
        {
            $post[$keys[$key]] = $value;
        }
        print_r($post);
        ?>
    </body>
</html>

方法二:array_combine()函数

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $f2 = fopen("post.csv", "r");
        $keys = fgetcsv($f2);
        $values = fgetcsv($f2);
        $post = array_combine($keys, $values);
        print_r($post);
        fclose($f2);
        ?>
    </body>
</html>

运行结果:
在这里插入图片描述

Lab5.3 - Progress Activity

将login的form信息保存在一个csv文件里

在写好的login文件的count($errors) == 0时填上如下代码:

在这里插入图片描述
其他的不用变,login网页代码详见 Week 4 —— Forms in PHP

原创文章 85 获赞 46 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Deam_swan_goose/article/details/105011931