linux檔案上傳,給檔案或目錄添加apache許可權

來源:互聯網
上載者:User

標籤:127.0.0.1   網上   css   目錄   ext   led   leo   cal   執行   

系統內容:ubuntu11.10/apache2/php5.3.6

在LAMP環境中,測試一個簡單的php檔案上傳功能時,發現/var/log/apache2/error.log中出現如下php警告:

  1. [Tue Jan 31 09:40:27 2012] [error] [client 127.0.0.1] PHP Warning: move_uploaded_file(/home/leotody/32883679.jpeg): failed to open stream: Permission denied in /var/www/upload_file.php on line 25, referer: http://localhost/info.html
  2. [Tue Jan 31 09:40:27 2012] [error] [client 127.0.0.1] PHP Warning: move_uploaded_file(): Unable to move ‘/tmp/phps05ssU‘ to ‘/home/leotody/32883679.jpeg‘ in /var/www/upload_file.php on line 25, referer: http://localhost/info.html

提示沒有存取權限,檔案上傳的代碼如下:

  1.     if (file_exists("upload/" . $_FILES["file"]["name"]))
  2.       {
  3.       echo $_FILES["file"]["name"] . " already exists. ";
  4.       }
  5.     else
  6.       {
  7.                 if(move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$_FILES["file"]["name"]))
  8.                 {
  9.                         echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  10.                 }
  11.                 else
  12.                 {
  13.                         echo "move error!";
  14.                 }
  15.       }

頁面中顯示"move error!",說明move_uploaded_file()函數執行失敗。
通過尋找資料,發現網上有說是selinux啟動的原因,檢查系統並未啟動selinux,查看upload目錄預設許可權ls -ld upload,結果為755:

  1. drwxr-xr-x 2 root root 4096 2012-01-31 10:59 upload

將upload目錄許可權改為777,sudo chmod 777 upload,再測試上傳功能成功。但這種修改許可權的方法並不安全。
故可以改upload目錄的擁有者為www-data(即apache),sudo chown -R www-data:www-data upload,然後再測試上傳功能成功。

    1. drwxr-xr-x 2 www-data www-data 4096 2012-01-31 10:59 upload

linux檔案上傳,給檔案或目錄添加apache許可權

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.