tags:

views:

18

answers:

1

Isn't for example this supposed to go to the server root on a site and get the sometext.txt from there?:

<?php require '/sometext.txt'; ?>

When I run this in a script, I get "failed to open stream: No such file or directory".

I vaguely remember using the forward slash in a require/include function before, and that it has worked..

I use Apache as webserver.

+2  A: 

I think you need a dot in front of the slash

<?php require('./sometext.txt'); ?>

or something like this would be better

<?php
   require $_SERVER['DOCUMENT_ROOT'].'/sometext.txt';
?>
krike
`./` and `/` are two different things. `./` means "here", `/` means the server root (not 100% sure which one the OP wants). And `require` is a language construct and doesn't need parentheses.
deceze
ok my bad, but in the php manuel it is referenced as require(). I edited my answer.
krike
The description of `include`, which `require` links to, has this: `Because include() is a special language construct, parentheses are not needed around its argument.`
deceze
What I don't get is why the slash doesn't go to the root.. I know I can use $_SERVER['DOCUMENT_ROOT'], but '/' would be so much easier and faster.
Nisto
@Nisto - I think you're confusing server root with the disk filesystem root. /sometext.txt will look for the file in the filesystem root, not in the web server root directory , which is typically something like /usr/local/apache/htdocs
Mark Baker
Guess I'll just have to use $_SERVER['DOCUMENT_ROOT'] then. Seems like "/" only works on Linux after reading some articles.
Nisto
@Nisto - / works on both Linux and Windows (and probably Mac too), but it's the __disk filesystem root__, not the web root directory... the web root directory ($_SERVER['DOCUMENT_ROOT']) is a subdirectory on the disk filesystem.
Mark Baker