簡體   English   中英

警告:mkdir():權限被拒絕

[英]Warning: mkdir(): Permission denied

創建新帳戶時,我試圖建立目錄。
該目錄應位於我的圖像文件夾中,用於更好地分離上載的圖像。

//get the ID of the new account that was just created/inserted
$accountID = mysqli_insert_id($dbc);

//create a new directory path for that account
$directoryPath =  "../images/" . $accountID;

// check if the directory exists
if (!is_dir($directoryPath)) {
    //create the directory
    mkdir($directoryPath, 0777);         //breaking here
}      

幾天前我可以正常使用它,但是今天進行測試時遇到了問題。

我已經包含了ini_set('display_errors', 'On'); 在我的頁面上查看我拋出了什么錯誤,這是一個權限錯誤。

Warning: mkdir(): Permission denied

images文件夾對所有用戶和組以及任何父文件夾都具有完全的讀/寫權限,因此我不知道這將是一個問題,並且它已經工作了好幾次。

如果重要的話,我正在Windows上工作。

有任何想法嗎?

為了避免在CLI用戶和Apache用戶之間的權限問題上花費過多時間,一種簡單的配置是將相同的用戶用於兩個進程。

通過執行操作獲取用戶ID和組

$ id
uid=1000(my_user), gid=1000(my_group), ...

接着:

$ sudo service apache2 stop
$ sudo vi /etc/apache2/envvars
export APACHE_RUN_USER=my_user
export APACHE_RUN_GROUP=my_group
$ sudo chown -R my_user /var/lock/apache2

比將您的整個目錄權限更改為777更好,更安全

您必須確保父目錄允許您創建文件夾,而不是創建自己的文件夾(使用0777權限創建的文件夾)...
另外,檢查與哪個用戶一起啟動Apache服務器
使用mkdir()之前檢查目錄是否已經存在

if (!is_dir ($directoryPath) ) {
    mkdir($directoryPath, 0777);
}

我認為您應該嘗試-

mkdir("../images/".$accountID, 0777, 'R');

遞歸文件夾創建可能會導致此問題。 還可以從-mkdir獲取更多信息

同時檢查文件夾權限。

如果重要的話,我正在Windows上工作。

是的

嘗試改變這個

if (!is_dir($directoryPath)) {
    //create the directory
    mkdir($directoryPath, 0777);         //breaking here
}   

對此

if (!is_dir($directoryPath)) {
    //create the directory
    mkdir($directoryPath);         //breaking here
}   

您在Windows機器上,因此它將忽略chmod模式。 也請嘗試使用完整路徑而不是相對路徑。

默認情況下,模式為0777,這意味着可能的訪問范圍最廣。 有關模式的更多信息,請閱讀chmod()頁面上的詳細信息。

注意:在Windows上,mode被忽略

http://php.net/manual/en/function.mkdir.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM