什麼是PEAR?
PEAR是PHP擴充與應用庫(the PHP Extension and Application Repository)的縮寫。它是一個PHP擴充及應用的一個代碼倉庫,簡單地說,PEAR之於PHP就像是CPAN(Comprehensive Perl Archive Network)之於Perl。
由此可見PEAR是PHP代碼的倉庫,在這裡可以找到很多有用的代碼,避免我們重複寫一些功能,但是PEAR程式碼封裝並不是直接通過HTTP網站下載的,我們需要安裝PEAR。
如何擷取並安裝PEAR
建議:下面操作在Windows 7環境下完成,同樣適用於Windows XP/2003/Vista/2008系統,部分操作帶有嘗試性,為了避免彎路,建議先看完本文,然後再進行相關操作。
如果PHP目錄下沒有內建PEAR,即找不到go-pear.php檔案,那麼就需要我們手動下載go-pear.php檔案,點擊這裡下載並另存新檔go-pear.php,這個檔案放到php.exe所在的PHP安裝目錄下,我這裡安裝目錄是D:\php5(下文以此目錄為準),那麼go-pear.php的位置應該是D:\php5\go-pear.php。
開始 – 運行,輸入cmd,啟動命令提示字元,輸入以下命令,主要是切換到go-pear.php目錄下,然後執行這個PHP指令檔:
cd D:\php5
D:
php go-pear.php
遺憾的是可能由於我的PHP版本太新,上述操作提示失敗,具體的資訊如下:
Deprecated: Assigning the return value of new by reference is deprecated in D:\php5\go-pear.php on line 733
Sorry! Your PHP version is too new (5.3.8) for this go-pear.
Instead use http://pear.php.net/go-pear.phar for a more stable and current version of go-pear, more suited to your PHP version.
Thank you for your coopertion and sorry for the inconvenience!
意思是要求我們到http://pear.php.net/go-pear.phar下載go-pear.phar檔案,然後取代go-pear.php再試。下載好go-pear.phar檔案並放到D:\php5檔案夾中,執行下面的命令:
cd D:\php5
D:
php go-pear.phar
這時出現:
Are you installing a system-wide PEAR or a local copy?
(system|local) [system] :
按斷行符號預設system然後繼續。以下是預設的pear的臨時、資料、配置、測試、執行目錄的設定:
Below is a suggested file layout for your new PEAR installation. Tochange individual locations, type the number in front of thedirectory. Type 'all' to change all of them or simply press Enter toaccept these locations. 1. Installation base ($prefix) : D:\php5 2. Temporary directory for processing : D:\php5\tmp 3. Temporary directory for downloads : D:\php5\tmp 4. Binaries directory : D:\php5 5. PHP code directory ($php_dir) : D:\php5\pear 6. Documentation directory : D:\php5\docs 7. Data directory : D:\php5\data 8. User-modifiable configuration files directory : D:\php5\cfg 9. Public Web Files directory : D:\php5\www10. Tests directory : D:\php5\tests11. Name of configuration file : C:\Windows\pear.ini12. Path to CLI php.exe : D:\php5 1-12, 'all' or Enter to continue:
直接斷行符號使用預設值然後繼續安裝。本來以為萬事大吉的,可是偏偏在這裡出現了錯誤:
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘UTC' for '8.0/no DST' instead in phar://D:/php5/go-pear.phar/PEAR/Registry.php on line 1012
PEAR_Config::writeConfigFile fopen(‘C:\Windows\pear.ini','w') failed (fopen(C:\Windows\pear.ini): failed to open stream: Permission denied)
看樣子是沒有許可權寫入C:\Windows\pear.ini,由於我使用的是Windows 7系統,然後普通使用者開啟的命令提示字元,在這裡當然沒有許可權寫入C:\Windows目錄了,看來要另闢蹊徑了。回到剛才Are you installing a system-wide PEAR or a local copy?這段,為什麼不選擇local copy呢?網上都是直接說預設斷行符號選擇system的,不想改寫C:\Windows目錄,於是我重新執行php go-pear.phar,並且選擇local。
Are you installing a system-wide PEAR or a local copy?
(system|local) [system] : local
Please confirm local copy by typing 'yes' : yes
好了,這下正常了:-)
Would you like to alter php.ini <D:\php5\php.ini>? [Y/n] :
然後就是上面這句,只有選擇Y同意修改php.ini。
php.ini <D:\php5\php.ini> include_path updated. Current include path : .;C:\php\pearConfigured directory : D:\php5\pearCurrently used php.ini (guess) : D:\php5\php.iniPress Enter to continue:
接著出現上面這段,看來沒有什麼好改的,直接斷行符號確認即可。
到這裡應該能看到Thanks for using go-pear!這句了,那麼恭喜你,PEAR總算是安裝成功了!
如何安裝PEAR Package包
接下來讓我們去PEAR網站上看看有什麼需要的吧。PEAR每個包都有安裝命令的提示,比如說MDB2這個包,上面已經提示使用pear install MDB2命令安裝了,假設目錄還是D:\php5,那麼我們利用下面的命令試試:
cd D:\php5
D:
pear install MDB2
結果出現下面這個錯誤:
D:\php5>pear install MDB2downloading MDB2-2.4.1.tgz ...Starting to download MDB2-2.4.1.tgz (119,790 bytes)..........................done: 119,790 bytesERROR: failed to mkdir C:\php\pear\docs\MDB2\docs\examples
無法建立目錄,看來又是許可權問題,索性以管理員身份啟動cmd命令提示字元,然後重新執行上面的命令。這次總算安裝成功了install ok!
另一個安裝報錯案例:PHP pear安裝出現 Warning: require_once(Structures/Graph.php)...錯誤
在WINDOWS安裝pear,一路無阻很順利安裝完成,接著想安裝下pear email包來玩下,但接下來卻報:
Warning: require_once(Structures/Graph.php): failed to open stream: No such file
or directory in PEAR\Downloader.php on line 1217
PHP Warning: require_once(Structures/Graph.php): failed to open stream: No such
file or directory in E:\Program Files\php5_3\pear\pear\PEAR\Downloader.php on l
ine 1217
新手沒辦法,網上找解決。折騰了老半天,還是報這錯誤.原因很清楚就是少了Structures_Graph這個PEAR應用,相直接通用pear命令:
pear install Structures_Graph-1.1.1
線上安裝這個應用,但讓人痛心的是竟然也報同樣的錯誤,我丟。。
不跟它玩了直接到PEAR官網下載Structures_Graph-1.1.0.tgz包。解壓看看裡面的結構;發現裡面正好有一個Structures/Graph.php;於是解壓並把壓縮包裡的Structures檔案夾放到pear根目下面。接著再運行Structures_Graph的安裝命令,驚喜!!成功了。
接著再安裝email,不出意外也成功。到此這個糾結了我半天的問題終於解決了。
謝謝大家的閱讀哦!