How do I find the directory of a file

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
thephd
Forum Newbie
Posts: 6
Joined: Tue Sep 26, 2006 3:00 pm

How do I find the directory of a file

Post by thephd »

I'm trying to change the permissions of a file on my server using a script. This is because I'm going to be giving a series of scripts to people who may not know how to change permissions by themselves.

I'll be using this code, which I think does the trick:

chmod($file,777);

The $file will refer to a document that is the same for everyone, file.txt. However, the path will be different for everyone, right?

Like mine might be /home/est/public_html/file.txt

But it would be different for someone else. Is there a function I can use to find out what path to use to find public html files on their server? So I can easily locate file.txt on any server automatically through the script?

I've been searching online for over an hour but can't find how. Thanks for any help.

Cheers,
Stephen
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

As long as you know the relative location when compared with the chmod() script's location is constant, you don't even need a full path, relative pathing will do just fine.

getcwd() may be of interest, as may $_SERVER['DOCUMENT_ROOT']
thephd
Forum Newbie
Posts: 6
Joined: Tue Sep 26, 2006 3:00 pm

Post by thephd »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thanks a ton Feyd.  I think the relative path will work, and $_SERVER["DOCUMENT-ROOT"]; worked for me too.

So this...

Code: Select all

$path =$_SERVER["DOCUMENT_ROOT"];
	$file = "$dir/text.txt";
	chmod($file,0777);
got me the same as this...

Code: Select all

chmod(text.txt,0777);
However, I got this as a result...

Code: Select all

Warning: chmod() [function.chmod]: open_basedir restriction in effect. File(/text.txt) is not within the allowed path(s): (/home/est:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/est/public_html/chmod.php on line
Is this because I'm on a shared server? Is it even realistic to write a script that will chmod a file for a user on his/her domain at the click of the button?

Thanks anyone for your help. And thanks again Feyd.


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

thephd wrote:$file = "$dir/text.txt";
[...]
Warning: chmod() [function.chmod]: open_basedir restriction in effect. File(/text.txt) is not within the allowed path(s)
Seems like there is no variable $dir and you're running this script without E_ALL or E_NOTICE in error_reporting (or you've ignored the warning message).
thephd
Forum Newbie
Posts: 6
Joined: Tue Sep 26, 2006 3:00 pm

Post by thephd »

That's my fault. I'm not sure how I got that piece of code on there. It should have read like this...

$path =$_SERVER["DOCUMENT_ROOT"];
$file = "$path/text.txt";
chmod($file,0777);

But this works as well...

chmod(text.txt,0777);

And I still get that error message, about text.txt being in a restricted path.

Anyone know a piece of code I can use to change chmod of a file by clicking a button?

Thanks Volka and Feyd.

Stephen
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

thephd wrote:But this works as well...

chmod(text.txt,0777);

And I still get that error message, about text.txt being in a restricted path.
Please copy&paste the new code and the new message. Don't type it, copy&paste please.
And please use

Code: Select all

and [quote] tags here.
thephd
Forum Newbie
Posts: 6
Joined: Tue Sep 26, 2006 3:00 pm

Post by thephd »

Thanks for the tip on how to use the forum. My brain's melting trying to figure this out and I missed the post on how to format my questions. Thanks for your patience.

Again, here's what I'm trying to do, in case you guys know an easier way I haven't thought of...

I'm going to be distributing a few .php files and one .txt file. The .txt file needs to have the permissions changed to 755, but many people who I'll be sharing this with won't know how to do it. So I'm trying to do it for them in one of the .php files at the click of a button.

Here's what the button code looks like...

Code: Select all

<form name="form1" method="post" action="chmod.php">
  <input type="submit" name="Submit" value="Change Permissions">
</form>
Here's what the chmod.php looks like...

Code: Select all

<?php
          chmod("ct.txt",0755); 
?>
But here's what happens when I click the button...

Code: Select all

Warning: chmod() [function.chmod]: Operation not permitted in /home/est/public_html/chmod.php on line 5
I've also thought about creating the file at the click of a button. The text file simply contains one string. So I thought using fopen to create a file might work. Here's the code I used...

Code: Select all

<?php

$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

?>
That's just code I found on a website to try and test the idea. And I was going to customize it to fit my situation. But it gives me this...

Code: Select all

Warning: fopen(testFile.txt) [function.fopen]: failed to open stream: Permission denied in /home/est/public_html/install.php on line 4
can't open file
Are permissions from my web host getting in the way? And since I'll be distributing this to a wide group of people on different servers, is it even plausible to create code that will be able to create or modify this .txt file to permissions of 755 for everyone?

Thanks for your help and patience.

Stephen
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Ok, that are two new messages.
Warning: chmod() [function.chmod]: Operation not permitted in /home/est/public_html/chmod.php on line 5
Only the superuser and the owner of the file/directory are allowed to change the permissions. Obviously the script process is neither root nor the owner.
Warning: fopen(testFile.txt) [function.fopen]: failed to open stream: Permission denied in /home/est/public_html/install.php on line 4
can't open file
Also a permisson problem. The php process does not have the permisson to access the file testFile.txt.
thephd
Forum Newbie
Posts: 6
Joined: Tue Sep 26, 2006 3:00 pm

Post by thephd »

Thanks Volka...

I'm running the code on my domain, but I use a hosting service where I'm probably on a server with a ton of other web sites I don't know about. Is it my webhost who could be limiting my PHP permissions? I've contacted them but it has me thinking...

Since I need the script to be somewhat universal for random people hosting with random web hosts, is a script like this just not plausible?

Thanks for any help.

Cheers,
Stephen
thephd
Forum Newbie
Posts: 6
Joined: Tue Sep 26, 2006 3:00 pm

Post by thephd »

Hi guys, my webhost says this can't be done from the browser unless the owner is changed to nobody:nobody.... and that'd be dangerous.

Does that sound right? Should I abandon the attempt? It's going to be a big hit to my code if I can't do this, but I think I'm out of options. If anyone knows how to write a fairly universal code snippet that can change the permissions of a file on the same domain... please, please let me know.

Cheers,
Stephen
Post Reply