![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
Include Problemo
Alright what im trying to do is include a file, and put its contents into a string. This is basicly how my content system goes, all my content is stored in the info folder and has a .html extention, my file handler, info.php handles and includes them into a string, I cannot just print them out because template.php is included at the end and it prints the variable out...
since im sure you didnt understand any of that except put the file contents in a string, heres the code I got from php.net which is not working:
$page = $_GET['page'];
function getContents($filename) {
/* save the current buffer */
$buffer = ob_get_contents();
/* write the include */
include $filename;
$output = substr(ob_get_contents(),strlen($buffer));
/* restore the original buffer */
ob_clean();
echo $buffer;
return $output;
}
if (file_exists("info/".$page.".html")) {
$page[content] = getContents('info/'.$page.'.html');
}All I get is the first character of the file, I tried a diffrent example to, and no success. I will add in the directory jumping protection later, even though its not really needed all thats allowed to be opened is .html files. One more thing I want to change is instead of the urls being info.php?page=something I just want into.php?something any idea on how to do that? since all the $_GET array is, is the name of the value, but the values empty so im not sure how I could get the name.... |
|
|
|
|
|
#2 |
|
Programmer
|
Try turning output buffering on. That's what all the "ob_" functions refer to (and they don't work without it). Like this (changes in bold),
$page = $_GET['page'];
function getContents($filename) {
//start the buffer
ob_start();
/* write the include and pull contents from buffer*/
include $filename;
$output = ob_get_contents();
/* restore the original buffer */
ob_end_clean();
return $output;
}
if (file_exists("info/".$page.".html")) {
$page[content] = getContents('info/'.$page.'.html');
}As always, for more, read the documentation. EDIT: Hmm.. I jsut saw something else that looked a little funny. All that stuff with the $buffer variable is rubbish. You can probably take that right out... (I'll change it in the code above).
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? |
|
|
|
|
|
#3 |
|
Professional Programmer
|
If you want to read it into a string, why not use fopen instead of include?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|