problems including local file not on server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

problems including local file not on server

Post by jaymoore_299 »

I want to be able to include a function file, but include doesn't seem to be able to read it.

I have no problems accessing local files with code like this.

Code: Select all

$filename = "C:\Documents and Settings\User\Desktop\aaa.htm";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
But when I ran a call to the function testfunction() with only the include like this:

Code: Select all

testfunction();
include "C:\Documents and Settings\User\Desktop\functions.php";
contents of functions.php is

Code: Select all

<?php
function testfunction()
{
   echo "Example function.\n";
   return 2;
}
?>
I get the error message:
Fatal error: Call to undefined function readit() in C:\Documents and Settings\Us
er\Desktop\regex.php on line 24

However, when I try doing it like this just as a test:

Code: Select all

testfunction();
function testfunction()
{
   echo "Example function.\n";
   return 2;
}
include "C:\Documents and Settings\User\Desktop\functions.php";
I get the error message:
Fatal error: Cannot redeclare readit() (previously declared in C:\Documents and
Settings\User\Desktop\regex.php:34) in C:\Documents and Settings\User\Desktop\fu
nctions.php on line 6

So include seems to be declaring that function, but however I can't seem to be able to use it. Anyone know what's going on?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Try including the file before you call the function.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Btw, in this stage you should use require.

And a \ in a string should be escaped. But the easiest is to use / in a path:

Code: Select all

$path = "c:/Documents and Settings/user/Desktop";
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

Post by jaymoore_299 »

I included before the function and it works fine now. I wonder why when I include the function it must be before the function, but when I write the function out, then it can be anywhere in the script. This is just a minor issue though.
Post Reply