2010-11-04 67 views
0

好的測試1和2工作正常,但如果我從數組中轉換該值不起作用。chmod動態設置權限值

$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"; 

輸出:

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

爲什麼不這項工作?

// $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";; 

我得到這個文件的權限:

// 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 /> 

編輯:

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

測試3級的作品,但目前還不是主要的例子。啊!!!

編輯#2:

我想我找到了問題,當我附和變量的類型是一個字符串。

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 

這是爲什麼?

回答

1

我認爲typecasting導致的問題。嘗試改變:

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

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

我改變它,它仍然無法正常工作 – 2010-11-04 14:49:26

+0

什麼是$ ini_array的電流值[ 'excel_file_info'] [ 'excel_file_permission']?嘗試將「0777」或「0644」等值或您想要的其他許可值。 – subosito 2010-11-04 14:58:47

+0

在ini中,我將值更改爲0666,當腳本執行時,我獲得權限:1232 – 2010-11-04 15:01:45

0

它可能不起作用,因爲當你做$excel_file_permission = (int)$ini_array['excel_file_info']['excel_file_permission'];時,你轉換爲整數值。一個整數是不同於八進制值...