PHP學習筆記 15 - File

打開與關閉

  • fopen() 打開文件,第一個參數文件名,第二個參數打開模式,與C語言類似(見下表),返回文件對象。

    模式 說明
    r 只讀
    w 只寫
    a 追加
    x 創建,並只寫
    r+ 讀寫,文件指針指向開頭
    w+ 讀寫,清除文件內容或創建新文件(如果不存在),文件指針指向開頭
    a+ 讀寫,文件指針指向末尾,如果文件不存在則創建
    x+ 創建新文件並讀寫,如果文件已經存在則返回FALSE並報錯
  • fclose() 關閉文件,參數爲 fopen() 返回的文件對象

讀文件

將要讀取的文件如下:

phpintro.txt

PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本預處理器”)是一種通用開源腳本語言。
語法吸收了C語言、Java和Perl的特點,利於學習,使用廣泛,主要適用於Web開發領域。
PHP 獨特的語法混合了C、Java、Perl以及PHP自創的語法。它可以比CGI或者Perl更快速地執行動態網頁。
PHP做出的動態頁面與其他的編程語言相比,PHP是將程序嵌入到HTML(標準通用標記語言下的一個應用)文檔中去執行,
執行效率比完全生成HTML標記的CGI要高許多;PHP還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。

讀取所有內容

  • readfile() 讀取文件所有內容,並寫入到輸出緩衝,返回讀取的字節數。

示例

readfile.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <title>Read File Content</title>
</head>
<body>
    <div class="container">
        <div class="well">
        <?php
        readfile("phpintro.txt");
        ?>
        </div>
    </div>

</body>
</html>

查看運行結果

讀取字節

  • string fread ( resource handle,int length ) 從文件 $handle$length 個字節,返回讀取的字節數

示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <title>Read File Content</title>
</head>
<body>
    <div class="container">
        <div class="well">
        <?php
        $f = fopen("phpintro.txt", "r");
        echo fread($f, 100);  // 讀取100個字節
        fclose($f);
        ?>
        </div>
    </div>

</body>
</html>

查看運行結果

讀取一行

  • fgets() 讀取文件中的一行
  • feof() 判斷是否讀取到文件未

示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <title>Read Line</title>
</head>
<body>
    <div class="container">
        <div class="well">
        <?php
        $f = fopen("phpintro.txt", "r") or die("Unable to open file!");
        $line = 0;
        while (!feof($f)) {
            echo "line " . ++$line . ": " . fgets($f) . "<br>";
        }
        fclose($f);
        ?>
        </div>
    </div>

</body>
</html>

查看運行結果

讀取字符

  • fgetc() 讀取一個字符

示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <title>Read Char</title>
</head>
<body>
    <div class="container">
        <div class="well">
        <?php
        $f = fopen("phpintro.txt", "r") or die("Unable to open file!");
        $line = 0;
        while (!feof($f)) {
            echo fgetc($f);
        }
        fclose($f);
        ?>
        </div>
    </div>

</body>
</html>

查看運行結果

寫文件

  • fwrite() 用來寫文件,第一個參數是文件對象,第二個參數是要寫入的字符串。

文件上傳

  • 需要設置 php.ini 中的 file_uploads = On
  • 表單 method 必須爲 post
  • 表單 enctype 必須爲 multipart/form-data
  • 處理上傳的PHP腳本使用 $_FILES 獲取上傳的文件信息

示例

上傳頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <title>File Upload</title>
</head>
<body>

    <div class="container">
        <form action="upload.php" method="post" enctype="multipart/form-data">
            選擇要上傳的文件:
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="上傳" name="submit">
        </form>
    </div>

</body>
</html>

處理頁面

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章