469,268 Members | 942 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Site Root directory?

Rv!
I've posted this onto alt.php earlier and stumped all involved.
I didn't cross post for the usual reasons Anyone here got any ideas?
--------------------------------------
I am simply attempting to find my sites root directory so I can then step up
into a folder
in which I will have my INCLUDE code. This will allow me to easily refer to
my
INCLUDE code without messing about.

Now, This code...

It simply attempts to put a file into the root of my server space, and
recall it. It doesn't work.

The file write doesn't error and the file read fails without error.
Most importantly the file is nowhere to be seen!

<Added when sent to the domain provider>
--------------------------------------
I am worried the file is dropping into a space not allocated to my account
and that
this could cause you/I some problems if it were to overwrite an important
file.
--------------------------------------
</Added when sent to the domain provider>

Robertv!


<?

$file="/tempfile.test";

#write a file to the server.
$thefile = fopen($file,"w");
fwrite ($thefile,"If you see this the file was successfully written to, and
re-read from the server as filename: $file") ;
fclose ($thefile);

#read the file from the server.
$thefile = fopen($file,"r");
$readdata=fread ($thefile,100000) ;
fclose ($thefile);
#display the results.
echo "Data file when read was :<HR>";
if($readdata=="")
{echo "Data came back empty. The file could not be read, and may not have
been written."; }
{echo $readdata."</HR>" ;}

#
#
#User comments...
# $file="tempfile.test"; will write to the current directory.
# $file="../tempfile.test"; will write up one directory.
# $file="/tempfile.test"; does some crazy disappearing act.
# Server phpinfo() snip...Windows NT localhost 5.2 build 3790
Jul 17 '05 #1
4 38910
Rv! wrote:

I am simply attempting to find my sites root directory so I can then step up
into a folder
in which I will have my INCLUDE code. This will allow me to easily refer to
my
INCLUDE code without messing about.

Now, This code...

It simply attempts to put a file into the root of my server space, and
recall it. It doesn't work.

The file write doesn't error and the file read fails without error.
Most importantly the file is nowhere to be seen!

<Added when sent to the domain provider>
--------------------------------------
I am worried the file is dropping into a space not allocated to my account
and that
this could cause you/I some problems if it were to overwrite an important
file.
--------------------------------------
</Added when sent to the domain provider>

Robertv!


<?

$file="/tempfile.test";
// Oops, first, this needs to be a system path, not a URI

$file=$_SERVER['DOCUMENT_ROOT'].'/tempfile.test';
#write a file to the server.
// should check if the file is writable
// (this means that the file must exist and have proper rights on the
// file system for the user that the webserver is running as)

if(is_writable($file)){
$thefile = fopen($file,"w");
fwrite ($thefile,"If you see this the file was successfully written to, and
re-read from the server as filename: $file") ;
fclose ($thefile);
}else{
echo 'The file is now writable by the webserver user.';
}
// Should check if it is readable as well.
if(is_readable($file)){
#read the file from the server.
$thefile = fopen($file,"r");
$readdata=fread ($thefile,100000) ;
fclose ($thefile);
}else{
echo 'Cannot read the file.';
}
#display the results.
echo "Data file when read was :<HR>";
if($readdata=="")
// need to modify this line now
if(!isset($readdata))
{echo "Data came back empty. The file could not be read, and may not have
been written."; }
{echo $readdata."</HR>" ;}


HTH

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #2
Rv!

Hmm. This is bringing back the entire path to my PHP that called
the lookup. ie.
\\nas04ent\domains\m\<MYDOMAIN>\user\htdocs\<FIRST DIRECTORY>\soft\soft.php

Interesting. I might just parse the file backwords until I get to the
\htdocs\ parts and add
a directory onthe end so it looks like
\\nas04ent\domains\m\<MYDOMAIN>\user\htdocs\librar y
where library is the directory I would like to call the include file from.
Seems a lot of hassle as I believ the / should throw me to the root folder
of my system,
not the bleeding servers entire data set! :(

I'm digging further now. Thanks for that input!

Robert

Jul 17 '05 #3
Rv!
So.. I stripped back the servers response to the text...
"\\nas04ent\domains\m\<MYDOMAIN>\user\htdocs"
and I attacked the text "\library" and tried to write a file to
it as...
"\\nas04ent\domains\m\<MYDOMAIN>\user\htdocs\libra ry\test.file"
and it failed with unwritable!

It seems I can't find my ROOT by that method either.
This is driving me nuts as it is virtually impossible for me to add my
includes to point to a file in another folder. I have to sit there and
count back down the folders and type in the right amount of
"../../../../library/code.txt"
until the bloody thing works!
Sick sick sick sick sick. I'm off to get a beer.

Robert

Jul 17 '05 #4
Rv!
I've been in discussion with the hosts who suggested I use their server
path as the include.. \\NAS01\users\m\<DOMAIN>\htdocs\...." etc
which would force me to hardcode the PHP code and stop me testing it
on anything other than their server. Instead if doing this I've written a
shart
directory crawler which will search out the include code.. It, and a message
originally sent to alt.php are included below...


Just thought about what they want me to do.
I would need to hardcode my program to use their
path command which would fail out on local tests or on moving to
another server etc. I've decided not to use their system. Sat down
for half an hours and wrote this simple function which will search
back directories looking for the file or failing after a short search.

It might not be great but it works on all tested systems (Their IIS Win2K3
and my Apache Win2Kas). Please excuse the coding if it's bad, but I'm
still learning this stuff! ;) Code is broken into little sections just to
see it clearer
when sending here.

Hope this seems a reasonable solution to the more experienced folkseses?
Any comments much appreciated.

Robert

<?
#find the include file! Preset the $inc value to the part of the path
we know.
#strlen part sets a maximum path length to force exit if file search
is unsuccessful.
?>
<? $inc="library/_usefulcode.txt";
while(!file_exists($inc) && strlen($inc)<70 ) { $inc="../".$inc;}

#check if the file actually exists or if we crashed out.
if (!file_exists($inc)) {die ("Include file could not be found in expected
path.");}?>
<?
#Display the path we found our file in!
echo "Include path is $inc so let's get it and use a function...<BR>";
require $inc;
echo GETTIME();
?>
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by engwar1 | last post: by
reply views Thread by Peter D. Dunlap | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.