PHP / read txt files (or anything)
I've been searching for a while now, but php-noob as I am, can't get it to work.
What I'm trying to accomplish is a way to make directories in your root, with in each of them images + a txt file. So let's say you got:
Root
|
+---Directory 1
| |
| +---Image1.jpg
| |
| +---Image2.jpg
| |
| +---Text.txt
|
+---Directory 2
|
+---Image1.jpg
|
+---Image2.jpg
|
+---Text.txt
I know how to read and display the images + names. But ho开发者_如何学Cw do I display the accompanied textfile? (The content that is, not the title of the file).
Much appreciated!
the easiest way is using file_get_contents ( http://php.net/manual/en/function.file-get-contents.php )
echo file_get_contents($path_to_file);
More advanced: fopen/fread http://php.net/manual/en/function.fread.php
From PHP fpassthru manual:
<?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;
?>
精彩评论