Jump to content

readfile() help!


elsombreron

Recommended Posts

I'm kind of self-taught here, so apologies if this is stupid or simple.

 

I have a couple of php files that have some content that gets repeated throughout all my site's pages, so to save me having to replicate the content every time, I have readfile() call the base content when I need it.

 

So I have something like this:

 

---Start base file called base.php (5 lines as an example)----

<?

//some PHP code here

$img_source="path/to/somewhere";

?>

<h1>Some header</h1>

---End base file called base.php----

 

---Start new file called newfile.php----

<?

readfile(base.php);

?>

<img src="<?php echo($img_source);?>">

---End base file called newfile.php----

 

In the newfile.php file, the readfile(base.php) works, because I can see the resulting source code to be this:

 

-------Resulting code when newfile.php is loaded------

<h1>Some header</h1>

<img src="">

------------------------------------------------------------

 

I can see that the header HTML code was pulled in from base.php, but I can also see that the variable $img_source, which was defined in base.php but called in newfile.php isn't recognized (see the empty src="" bit)!

 

I'm scratching my head here since I thought readfile() was essentially just copying and pasting base.php's contents into newfile.php, but something is going on with the PHP that I've written that I'm missing, obviously.

 

Any pointers would be more than appreciated, I've spent ages on this and still can't figure it out!

 

Many thanks.

 

 

Link to comment
https://forums.phpfreaks.com/topic/152341-readfile-help/
Share on other sites

Thanks monokrome! I tried require() and include() but these have been disabled by the server. From reading around, I think a lot of people prefer to not use require or include for security reasons. Any other simple way around this other than enabling require() or include()?

Link to comment
https://forums.phpfreaks.com/topic/152341-readfile-help/#findComment-800074
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.