How do I find the directory of a file
Moderator: General Moderators
How do I find the directory of a file
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
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
Weirdan | Please use
got me the same as this...
However, I got this as a result...
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]
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);Code: Select all
chmod(text.txt,0777);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 lineThanks 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]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 wrote:$file = "$dir/text.txt";
[...]
Warning: chmod() [function.chmod]: open_basedir restriction in effect. File(/text.txt) is not within the allowed path(s)
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
$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
Please copy&paste the new code and the new message. Don't type it, copy&paste please.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.
And please use
Code: Select all
and [quote] tags here.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...
Here's what the chmod.php looks like...
But here's what happens when I click the button...
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...
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...
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
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>Code: Select all
<?php
chmod("ct.txt",0755);
?>Code: Select all
Warning: chmod() [function.chmod]: Operation not permitted in /home/est/public_html/chmod.php on line 5Code: Select all
<?php
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
?>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 fileThanks for your help and patience.
Stephen
Ok, that are two new messages.
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: chmod() [function.chmod]: Operation not permitted in /home/est/public_html/chmod.php on line 5
Also a permisson problem. The php process does not have the permisson to access the file testFile.txt.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
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
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
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
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