本文目錄一覽:
php檢測上傳文件大小的問題
在用PHP進行文件上傳的操作中,需要知道怎麼控制上傳文件大小的設置,而文件可傳大小是受到多種因素制約的,現總結如下:
1、php.ini:upload_max_filesize 所上傳的文件的最大大小。默認值2M。
2、php.ini:memory_limit 本指令設定了一個腳本所能夠申請到的最大內存位元組數,默認值8M。如果不需要任何內存上的限制,必須將其設為 -1。如果內存不夠,則可能出現錯誤:Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y bytes)
3、php.ini:post_max_size 設定POST數據所允許的最大大小。此設定也影響到文件上傳。要上傳大文件,該值必須大於 upload_max_filesize。
4、php.ini:max_execution_time = 30 ; Maximum execution time of each script, in seconds
5、php.ini:max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
6、如果用到mysql的BLOB進行二進制文件存儲,則需要設置my.ini:max_allowed_packet=xxM
7、httpd.conf
在 Apache 裏面有一個選項是 LimitRequestBody,這個選項可以限制用戶送出的 HTTP 請求內容。這個選項可以在 .htaccess 或 httpd.conf 里使用,而如果在 httpd.conf 內使用,分別可以用在 virtualhost 或目錄屬性設定。而 LimitRequestBody 的設定值是介乎 0 (無限制) 至 2147483647 (2GB)。
例如要在目錄 D:/AppServ/www 設定上傳限制為 100K,可以在 .htaccess 或 httpd.conf 加入以下語句:
LimitRequestBody 1024000000
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
LimitRequestBody 1024000000Options Indexes FollowSymLinks MultiViews ExecCGIAllowOverride AllOrder allow,denyAllow from all
如果透過 .htaccess 設定,儲存檔案後會立即生效;如透過 httpd.conf 設定,須要重新啟動 Apache。
PHP關於文件上傳部分,特別提到表單隱藏域:MAX_FILE_SIZE,意思是接收文件的最大尺寸。文檔中給出的例子如下:
form enctype=”multipart/form-data” action=”_URL_” method=”POST”
input type=”hidden” name=”MAX_FILE_SIZE” value=”30000″
Send this file: input name=”userfile” type=”file”
input type=”submit” value=”Send File”
form
Send this file:
這裡設置MAX_FILE_SIZE = 30000,期待一種可能,使得瀏覽器在傳送文件之前能夠依此作出預先判斷,如果文件尺寸大於30000位元組,則不執行實際的POST動作。也就是不往服務器發送文件內容,而是直接在客戶端提醒用戶「你試圖上傳的文件超過30000位元組」。
這的確是一個非常棒的主張,但在現實中卻暫時無法實現。不是因為這個限制可以「被簡單地繞過」,而是IE和FireFox這兩個主流瀏覽器都不支持這個特性。PHP的這個建議尚未被採納。
MAX_FILE_SIZE還有一個用場:後台PHP會判斷接收到的文件大小是否大於這個值,如果超出,$_FILES[‘thisfile’][‘error’]會被設置為UPLOAD_ERR_FORM_SIZE(2),同時放棄保存臨時文件,將$_FILES[‘thisfile’][‘size’]置0。
這個例子,沒問題,表現正常,當我試圖上傳一個40多K的文件時,PHP程序報告「文件超過MAX_FILE_SIZE」。
但是,如果我們將表單中的MAX_FILE_SIZE從30000減少到1000,情形又如何呢?
上傳800位元組的文件,正常;
上傳40K的文件,PHP報告文件過大,也正常;
上傳3000個位元組的文件,PHP未報告錯誤,它成功保存了文件!出乎意料!
問題就出在main/rfc1867.c中判斷文件是否超長的這部分代碼上。php每次從buffer中讀取FILLUNIT位元組長度的內容後,首先判斷「已經讀到的內容長度(total_bytes)」是否大於MAX_FILE_SIZE,然後再增加「已經讀到的內容長度(total_bytes)」。這樣一來,和預計的結果之間至多會有FILLUNIT位元組的誤差,而FILLUNIT=1024*5=5K。(點擊bug了解詳細內容)
這就是說,當MAX_FILE_SIZE5K時,上傳一個大於MAX_FILE_SIZE,但是小於5K的文件是沒有問題的。
當然,因為這個設置很容易被繞過,所以服務器端編程不應當依賴於MAX_FILE_SIZE。而且,5K到底是個很小的數值,對大多數上傳文件的表單來說沒有影響。
PHP中post_max_size,upload_max_filesize, MAX_FILE_SIZE的設置,和客戶端上傳給服務器端的流量大小無關。
Apache服務器從客戶端接收長度不超過LimitRequestBody位元組數的請求,然後傳送給php模塊,php模塊再決定是否保存成臨時文件,設置$_FILES全局變量,移交給script進一步處理。
這個Apache的LimitRequestBody選項缺省值=0,允許Request body的最大位元組數是2G(Linux + Apache)
最後還要注意的是:
html本身能夠post數據也是有限制的,不能超過2G。
FTP客戶端有文件偏移指針的2GB邊界限制,未使用特殊編譯flag編譯的ftp服務器端或者客戶端,無論在什麼FS中都不支持大於2GB的文件。不知道PHP會不會也有這種情況。
PHP運行於Apache 模塊方式
當使用 PHP 作為 Apache 模塊時,也可以用 Apache 的配置文件(例如 httpd.conf)和 .htaccess 文件中的指令來修改 PHP 的配置設定。需要有「AllowOverride Options」或「AllowOverride All」權限才可以。
有幾個 Apache 指令可以使用戶在 Apache 配置文件內部修改 PHP 的配置。哪些指令屬於 PHP_INI_ALL,PHP_INI_PERDIR 或 PHP_INI_SYSTEM 中的哪一個,請參考附錄中的 php.ini 配置選項列表。
php_value name value
設定指定的值。只能用於 PHP_INI_ALL 或 PHP_INI_PERDIR 類型的指令。要清除先前設定的值,把 value 設為 none。
Note: 不要用 php_value 設定布爾值。應該用 php_flag(見下面)。
php_flag name on|off
用來設定布爾值的配置指令。僅能用於 PHP_INI_ALL 和 PHP_INI_PERDIR 類型的指令。
php_admin_value name value
設定指定的指令的值。不能用於 .htaccess 文件。任何用 php_admin_value 設定的指令都不能被 .htaccess 或 virtualhost 中的指令覆蓋。要清除先前設定的值,把 value 設為 none。
php_admin_flag name on|off
用來設定布爾值的配置指令。不能用於 .htaccess 文件。任何用 php_admin_flag 設定的指令都不能被 .htaccess 或 virtualhost 中的指令覆蓋。
Example #1 Apache 配置例子
IfModule mod_php5.c
php_value include_path “.:/usr/local/lib/php”
php_admin_flag engine on
/IfModule
IfModule mod_php4.c
php_value include_path “.:/usr/local/lib/php”
php_admin_flag engine on
/IfModule
Caution
PHP 常量不存在於 PHP 之外。例如在 httpd.conf 中不能使用 PHP 常量如 E_ALL 或 E_NOTICE 來設定 error_reporting 指令,因為其無意義,實際等於 0。應該用相應的掩碼值來替代。這些常量可以在 php.ini 中使用。
通過 Windows 註冊表修改 PHP 配置
在 Windows 下運行 PHP 時,可以用 Windows 註冊表以目錄為單位來修改配置。配置值存放於註冊表項 HKLM\SOFTWARE\PHP\Per Directory Values 下面,子項對應於路徑名。例如對於目錄 c:\inetpub\wwwroot 的配置值會存放於 HKLM\SOFTWARE\PHP\Per Directory Values\c\inetpub\wwwroot 項下面。其中的設定對於任何位於此目錄及其任何子目錄的腳本都有效。項中的值的’名稱是 PHP 配置指令的名字,值的數據是字符串格式的指令值。值中的 PHP 常量不被解析。不過只有可修改範圍是 PHP_INI_USER 的配置值可以用此方法設定,PHP_INI_PERDIR 的值就不行。
其它接口下的 PHP
無論怎樣運行 PHP,都可以在腳本中通過 ini_set() 而在運行時修改某個值。更多信息見手冊中 ini_set() 的頁面。
如果對自己系統中的配置設定及其當前值的完整列表感興趣,可以運行 phpinfo() 函數並查看其結果的頁面。也可以在運行時用 ini_get() 或 get_cfg_var() 取得個別配置指令的值。
add a note add a note
User Contributed Notes 13 notes
up
down
3 Anteaus ?2 years ago
One of the most serious problems here is that it is hard to determine programmatically which of three or more possible configuration methods (php.ini, .user.ini, .htaccess) should be used on any given hosting company’s server.
The worst outcome is if an install.php routine attempts to set .htaccess directives on a cgi-mode server, in which case the outcome is usually a ‘500’ crash. Once in that situation the php installation routine cannot be rerun to correct the problem, so you effectively have a hosting lockout situation which can only be corrected by manual intervention.
up
down
-2 contrees.du.reve at gmail dot com ?9 years ago
Being able to put php directives in httpd.conf and have them work on a per-directory or per-vitual host basis is just great. Now there’s another aspect which might be worth being aware of:
A php.ini directive put into your apache conf file applies to php when it runs as an apache module (i.e. in a web page), but NOT when it runs as CLI (command-line interface).
Such feature that might be unwanted by an unhappy few, but I guess most will find it useful. As far as I’m concerned, I’m really happy that I can use open_basedir in my httpd.conf file, and it restricts the access of web users and sub-admins of my domain, but it does NOT restrict my own command-line php scripts…
up
down
-3 nick at vistaworks dot net ?4 years ago
On Windows, as the documentation above states, you cannot set max_upload_filesize and post_max_size in the registry, because they are PHP_INI_PERDIR.
Confusingly, however, is the fact that phpinfo() will show your changes if you do add those to the regsitry, as if they were taking effect. However, the upload size is *not* changed, regardless of what phpinfo() reports.
I believe the reading of registry values and override happen ‘too late’ in the request processing, ie, after the file has already been uploaded and rejected.
In a perfect world, phpinfo() would recognize this and not report the overridden values, which is very confusing.
up
down
-4 Woody/mC ?9 years ago
@ pgl: As the documentation says:
“To clear a previously set value use none as the value.”
Works fine for me.
up
down
-6 user at NOSPAM dot example dot com ?5 years ago
PHP Constants will work with php_value; for example:
php程序怎麼部署運行
首先你要有windows2003和iis6.0(6.0以下就免談了)。
然後要安裝的東東依次為:php5.2,mysql5.0,phpmyadmin2.11
這些東西一個個去找實在是太麻煩了,幸好我們有wamp。
只要學會了安裝wamp,那麼就只剩下「配置」了。
下面所有牽涉到的文件路徑都以wamp為準,php在wamp中的目錄為wamp\bin\php\php5.2.5。
要讓iis能夠解析php,首先要把php.ini複製到c:\windows目錄下,把php5isapi.dll複製到c:\windows\system32下。
然後從windows2003的管理工具中進入iis,新添加一個web服務擴展,擴展名可以任意填寫,比如說「php服務擴展」,要求的文件則是php5isapi.dll,勾上「設置擴展狀態為允許」。
然後新建一個網站,假設已經有了一個網站,右鍵「屬性」,為網站創建一個「應用程序池」,執行權限為「腳本和可執行程序」,點擊「配置」,添加一個
「應用程序擴展」,在新彈出的窗口中,可執行文件選擇剛才的php5isapi.dll(最好是c:\windows\system32下的),」擴展
名」為.php,動作限制為「get,head,post,trace」,勾去「確認文件是否存在」
ok,這樣php就基本上配置好了,可以去網站根目錄下創建一個index.php,寫入「
?php
phpinfo();?」,然後打開瀏覽器測試一下就可以了。
首先你必須修改php.ini,找到extension_dir這個參數,假設wamp安裝在d盤,改成
extension_dir
=
「d:\wamp\bin\php\php5.2.5\ext\」,然後找到extension=php_mysql.dll和
extension=php_mysqli.dll,確保他們之前的逗號已經去掉。
接下就是關鍵了,在wamp\bin\php\php5.2.5目錄下有一個關鍵的文件libmysql.dll,昨天困擾了許久,才想起以前配置
mysql的慘痛經歷(都是wamp惹的禍,把php/mysql的安裝搞的太簡單了),這個文件一定要複製到c:\windows\system32目
錄下去,否則就算在php.ini裏面打開extension=php_mysql.dll也沒有用,當瀏覽需要連接mysql的頁面時,會報錯說
「mysql」類型不存在。
我們就遇到了這樣一個實際問題:54上已經在運行asp以及asp.net的網站,iis搶佔了80端口,apache就不能使用了,這樣難道你要別人在
單,在wamp中找到httpd.conf文件,將其中所有「80」字樣替換成你想要的端口,重啟apache就可以了。apache裏面一共有4個地方
需要修改80:
listen
80,servername
localhost:80,namevirtualhost
*:80,前2個是默認的,後2個是配置了virtualhost才會出現。/pre
評論
加載更多
php網站搭建端口問題
打開iis管理器,設置對應網站的訪問端口既可。這裡端口不僅限於80,只要沒被佔用都可以設置,但是默認訪問的端口是80
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/190747.html