首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 系統級函數速查手冊:權限、進程、環境與配置

     編輯:PHP 相關知識

用戶與權限

函數說明示例
get_current_user()獲取腳本擁有者用戶名echo get_current_user(); // www-data
getmyuid()獲取腳本擁有者 UIDecho getmyuid(); // 33
posix_getpwuid($uid)根據 UID 獲取用戶信息print_r(posix_getpwuid(1000));
posix_getgrgid($gid)根據 GID 獲取組信息print_r(posix_getgrgid(1000));
fileowner($file)獲取文件擁有者 UIDecho fileowner('test.txt');
fileperms($file)獲取文件權限(八進制)echo substr(sprintf('%o', fileperms('test.txt')), -4); // 0644
chmod($file, 0755)修改文件權限chmod('script.sh', 0755);

目錄與文件系統

函數說明
getcwd()獲取當前工作目錄
scandir($dir)列出目錄內容(含 . 和 ..)
glob($pattern)模式匹配文件(更強大)
realpath($path)獲取絕對路徑(解析鏈接)
disk_free_space($dir)目錄可用空間(字節)
disk_total_space($dir)目錄總空間(字節)

系統環境信息

函數說明
php_uname($mode = 'a')系統信息:a=全部, s=系統名, r=版本, m=硬件
php_ini_loaded_file()加載的 php.ini 路徑
php_ini_scanned_files()掃描的額外 .ini 文件
ini_get_all($extension)所有配置選項(含當前值)
get_cfg_var($option)獲取 php.ini 原始值(非運行時修改)

進程與執行

函數說明
exec($cmd, &$output, &$return_var)執行命令,獲取輸出數組
shell_exec($cmd)執行命令,返回完整輸出字符串
system($cmd, &$return_var)執行命令,直接輸出並返回最後一行
passthru($cmd, &$return_var)執行命令,二進制輸出(圖片/PDF)
proc_open()高級進程控制(雙向管道)
pcntl_fork()創建子進程(CLI 專用)
posix_getpid()獲取當前進程 ID
posix_kill($pid, $signal)發送信號給進程

實用組合示例

獲取當前運行用戶完整信息

$uid = posix_getpwuid(posix_geteuid());
$gid = posix_getgrgid(posix_getegid());
echo "用戶: {$uid['name']} (UID: {$uid['uid']})";
echo "組: {$gid['name']} (GID: {$gid['gid']})";

安全的命令執行

function safeExec($cmd, array $args = []) {
    $escaped = array_map('escapeshellarg', $args);
    $fullCmd = $cmd . ' ' . implode(' ', $escaped);
    $output = [];
    $returnVar = 0;
    exec($fullCmd, $output, $returnVar);
    return [$returnVar, $output];
}

// 使用
[$code, $out] = safeExec('ls', ['-la', '/var/www']);
if ($code === 0) {
    print_r($out);
}

檢查擴展是否加載

if (!extension_loaded('redis')) {
    die('Redis 擴展未安裝');
}
echo 'Redis 版本: ' . phpversion('redis');

注意事項

  • 系統函數多依賴 POSIX 擴展(Windows 部分不可用)
  • 命令執行**極高風險**,嚴禁用戶輸入直接拼接,必須用 escapeshellarg/escapeshellcmd
  • 權限操作需腳本擁有者具備相應權限
  • 共享主機常禁用 exec、shell_exec 等函數
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved