Jump to content

Help Needed Badly


DJCMBear

Recommended Posts

Hi i'm in need of a bit of help with a function and even though im good at php coding I don't know how to form this function.

 

What I want to do:

What I want to do is create a function that will use the file read function but not read the php codes inside that file.

e.g. <?php print "hello"; ?> would just send out the word hello, php codes that get information from a database would do the same, it would print out the information being written by the php code.

 

Example of the database php codes:

 

<?php
$sql = "SELECT * FROM TABLE";
$res = mysql_query($sql);
if($mysql_num_rows($res)){
while($row = mysql_fetch_array($res)){
print $row["table_data"];
}
}
?>

Lets say $row["table_data"] equals the name "Bob", that php code will send out the name Bob plus other names that could be printed from that php code and leave the php coding out so it does not get printed after  being put through the ReadFile function.

 

The Files:

  • header.inc
  • footer.inc
  • index.php

 

The Function:

 

<?php
function ReadFile($file){
$fh = fopen($file, 'r');
$theData = fread($fh, filesize($file));
fclose($fh);
return $theData;
}
?>

All the function above does is reads the file and that means the text between the <? & ?> tags also get read, what I want to know is how I could make a function that calls the page as it is ment to be shown.

 

E.G. - If I wanted to get the current year and on the "footer.inc" page I write <?=date('Y');?> what fopen does is read that as it is but what I need it to do is to run the code as it would normaly do and for example it would print out "2010" and thats what fopen would read. So <?=date('Y');?> on the footer.inc file will print out 2010 after going through the ReadFile function.

Link to comment
Share on other sites

What you are asking for and what you really want are two different things. The only way you could read a script file and determine the output of that file would be to create your own PHP parser. Good luck with that!

 

Instead there is a much simpler solution. Just call the file via HTTP so the file is run through the server's PHP parser and you will only be reading the output. For example here is your directory structure on the server for your site:

 

c:\websites\mydomain.com\

 

Let's say the file in question exists at c:\websites\mydomain.com\subfolder\somefile.php

 

Instead of accessing the file through the directory structure

$file = "/somefolder/somefile.php";

 

reference it via the web

$file = "http://www.mydomain.com/somefolder/somefile.php";

 

That assumes the file is externally accessible.

 

Link to comment
Share on other sites

As you found out just reading the file wont work. if you include the file it will run it and spit the return to the page, which I get from your example you dont want. So what you need to do is use the function to include the php file(s) and buffer the output then assign the buffer contents to a var. The below example I did does just that, modify it as needed.

BTW. even though I included a text file, if it had php code in it would be executed with the include.

 

function readme()
{
ob_start();
include("./test.txt");
$test = ob_get_clean();
return $test;
}
$out = readme();
echo "$out";

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Thanks mjdamato for your reply and from what you suggested it's the same thing as i was going to do but I just wanted to know if it could be done on the server so that i could hide the files with a .ht 403 code but ill do it the easy way for now. And thanks to teamatomic for your reply and its a good function however i need the function to execute the file without the php codes there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.