tags:

views:

49

answers:

2

Heya all..

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 how do I display the accompanied textfile? (The content that is, not the title of the file).

Much appreciated!

+1  A: 

Hi,

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

sled
for binary files and large files see Paulo's answer below.
sled
file_get_contents() sucks the entire contents of the file into memory before returning, so for large files this will kill the script when PHP's `memory_limit` is exceeded. `readfile()` is a better option as it'll read/transmit the file in small chunks.
Marc B
And how about a automatically display? Without defining the filename in PHP? Like.. Users can put stuff in a directory, and the code will read all of them and display it automatically.
Don Munter
first solution: use a database and store the path to the file which the user has uploaded. second solution: list all files in the user's upload directory ( http://php.net/manual/en/book.dir.php ) or use glob http://php.net/manual/en/function.glob.php
sled
A: 

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;

?>
Paulo Scardine
Just omit the header calls and you can fpassthru your textfile anywhere in your PHP page.
Paulo Scardine
Ouch, I've put this question in the comment above as well. How can I reply here? :P
Don Munter
Anyway, how do I make the script so.. That it will automatically read out the txt files from the dir's, without defining it's names in the php code.
Don Munter