顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

2011年12月25日

PHP建立ZIP檔時出現偏移而無法解壓縮

最近遇到個問題是php壓縮zip檔後,再利用readfile函式供使用者下載,但下載後的zip檔利用windows內建程式解壓縮時,都會出現檔案損毀訊息,用7zip的資訊功能反應出檔案有偏移的狀況,經過我觀察比較後,若壓縮後的檔案不要用readfile下載都沒問題,且兩種下載方式,檔案大小差了1個位元組。

查看PHP手冊,readfile函式對於採用gzip壓縮模式傳輸時可能會造成問題,建議採用以下方式解決:

1. 在程式加上:
@apache_setenv('no-gzip', 1);
@
ini_set('zlib.output_compression', 0);


2. 在php.ini設定:
zlib.output_compression = 0

參考文獻
PHP readfile
PHP zlib.configuration

2011年3月22日

phpMyAdmin 撈MySQL中文亂碼

由於MySQL預設語系為latin1,此時用phpMyAmin撈取中文資料時會變成亂碼

先確認MySQL的語系
mysql> show variables like 'char%';

+--------------------------+----------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/mysql/charsets/ |
+--------------------------+----------------------------------------+


檢查Table的語系
mysql> use db;
mysql> show full columns from table\G;

*************************** 1. row ***************************
Field: p_custom1
Type: varchar(32)
Collation: latin1_swedish_ci
Null: YES
Key:
Default: NULL
Extra:
Privileges: select,insert,update,references
Comment:
1 rows in set (0.00 sec)


解法
shell># vim /etc/my.cnf
[client]
default-character-set=utf8

[mysqld]
default-character-set=utf8
init_connect='SET NAMES utf8'

記得重新啟動MySQL

2010年11月24日

CentOS 安裝 PHP PEAR

若不知道PEAR是什麼的,可以參考 What is PEAR,PEAR目前也可以用來安裝PECL套件

YUM已經有PEAR套件了
shell># yum install php-pear

下載完成後,更新channel資料
shell># pear channel-update pear.php.net
Retrieving channel.xml from remote server
Channel pear.php.net channel.xml is up to date


安裝pear套件
shell># pear install Net_Ping
downloading Net_Ping-2.4.5.tgz ...
Starting to download Net_Ping-2.4.5.tgz (9,600 bytes)
.....done: 9,600 bytes
install ok: channel://pear.php.net/Net_Ping-2.4.5

列出目前系統安裝的pear套件庫
shell># pear list

Installed packages, channel pear.php.net:
=========================================
Package Version State
Archive_Tar 1.3.1 stable
Console_Getopt 1.2 stable
Net_Ping 2.4.5 stable
PEAR 1.4.9 stable
XML_RPC 1.5.0 stable


若只想下載套件的話
shell># pear download Net_Ping

到PEAR PACKAGES尋找你要的套件
http://pear.php.net/manual/en/packages.php

參考文獻
http://pear.php.net

2010年11月17日

php fgets 老是出現 Undefined offset 的錯誤訊息

最近在碰PHP程式,讓我開始懷念Perl在處理文字強大的威力,不管如何好不容易搞定fgets()的錯誤訊息,趕快紀錄下來,免得以後又吃到苦頭

我原本的程式碼如下
$fh = fopen('/proc/meminfo', 'r');
if($fh) {
$i=0;
while( !feof($fh) ) {
$buffer = fgets($fh);
list($key[$i], $value[$i]) = explode(':', $buffer);
$i++;
}
}
print_r($value);


執行之後PHP會一直出現 PHP Notice: Undefined offset: 1 錯誤訊息,問Google大神後得知可能是因為EOF的關係會多一行空白,導致陣列出現錯誤索引,我試著用preg_replace也無法將這空白拿掉,最後用strpos函式先確認每一行是否有我要的字串,才將此問題解決。程式改寫如下

$fh = fopen('/proc/meminfo', 'r');
if($fh) {
$i=0;
while( !feof($fh) ) {
$buffer = fgets($fh);
if( strpos($buffer, ':') ) {
$buffer = preg_replace( array('/\s+/', '/\n/', '/(kB)$/'), '', $buffer);
list($key[$i], $value[$i]) = explode(':', $buffer);
$i++;
}
}
fclose($fh);
}
print_r($value);

2010年9月22日

php內建支援的protocols / wrappers

參閱php官方手冊以取得詳細說明
http://www.php.net/manual/en/wrappers.php

包含以下功能

* Filesystem
* HTTP and HTTPS
* FTP and FTPS
* PHP input/output streams
* Compression Streams
* Data (RFC 2397)
* Glob
* Phar (phar://)
* Secure Shell 2 (This wrapper is not enabled by default)
* Rar (rar://)
* Audio Streams (ogg://)
* Process Interaction Streams (expect://command)

2010年4月24日

Windows安裝PHP筆記(使用IIS)

這次有個案子要跟別的系統共用伺服器,而伺服器執行Windows 2003,網頁伺服器是用IIS 6,趁這次機會在自己的LAB機上先安裝一下,整個的安裝方式是參考PHP官方的手冊進行,若有需要可以到這裡取得PHP的線上手冊。

1.下載PHP套件

由於我要採用IIS作為我的網站伺服器,手冊建議我們使用Non-thread-safe的版本,請到這裡下載PHP 5.2.x,我將使用zip包進行安裝。

2.解壓縮及安裝PHP

將下載的PHP解壓縮到 "c:\php",在php目錄裡面有個檔案名稱為 php.ini-dist,請將它複製到 "c:windows",並改名為 php.ini。

我們來確認一下 php 程式是否真的可以找到 php.ini檔,請開啟「命令提示字元」並輸入指令:

C:\php>php -i|find "php.ini"
Configuration File (php.ini) Path => C:\WINDOWS
Loaded Configuration File => C:\WINDOWS\php.ini <-- 看到這行表示php以確實找到囉


請編輯C:\WINDOWS\php.ini的內容,確保指令與下面列出的相同
extension_dir = "c:\php\ext"

若你還有需要其他的模組,也請將模組前方的分號拿掉
extension=php_gd2.dll
extension=php_mbstring.dll
extension=php_mcrypt.dll
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_openssl.dll
extension=php_zip.dll

fastcgi.impersonate = 1
fastcgi.logging = 0
cgi.fix_pathinfo=1
cgi.force_redirect = 0


3.安裝FastCGI模組

再來安裝IIS的FastCGI模組以提高執行效率及穩定性,請到這裡下載FastCGI並著手進行安裝。最後在「命令提示字元」輸入如下指令設定IIS使用FastCGI處理.php的檔案:
C:\>cscript %windir%\system32\inetsrv\fcgiconfig.js -add -section:"PHP" ^
-extension:php -path:"C:\PHP\php-cgi.exe"


設定一個php instance 最多可以處理多少個連線請求,以回收系統資源。
cscript %windir%\system32\inetsrv\fcgiconfig.js -set -section:"PHP" ^
-InstanceMaxRequests:10000

cscript %windir%\system32\inetsrv\fcgiconfig.js -set -section:"PHP" ^
-EnvironmentVars:PHP_FCGI_MAX_REQUESTS:10000


設定c:\php目錄的權限,增加IIS_WGP有讀取及執行的權限,如下圖(沒加的話會出現FastCGI error)


4.設定IIS

將 index.php 設定為 default document


5. 測試功能

我們可以利用 phpinfo() 函式來確認 php 與 FastCGI 功能是否正確執行。若 phpinfo 頁面的 Server API 是 CGI/FastCGI 就表示 PHP 的安裝沒有問題囉。



參考文獻
˙Using FastCGI to Host PHP Applications on IIS 6.0
˙PHP Manual > Microsoft IIS 5.1 and IIS 6.0
˙PHP Manual > Manual Installation Steps

php pecl

用過Perl的人都會知道CPAN,原來PHP也有類似CPAN的東西,叫做PECL,使用PECL的方法:

shell#> pecl install module-name

2008年5月9日

安裝PHP PECL 延伸模組

這次在安裝horde時需要用到memcache與fileinfo這兩個extension,順便做個筆記

安裝前的準備

一個能運作的PHP

編譯PECL模組

1.首先到pecl.php.net搜尋模組的source並下載到主機上,以memcache為例

shell> wget http://pecl.php.net/get/memcache-3.0.1.tgz

2.解壓縮

shell> tar xzvf memcache-3.0.1.tgz

3.編譯

shell> cd memcache-3.0.1
#產生configure和makefile
shell> phpize
shell> ./configure
shell> make
#下達make install後,會告知你extension被安裝在哪
#以我的例子是在/opt/php/lib/php/extensions/no-debug-zts-20060613
shell> make install


4.編輯php.ini

shell> vim /opt/php/lib/php.ini
修改 extension_dir = /opt/php/lib/php/extensions/no-debug-zts-20060613
增加 extension=memcache.so

5.重新啟動apache

shell> apachectl restart