Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 10th, 2005, 1:01 AM   #1
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
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....
brokenhope is offline   Reply With Quote
Old Aug 10th, 2005, 9:43 AM   #2
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
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?
EdSalamander is offline   Reply With Quote
Old Aug 10th, 2005, 10:38 AM   #3
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
If you want to read it into a string, why not use fopen instead of include?
Lich is offline   Reply With Quote
Old Aug 10th, 2005, 10:58 AM   #4
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
yeah fopen would be my choice. Or a db, which in my opinion is a far better option than a file that you read from.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC