第 1 步:確定 PHP 用戶
創(chuàng)建一個(gè)包含以下內(nèi)容的 PHP 文件:
<?php echo `whoami`; ?>
將其上傳到您的網(wǎng)絡(luò)服務(wù)器。輸出應(yīng)類似于以下內(nèi)容:
www-data
因此,PHP 用戶是 www-data
。
第 2 步:確定目錄所有者
接下來,通過命令行檢查Web目錄的詳細(xì)信息:
ls -dl /var/www/example.com/public_html/example-folder
結(jié)果應(yīng)類似于以下內(nèi)容:
drwxrwxr-x 2 exampleuser1 exampleuser2 4096 Mar 29 16:34 example-folder
因此,該目錄的所有者是 exampleuser1
。
第 3 步:將目錄所有者更改為 PHP 用戶
然后,將Web目錄的所有者更改為PHP用戶:
sudo chown -R www-data /var/www/example.com/public_html/example-folder
驗(yàn)證 Web 目錄的所有者是否已更改:
ls -dl /var/www/example.com/public_html/example-folder
結(jié)果應(yīng)類似于以下內(nèi)容:
drwxrwxr-x 2 www-data exampleuser2 4096 Mar 29 16:34 example-folder
至此,example-folder
的所有者已成功更改為 PHP 用戶:www-data
。
完成! PHP 現(xiàn)在應(yīng)該能夠?qū)懭朐撃夸洝?/strong>
一個(gè)簡單的方法是讓 PHP 首先創(chuàng)建目錄本身。
<?php $dir = 'myDir'; // create new directory with 744 permissions if it does not exist yet // owner will be the user/group the PHP script is run under if ( !file_exists($dir) ) { mkdir ($dir, 0744); } file_put_contents ($dir.'/test.txt', 'Hello File');
這可以為您省去權(quán)限方面的麻煩。