?
Solved

php & chmod

Posted on 2005-03-16
22
Medium Priority
?
994 Views
Last Modified: 2009-07-29
Linux OS
Is there anyone interested in doing the following for 500 points?
1) clean up the code below
2) tell me what I should be looking for, "chmod" doesn't want to work, the files and directories involved are owned by user running the script, I tried to "chown" the script to "root" without success,    ---   echo $file,"<br>";  works and gives the correct output

<?
$filename="dire.txt";
$files=array();
$files=file("$filename");

foreach($files as $file)
{
$file="/home/meeting/chair" . $file;
echo $file,"<br>";
chmod($file, 0755);  // octal; correct value of mode
}

echo "last modified ".date('d F Y, H:i:s.',getlastmod());
?>

Thank you for your help
0
Comment
Question by:rblampain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
  • 5
  • +1
22 Comments
 
LVL 9

Accepted Solution

by:
keteracel earned 1050 total points
ID: 13563577
just looks to me like you've missed the forward slash off the file, i.e.:

<?
$filename="dire.txt";
$files=file("$filename");

foreach($files as $file)
{
$file="/home/meeting/chair/" . $file;
echo $file,"<br>";
chmod($file, 0755);  // octal; correct value of mode
}

echo "last modified ".date('d F Y, H:i:s.',getlastmod());
?>

This works fine for me using PHP 4.3.10 on Linux,

keteracel
0
 

Author Comment

by:rblampain
ID: 13564778
Thanks to keteracel
No, that's not the problem, the file "dire.txt" contains filenames starting with a couple of directories like:
/dir1/dir2/filename
Just to check it, I  issued the command "emacs " followed by a "cut & paste" of one line of the output created by the 'echo $file,"<br>"; ' line and this editor gets the correct file.
0
 
LVL 25

Assisted Solution

by:Marcus Bointon
Marcus Bointon earned 300 total points
ID: 13565742
Does the user running the script have write access to the directory the items are in? Have you tried 'chmod 777 .' to test it?
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 9

Expert Comment

by:keteracel
ID: 13565903
try to use php to write to a file. If it can't do that then you're not going to be able to change permissions... try:

<?

$fp = fopen ("test.txt", "w");
fwrite($fp, "test");
fclose($fp);

?>

If this file is not created then your permissions are messed up!

0
 

Author Comment

by:rblampain
ID: 13566905
To Squinky:
 yes user had write access to directory holding the files, it was 755, I made it 777 but no difference, see below!

To keteracel:
the file is not created but I can't figure out where the permissions are incorrect.
The script is run from
 /home/meeting/chair
and if affects files held in
/home/meeting/chair/test/a0
I made the directories "test" and "test/a0" 777 but it doesn't help.  

Any other idea?


0
 
LVL 11

Assisted Solution

by:ZhaawZ
ZhaawZ earned 150 total points
ID: 13568589
Enabled safemode (for php) can be a matter why chmod doesn't want to work. It can be also a problem to access files that have different user (even if chmod allows it), when safemode is enabled.
0
 

Author Comment

by:rblampain
ID: 13571959
To ZhaawZ:
/etc/php.ini says "safe mode = off"
0
 
LVL 9

Expert Comment

by:keteracel
ID: 13573282
we can try a nasty workaround using ftp!

<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

$filename="dire.txt";
$files=file("$filename");

foreach($files as $file)
{
  $file="/home/meeting/chair" . $file;
  echo $file,"<br>";

  // try to chmod $file to 755
  if (ftp_chmod($conn_id, 0755, $file) !== false) {
    echo "last modified ".date('d F Y, H:i:s.',getlastmod());
  } else {
    echo "could not chmod $file\n";
  }
}

// close the connection
ftp_close($conn_id);
?>
0
 
LVL 9

Expert Comment

by:keteracel
ID: 13573293
the above should work (if you have PHP set up with the ftp module). What's probably happening is that PHP (or maybe Apache) is being run as nobody so PHP has no privileges. Using ftp, you can use a user with appropriate privileges as long as you enable your ftp service in Linux.
0
 

Author Comment

by:rblampain
ID: 13574028
Thanks to keteracel
I have "ftp" disabled and i'd like to exhaust all other options before I try this solution although it's good to know that something should work.
I've changed the Apache configuration file to reflect this user and its group but it makes no difference.
I had a look at "/etc/php.ini" but I can't find anything obvious that could help although I might simply miss it because I don't know.
I also had a look at "/etc/httpd/conf.d/php.conf" but this is only a couple of unrelated lines.
As far as permissions, PHP is the only suspect left but my understanding is that it will run under the permissions of user "chair" which has
permissions 777 over the directories below, or is there another file to configure?
After all the changes I rebooted the machine and having no success I restarted "httpd" (apache) but the result is always the same. I'll try to make the current directory 777 and see if it makes any difference.
0
 

