DJCMBear Posted February 9, 2010 Share Posted February 9, 2010 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 9, 2010 Share Posted February 9, 2010 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. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 9, 2010 Share Posted February 9, 2010 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 Quote Link to comment Share on other sites More sharing options...
DJCMBear Posted February 9, 2010 Author Share Posted February 9, 2010 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. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 9, 2010 Share Posted February 9, 2010 So what you are saying is this: if the file has <?php $a=1; $b=2; $c=$a+$b; echo "$c"; ?> all you want returned is "3". Is that correct? HTH Teamtomic Quote Link to comment Share on other sites More sharing options...
DJCMBear Posted February 10, 2010 Author Share Posted February 10, 2010 yes so the only thing thats read is the number 3 and the rest of the php coding isnt read by the function. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 10, 2010 Share Posted February 10, 2010 Did you even try the function I gave you? HTH Teamtomic Quote Link to comment Share on other sites More sharing options...
DJCMBear Posted February 10, 2010 Author Share Posted February 10, 2010 Yes and it does work so thanks for your help and it took me a while to test the code because i only just got onto my server to do it and now i can finish my system off =) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.