PHP学习笔记 15

来源:互联网 发布:防蓝光夹片 知乎 编辑:程序博客网 时间:2024/06/08 09:21

打开与关闭

  • 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,intlength ) 从文件 $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);?>