Author Comment

by:rblampain
ID: 13575836
This was already 777
0
 

Author Comment

by:rblampain
ID: 13576037
To keteracel:
I tried  your ftp script, I thought it was going to tell me something to the effect that FTP was disabled but it looks like the script got half way,  it printed the first filename on the screen and then froze.
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13576073
Check that your user change in apache actually happened. You shouldn't have to change something so drastic just to fix this.

If you're running PHP via mod_php (the usual arrangement), PHP will be running as the web server's user. If you're running PHP as a CGI, it will be running as some other user.

You can see all these settings in the output from phpinfo();
0
 
LVL 9

Expert Comment

by:keteracel
ID: 13576099
ok, let's try a little more debugging:

<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server);

if (!$conn_id) die("Unable to connect to ftpserver");

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if (!$login_result) die("Login Failed...");

$filename="dire.txt";
$files=file("$filename");

foreach($files as $file)
{
  $file="/home/meeting/chair" . $file;
  echo $file,"<br>";

  // try to chmod $file to 755
  if (ftp_chmod($conn_id, 0755, $file) !== false) {
    echo "last modified ".date('d F Y, H:i:s.',getlastmod());
  } else {
    echo "could not chmod $file\n";
  }
}

// close the connection
ftp_close($conn_id);
?>
0
 

Author Comment

by:rblampain
ID: 13576741
I had ftp disabled at installation time and of course I get the message "Unable to connect to ftpserver".  I could not find out how to enable it. Is this easy?
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13576948
Although FTP might let you work around this, it's a really ugly hack. It should be easier to just sort out ownership and permissions correctly.

Can you do a phpinfo() and post the bits we've been talking about? (not the whole thing!).
0
 

Author Comment

by:rblampain
ID: 13581176
To Squinky:
It's PHP HTTPD (Apache) installation
I had restored the original parameters in "httpd.conf" after I found out the change made no difference.

From phpinfo()
User/Group       apache(48)/48

From ls -l
drwxrwxrwx  37 chair meetings   4096 Mar 19 14:59 chair
drwxrwxrwx  69 chair meetings   4096 Feb 27 23:45 test
drwxrwxrwx   2 chair meetings   4096 Feb 27 23:45 a0
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13581920
You're not running apache chrooted are you? If so, apache probably can't see /home/meeting/chair, and may be looking in /var/www/home/meeting/chair instead (if it's chrooted in /var/www, like it is on OpenBSD).

You could try making apache a member of the meeting group to avoid making things world writable. One other thing - Don't set execute bits on files unnecessarily. Unless you're actually running PHP scripts from a CLI using #!, you don't need it - use 666 instead of 777 on files. Directories do need it if you're going to look in them.

You need to persevere with Keteracel's file creation test. It's a somewhat more obvious way of making sure you have appropriate access. Also check the return values to see if you get any errors reported.
0
 

Author Comment

by:rblampain
ID: 13582192
Thanks to Squinky
I'll follow your suggestion about making apache a member of meetings.
I found I probably had the $_SESSION() in the wrong place. I thought it had to be first in the PHP script but it looks like it's got to be first - full stop. My scripts seem to go a bit better since I made this change ($_SESSION before PHP and HTML)

Nothing 'chroot'ed
Execute bits not set.
Apache gives following error message:
[client 192.168.1.13] PHP Warning:  chmod(): Operation not permitted in /home/meeting/chair/gedit_file.php on line 25, referer: http://192.168.1.13/display_tree.php
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13583036
OK, firstly it's not clear what you're saying about $_SESSION - $_SESSION() makes no sense - it's a variable not a function. You have to say session_start() before you can use $_SESSION, so it's all within PHP, certainly not before it.

That error states that the script that was running was in /home/meetings/chair - the file that you're changing may not be in there. I suggest you print the full path to the file you're attempting to chmod (use realpath() to make sure it's right)
0
 

Author Comment

by:rblampain
ID: 13584151
I apologize about my lack of clarity, yes it's session_start().
Files to be chmod are in a number of directories below ~test/
/home/meeting/chair/test/a0
0
 

Author Comment

by:rblampain
ID: 13588772
It looks like the culprint was SELinux. It's off by default on Fedora C2 I'm using but disabling it seems to fix the problem. Even though you didn't provide the answer, your help was really ... helpful in narrowing the cause. I'll allocate the points according to your involvment and
I apologize because I really thought I was asking something simple from you all.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question