Forum Moderators: coopster

Message Too Old, No Replies

Including a file within a function?

Including a file within a php function?

         

nealw

8:11 pm on Mar 15, 2004 (gmt 0)

10+ Year Member


Question: I am trying to include a text file from within a function. This "kinda" works, in that it will grab the text.inc file and display the contents of the include, but not in the place on the page where I call this function. This .inc include text is being displayed/printed on the 'top' of the page above the opening <HTML> tag. Which it should not be doing...?

Anyone have any ideas?

### Example:
<?php
function include_content_text() {

global $searchengine;
$fullcategory = $searchengine->category->data[full_name];

$splitdata = "$fullcategory";
list($state,$city) = explode(":",$splitdata);

//Let's remove some spaces from the city and state category names so we can create solid urls.
$newstate = str_replace(" ","_", $state);
$newcity = str_replace(" ","_", $city);

$navinclude = include ("guide_includes/$newcity.inc"[smilestopper]) ;

return $navinclude;
}
? >

ergophobe

8:48 pm on Mar 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



include does not return a value. To assign the contents to a variable, you will need to use something like

$x = file($filename);
$x = file_get_contents($filename);

Check out the PHP Manual Function Reference under Filesystem Functions

Tom

nealw

10:37 pm on Mar 15, 2004 (gmt 0)

10+ Year Member


ergophobe,

Thanks. I was able to track this down and modify it for my needs and works great...thanks for the pointer.

<?php

if (!function_exists('file_get_contents'))
{
function file_get_contents($filename, $use_include_path = 0)
{
$file = @fopen($filename, 'rb', $use_include_path);
if ($file)
{
if ($fsize = @filesize($filename))
{
$data = fread($file, $fsize);
}
else
{
while (!feof($file))
{
$data .= fread($file, 1024);
}
}
fclose($file);
}
return $data;
}
}

?>