跳到主要內容

CentOS 7 + PHP 7 無法上傳檔案

下以動作請使用root權限控制,確保指令可以正確運作。

查看 vi /etc/php.ini

;;;;;;;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
; upload_tmp_dir =

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2M

此設定說明,上傳檔案時,會先將檔案存放在預設/tmp 資料夾。
查看 /tmp 的權限應該為
drwxrwxrwt. 10 root root tmp
再往下檢查 /tmp下的檔案權限
ls /tmp -all
drwx------. 3 root root systemd-private-xxxxx-httpd.service-xxxxxx
將其擁有者修改為apache
chown -R apache /tmp/systemd-private-xxxxx-httpd.service-xxxxxx
再將其權限設定為755
chmod -R 755 /tmp/systemd-private-xxxxx-httpd.service-xxxxxx

--------------------------------------------------------------------------------
再來我們也需檢查PHP程式指定上傳的路徑
假設上傳路徑為 /var/www/html/upload
ls /var/www/html/upload
drwxr-xr-x. 2 myname web upload
將其擁有者修改為 apache
chown -R apache /var/www/html/upload
再將其權限設定為755
chmod -R 755 /var/www/html/upload

基本上這樣就可以讓上傳程式正常運作了。
如果,還是無法上傳檔案,那就是SELinux的限制了。
我們可以選擇關閉SELinux(以後不會遇到奇怪的問題,但安全性低)或是開放httpd上傳檔案設定。(安全性高)

關閉SELinux 

開啟檔案 vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#         enforcing - SELinux security policy is enforced.
#         permissive - SELinux prints warnings instead of enforcing.
#         disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
#         targeted - Targeted processes are protected,
#         minimum - Modification of targeted policy. Only selected processes are protected.
#         mls - Multi Level Security protwction.
SELINUXTYPE=targeted

將SELINUX=enforcing 改為SELINUX=disabled
儲存後重新開機。

開放httpd上傳檔案設定

chcon -t httpd_sys_rw_content_t /var/www/mysite1/upload -R
因為預設的 webservice 能擁有的權限角色為 httpd_sys_content_t , 沒有寫入檔案的權限。

留言

這個網誌中的熱門文章

安裝webmin

Webmin是一套透過網頁的界面來維護linux主機各項伺服器的程式,非常的好用方便。 其安裝方式如下: 1. 在yum.repos.d資料夾下新增一個設定檔。     vi /etc/yum.repos.d/webmin.repo     [Webmin]       name=Webmin Distribution Neutral       baseurl=http://download.webmin.com/download/yum       enabled=1 2. 匯入GPGkey     rpm --import http://www.webmin.com/jcameron-key.asc     如果系統顯示匯入失敗。可改下列做法     wget http://www.webmin.com/jcameron-key.asc     rpm --import jcameron-key.asc 3. 執行安裝     yum install -y webmin 4. 設定使用者及密碼     /usr/libexec/webmin/changepass.pl /etc/webmin 帳號 密碼

CSS 文字效果

text-decoration 於 CSS 中定義為設定 文字效果 屬性有: 1. none ─ 預設值 2. overline ─ 文字標上線 3. underline ─ 文字標底線 4. line-throughx ─ 文字標刪除線 5. blink ─ 文字閃爍

安裝CKEditor 4.11

下載路徑 https://ckeditor.com/ckeditor-4/download/ 選擇版本 安裝到網站 將下載的資料夾移至網站目錄。 下載路徑 網站目錄 html檔案內容 <script src="CKEditor路徑/ckeditor.js"></script> <form><code class="prettyprint">內容:<textarea id="ckedit"></textarea></code></form> <script>CKEDITOR.replace('ckedit');</script> 呈現結果 設定toolbar 見到如此煩雜的工具列,看了就是不舒服。 有些工具其實也不必用到,只要在config.js檔設定一些內容,就可以有清爽的工具列了。 config.toolbar = 'Full'; config.toolbar_Full = [ { name: 'document', items : [ 'Source'] }, { name: 'basicstyles', items : [ 'Bold','Italic','Underline','Subscript','Superscript' ] }, { name: 'paragraph', items : [ 'NumberedList','BulletedList','CreateDiv' ] }, { name: 'links', items : [ 'Link','Unlink' ] }, { name: 'insert', items : [ 'Image','Table...