我可以用 PHP 读取 .TXT 文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9861603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:48:07  来源:igfitidea点击:

Can I read a .TXT file with PHP?

phpfile

提问by Fredashay

As I start the process of writing my site in PHP and MySQL, one of the first PHP scripts I've written is a script to initialize my database. Drop/create the database. Drop/create each of the tables. Then load the tables from literals in the script.

当我开始用 PHP 和 MySQL 编写我的网站时,我编写的第一个 PHP 脚本之一是用于初始化我的数据库的脚本。删除/创建数据库。删除/创建每个表。然后从脚本中的文字加载表。

That's all working fine! Whoohoo :-)

一切正常!呜呼:-)

But I would prefer to read the data from files rather than hard-code them in the PHP script.

但我更愿意从文件中读取数据,而不是在 PHP 脚本中对它们进行硬编码。

I have a couple of books on PHP, but they're all oriented toward web development using MySQL. I can't find anything about reading and writing to ordinary files.

我有几本关于 PHP 的书,但它们都面向使用 MySQL 的 Web 开发。我找不到任何关于读取和写入普通文件的信息。

Yes, I know there's a gazillion questions here on stackoverflow about reading TXT files, but when I look at each one, they're for C or C# or VB or Perl. I'm beginning to think that PHP just can't read files :-(

是的,我知道在 stackoverflow 上有无数关于阅读 TXT 文件的问题,但是当我查看每个问题时,它们是针对 C 或 C# 或 VB 或 Perl 的。我开始认为 PHP 无法读取文件:-(

All I need is a brief PHP example of how to open a TXT file on the server, read it sequentially, display the data on the screen, and close the file, as in this pseudo-code:

我只需要一个简短的 PHP 示例,说明如何在服务器上打开 TXT 文件、按顺序读取它、在屏幕上显示数据并关闭文件,如以下伪代码所示:

program readfile;
handle = open('myfile.txt');
data = read (handle);
while (not eof (handle)) begin
    display data;
    data = read (handle);
    end;
close (handle);
end;     

I will also need to write files on the server when I get to the part of my site where people upload avatars, and save them as JPG or GIF files. But that's for later.

当我到达人们上传头像的网站部分时,我还需要在服务器上写入文件,并将它们保存为 JPG 或 GIF 文件。但那是以后的事。

Thanks!

谢谢!

回答by JKirchartz

From the PHP manual for fread():

来自PHP 手册fread()

<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>

EDITper the comment, you can read a file line by line with fgets()

根据评论编辑,您可以逐行读取文件fgets()

<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
?>

回答by hakre

All I need is a brief PHP example of how to open a TXT file on the server, read it sequentially, display the data on the screen, and close the file, as in this pseudo-code:

我只需要一个简短的 PHP 示例,说明如何在服务器上打开 TXT 文件、按顺序读取它、在屏幕上显示数据并关闭文件,如以下伪代码所示:

echo file_get_contents('/path/to/file.txt');

Yes that brief, see file_get_contents, you normally don't need a loop:

是的,简短,看file_get_contents,您通常不需要循环:

$file = new SPLFileObject('/path/to/file.txt');
foreach($file as $line) {
    echo $line;
}

回答by Madara's Ghost

Well, since you're asking about resources on the subject, there's a whole book on it in the PHP.net docs.

好吧,既然您要询问有关该主题的资源,那么PHP.net docs 中有一整本书。

A basic example:

一个基本的例子:

<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>

回答by Harry

Why you not read php documentation about fopen

为什么你不阅读关于fopen 的php 文档

 $file = fopen("source/file.txt","r");
  if(!file)
    {
      echo("ERROR:cant open file");
    }
    else
    {
      $buff = fread ($file,filesize("source/file.txt"));
      print $buff;
    }

回答by Zed

file_get_contentsdoes all that for you and returns the text file in a string :)

file_get_contents为您完成所有工作并以字符串形式返回文本文件:)

回答by Zed

You want to read line by line? Use fgets.

你想逐行阅读吗?使用fgets

$handle = @fopen("myfile.txt", "r");
if ($handle) {
    while (($content = fgets($handle, 4096)) !== false) {
        //echo $content;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}