Forum Moderators: coopster
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 :)
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!
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.
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 :)
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.
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 :)