开发者

chmod setting the permissions value dynamicly

开发者 https://www.devze.com 2023-01-23 06:48 出处:网络
Ok test 1 and 2 work fine, but If I cast the value from an array it doesn\'t work. $file = \'/test/file.xlsx\';

Ok test 1 and 2 work fine, but If I cast the value from an array it doesn't work.

$file = '/test/file.xlsx';
echo "Original Permissions: ".substr(decoct(fileperms($file)),2)."<br />\n\r";

// test 1
$permission = 0775;
chmod($file,$permission);
clearstatcache();
echo "Test 1 Permissions: ".substr(decoct(fileperms($file)),2)."<br />\n\r";

// test 2
define("PERMISSION", 0775);
chmod($file,PERMISSION);
clearstatcache();
echo "Test 2 Permissions: ".substr(decoct(fileperms($file)),2)."<br />\n\r";

Output:

Original Permissions: 1407<br />
Test 1 Permissions: 0775<br />
Test 2 Permissions: 0775<br />

Why doesn't this work?

// $ini_array['excel_file_info']['excel_file_permission'] 
// is in a ini file with the value set to 0775
if(isset($ini_array['excel_file_info']['excel_file_permission'])) {
    $excel_file_permission  = $ini_array['excel_file_info']['excel_file_permission'];  
    define("EXCEL_FILE_PERMISSION", $excel_file_permission);
} else {
    $excel_file_permission  = 0777; 
    define("EXCEL_FILE_PERMISSION", $excel_file_permission);
}

echo "Permissions Before chmod: ".substr(decoct(fileperms($file)),2)."<br />\n\r";
chmod($file,EXCEL_FILE_PERMISSION);
clearstatcache();
echo "Permissions After chmod: ".substr(decoct(fileperms($file)),2)."<br />\n\r";;
chmod($file,0755);
clearstatcache();
echo "Permissions Hard Coded chmod: ".substr(decoct(fileperms($file)),2)."<br />\n\r";;

I get this for file permissions:

// Before I chmod
Permissions Before chmod: 0644<br />

// Using the DEFINED CONSTANT w/ set value to 0775
Permissions After chmod: 1363<br />

// Hard Coded 0755
Permissions Hard Coded chmod: 0755<br />

EDIT:

// test 3
$permission = array('perm' => 0775);
chmod($file,$permission['perm']);
clearstatcache();
echo "T开发者_StackOverflow中文版est 3 Permissions: ".substr(decoct(fileperms($file)),2)."<br />\n\r";

Test 3 works, but still not the main example. UGH!!!

EDIT #2:

I think I found the problem, when I echo the type of the variable it's a string.

echo "Defined Excel File Permission: ".EXCEL_FILE_PERMISSION."\n\r";
echo "Defined Type: ".gettype(EXCEL_FILE_PERMISSION)."\n\r";

Defined Excel File Permission: 0775
Defined Type: string

Why is this?


I think typecasting cause the problem. Try to change:

$excel_file_permission  = (int)$ini_array['excel_file_info']['excel_file_permission'];

to

$excel_file_permission  = intval($ini_array['excel_file_info']['excel_file_permission'], 8);


It probably doesn't work because you convert to an integer value when you do $excel_file_permission = (int)$ini_array['excel_file_info']['excel_file_permission'];. An integer is not the same as an octal value...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号