Forum Moderators: coopster

Message Too Old, No Replies

readfile won't work

but include does...

         

johnglass

11:28 am on Mar 24, 2004 (gmt 0)

10+ Year Member



Hi everyone,

I'm a beginner with php. I'm trying to execute a few simple php scripts.

Anyway, I can't figure out why this won't work:

<?php
$trfile = 'artest.htm';
readfile($trfile);
?>

Even though this works perfectly:

<?php
$trfile = 'artest.htm';
include($trfile);
?>

I'm trying to include information from the "artest.htm" file, without printing the body (the way the "include" does). So, I figure easy, just use readfile instead...

But it gives me an error:

Warning: file(artest.htm): failed to open stream: No such file or directory in /blah/blah on line 7.

Is there something wrong with my syntax? Why will it access the file with include and not readfile?

Thanx :)

nostra

12:26 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



your file artest.htm does not exists in this root =)

johnglass

6:21 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Yeah, that would it seems to be doing. I think I got it fixed by pointing through a directory or two... Still though, why would "include" work and not "readfile"?

Anyway, it seems to be working better, but it's still printing the body. How do you get info from a htm file, without printing the body? Basically, I want it to do this:
1. Name the file (i.e. $targetfile = 'dir/name.file')
2. Get info from $targetfile, without printing the <body>
3. Echo a few $vars found inside $targetfile
4. Echo the body of $targetfile
This would be easy if I had to print the body first - just include it and echo the $vars afterwards. But it can't find the $vars from the file until the file is included, and if the file is included, it prints body. It's a vicious cycle :).

I dunno, like I said, I'm new at this, so maybe I'm going about it all wrong...

Thanx!

Warboss Alex

6:29 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



There's plenty of ways to get round this..

The easiest one is to have something like this in your include file:

//file = artest.php

<html>
<head>
..
</head>
<body>
<p>This is a $PARAGRAPH.</p>
</body>
</html>

And in your script:
//script.php

$PARAGRAPH = 'paragraph';
include 'artest.php';

That should output (in the browser):

This is a paragraph.

johnglass

8:01 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Hi,

Quick replies, thanx :) Yeah, I think I get that, but... Well, lemme show you what I have:

Files: artest.htm, test.htm

artest.htm

<html>
<head>
<?php
$trdate = '00/00/00';
$trtitle = 'Blah Blah';
?>
</head>
<body>
<p>Text text and more text.</p>
</body>
</html>

test.htm

<html>
<head>
<?php
$trfile = 'artest.htm';
include($trfile);
?>
</head>
<body>
<p><?php
echo $trtitle;
?>
<br>
<?php
echo $trdate;
?></p>
<p><?php
include($trfile);
?>
</p>
</body>
</html>

Test.htm outputs (I added the astricks):

*Text text and more text.
*
*Blah Blah
*00/00/00
*
*
*Text text and more text.

But I want it to just output:

*Blah Blah
*00/00/00
*
*
*Text text and more text.

See what I what by including without the body? I want it to go "backwards", use the $trtitle and $trdate vars before including the artest.htm body in the test.htm body. Trick is, it won't use those vars until I've included the file (and that prints the body).

That's why I tried putting the include in test.htm header, hoping it would read it without writing it. Then I thought maybe that's what readfile() is for, but that didn't seem to work either.

One thought I had is that maybe I need to write something like:
echo $trdate($trfile)

I doubt this syntax is correct (if this can be done, what would be correct)? In this case I guess I would just not include the file in the header, just in the body (after the echo statements), and the echo statements would look for the $vars in the file themselves?

How can I resolve this?

Thanx :)

daisho

8:54 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



readfile and include are different functions for different purposes.

readfile does not include the file into your current running program. It opens file file and byte for byte reads and sends the output to stdout.

Include on the other hand includes the file into your currently running program.

If the included file is just HTML they may look like they do the same thing but if you "readfile" a file with php the php code will be sent to the client whereas with include the php code will be interperated.

daisho.

johnglass

9:35 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Thanx Daisho, that helps me understand them better.

But I think the root of my problem is how to import vars from a file before they are included, or how to include the file in such a way that it doesn't include the body output.

I suppose I could "hide" the body (i.e. make the message body a php variable) but I was hoping for a way to make the message body plainly inside the <body> for output.

So I need to know how to one or a combination of these: import vars from a file before that file is include()'d; a variation of include() that would not print the body; import vars individually within a statement, something like echo $trtitle(artest.htm); or maybe a way to omit the body in the first include() statement.

I'm pretty sure it's one of those :)