当前位置: 动力学知识库 > 问答 > 编程问答 >

php - chmod setting the permissions value dynamicly

问题描述:

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 "Test 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...

分享给朋友:
您可能感兴趣的文章:
随机阅读: