N
N
nojoke2014-06-14 15:16:33
PHP
nojoke, 2014-06-14 15:16:33

Can't change file permissions with php chmod?

Can't change file permissions with chmod php

<?php
echo "Current user:".get_current_user()."\r\n";
$processUser = posix_getpwuid(posix_geteuid());
print "Process user:".$processUser['name']."\r\n";
chdir( dirname ( __FILE__ ) );
//$old = umask(0);
$fname='./1.js';
//$fname= dirname ( __FILE__ ).'/1.js';
echo $fname."\r\n";
echo "current perm:".substr(sprintf('%o', fileperms($fname)), -4)."\r\n";
$permitions=fileperms($fname);
chmod($fname,0444);
echo "Try to 444:\r\n";
echo "current perm:".substr(sprintf('%o', fileperms($fname)), -4)."\r\n";
echo "Return:\r\n";
chmod($fname,$permitions);
echo "current perm:".substr(sprintf('%o', fileperms($fname)), -4)."\r\n";
//umask($old);
?>

Output:
Current user:apache
Process user:apache
./1.js
current perm:0644
Try to 444:
current perm:0644
Return:
current perm:0644

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nojoke, 2014-06-15
@nojoke

In fact, everything changed.
fileperms cached the previous result.
Inserted clearstatcache(); before fileperms and everything was displayed as expected.

F
FilimoniC, 2014-06-14
@FilimoniC

print_r(posix_getpwuid(fileowner($filename)));
Who is the owner of the file? Must be apache, otherwise it will fail

S
svd71, 2014-06-14
@svd71

most likely the owner of the file is someone else. Try changing the owner.
since you have an apache user, I dare to assume that the actions are carried out under Windows. On niks systems, the files are usually owned by the www-data user.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question