PHP /读取txt文件(或任何东西)

时间:2010-09-14 00:25:51

标签: php text-files readdir

我一直在寻找一段时间,但是像我一样的php-noob,无法让它发挥作用。

我想要完成的是一种在根目录中创建目录的方法,在每个目录中都有图像+一个txt文件。所以,假设你得到了:

 Root
 |
 +---Directory 1
 |   |
 |   +---Image1.jpg
 |   |
 |   +---Image2.jpg
 |   |
 |   +---Text.txt
 |
 +---Directory 2
     |
     +---Image1.jpg
     |
     +---Image2.jpg
     |
     +---Text.txt

我知道如何阅读和显示图像+名称。但是如何显示附带的文本文件? (内容,而不是文件的标题)。

非常感谢!

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用file_get_contents(http://php.net/manual/en/function.file-get-contents.php

echo file_get_contents($path_to_file);

更高级:fopen / fread http://php.net/manual/en/function.fread.php

答案 1 :(得分:0)

来自PHP fpassthru手册:

<?php

// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');

// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));

// dump the picture and stop the script
fpassthru($fp);
exit;

?>