Page 1 of 1
How do I find the directory of a file
Posted: Tue Sep 26, 2006 3:02 pm
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
Posted: Tue Sep 26, 2006 3:12 pm
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']
Posted: Tue Sep 26, 2006 3:46 pm
by thephd
Weirdan | Please use 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...
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
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]
Posted: Tue Sep 26, 2006 4:55 pm
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).
Posted: Tue Sep 26, 2006 5:20 pm
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
Posted: Tue Sep 26, 2006 5:24 pm
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
Posted: Tue Sep 26, 2006 6:32 pm
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...
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
Posted: Tue Sep 26, 2006 6:49 pm
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.
Posted: Tue Sep 26, 2006 7:01 pm
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
Posted: Wed Sep 27, 2006 12:27 am
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