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

2009年2月7日 星期六

發送文件類型及編碼 Header 在IE中變檔案下載

今天被這個Header發送搞了一個小時多
從伺服器發給Browser 一個文件類型及編碼的Header
指令為 header('Content-Type: text/html ; charset=utf-8');
Firefox及Chrome都可以正確運行
一遇上IE就變成檔案下載...
最後才發現在 text/html 之後多了一個空格
將空格移除後,就正常了 ^^"
header('Content-Type: text/html; charset=utf-8');

順道補上其他語言的文件類型及編碼Header設定
Java
resource.setContentType ("text/html; charset=utf-8");
JSP
<%@ page contenttype="text/html; charset=utf-8"%>
.Net
Response.ContentType = "text/html; charset=utf-8";
Asp
<%Response.charset="utf-8"%>

2008年8月27日 星期三

PHP 將RFC 822日期轉換成西洋曆格式

昨天前同事突然提出這個需求,需將RFC 822格式的時間再換回一般的西洋曆
一樣很簡單就能轉換,範例如下

Date('Y-m-d H:i:s ', strtotime("Sat, 16 Aug 2008 12:55:25 +0800"));

2008年3月7日 星期五

PHP中RSS的RFC 822日期格式轉換

原來在PHP中轉換日期格式成 RFC 822是這麼容易的事情
先前在程式中轉換時間格式的寫法
$strPubDate = Date('D, d M Y H:i:s O',$intDate);
剛剛在看PHP線上手冊時
突然看到Date函數下方的說明時看到了ChangeLog
之前"目小"都沒沒看到ChangeLog
5.1.1 There are useful constants of standard date/time formats that can be used to specify the format parameter.
原來PHP開發團隊早在PHP 5.1.1版時就已經預先加入一些常用的日期格式常數
方便開發者進行日期格式轉換
http://tw.php.net/manual/en/ref.datetime.php#datetime.constants
節錄其中的一些說明
DATE_ATOM (string)
Atom (example: 2005-08-15T15:52:01+00:00)
DATE_COOKIE (string)
HTTP Cookies (example: Monday, 15-Aug-05 15:52:01 UTC)
DATE_ISO8601 (string)
ISO-8601 (example: 2005-08-15T15:52:01+0000)
DATE_RFC822 (string)
RFC 822 (example: Mon, 15 Aug 05 15:52:01 +0000)
DATE_RFC850 (string)
RFC 850 (example: Monday, 15-Aug-05 15:52:01 UTC)
DATE_RFC1036 (string)
RFC 1036 (example: Mon, 15 Aug 05 15:52:01 +0000)
DATE_RFC1123 (string)
RFC 1123 (example: Mon, 15 Aug 2005 15:52:01 +0000)
DATE_RFC2822 (string)
RFC 2822 (Mon, 15 Aug 2005 15:52:01 +0000)
DATE_RFC3339 (string)
Same as DATE_ATOM (since PHP 5.1.3)
DATE_RSS (string)
RSS (Mon, 15 Aug 2005 15:52:01 +0000)
DATE_W3C (string)
World Wide Web Consortium (example: 2005-08-15T15:52:01+00:00)

所以現在作日期格式轉換時可以改寫的成
$strPubDate = Date(DATE_RSS,$intDate);
$strPubDate = Date(DATE_RFC2822,$intDate);
方便又有效率
完整的RSS格式可參考下面網址
http://cyber.law.harvard.edu/rss/rss.html

2008年2月26日 星期二

PHP 上傳中文檔案 BIG5

看到BIG5就應該知道會遇上許功蓋
目前手上進行的專案需要在BIG5的環境中上傳中文命名的檔案
並且將原始的中文檔名存入資料庫
這個問題最好的解決方式仍是將系統轉為UTF-8
但不適合現在的專案,再來想到的處理方式是
把該相關的處理程式轉成UTF-8,要存進資料庫時再將UTF-8轉成BIG5
不過,這只適合處理程式邏輯不多的狀況,
現行的相關處理程式邏輯很多
接著又想到一個搭配Javascript的處理方式
先在表單中增加一個Hidden欄位存放上傳的中文檔名
將Submit時,取出使用者上傳的檔名經由escape編碼後寫到Hidden欄位中
語法如下
//Javascript For Windows

function setFilesName(s, ss) { < br / >
    //s = 上傳欄位id, ss = hidden欄位id

    var aryFiles = document.getElementById(s).value.split('\\'); < br / >
    var strFileFullName = aryFiles[aryFiles.length - 1]; < br / >
        document.getElementById(ss).value = escape(strFileFullName.substr(0, strFileFullName.lastIndexOf('.'))); < br / >
}
然後在處理的程式使用unescape回復原始檔名
//PHP unescape
function unescape($arg_strEscapeContent, $arg_strCharset = 'big5') {
    return mb_convert_encoding(preg_replace_callback("/%u?\w{2,4}/", "uni_2_utf8", $arg_strEscapeContent), $arg_strCharset, 'utf-8');
}

function uni_2_utf8($arg_aryWords) {
    $strWord = str_replace(array('%', 'u'), '', $arg_aryWords[0]);
    $intHex2Dec = intval($strWord, 16);
    if (isset($strWord[2])) {
        if ($intHex2Dec <= 0x7F) {
            return chr($intHex2Dec);
        } else if ($intHex2Dec <= 0x7FF) {
            return chr(0xC0 $intHex2Dec >> 6).chr(0x80 $intHex2Dec & 0x3F);
        } else if ($intHex2Dec <= 0xFFFF) {
            return chr(0xE0 $intHex2Dec >> 12).chr(0x80 $intHex2Dec >> 6 & 0x3F).chr(0x80 $intHex2Dec & 0x3F);
        } else if ($intHex2Dec <= 0x10FFFF) {
            return chr(0xF0 $intHex2Dec >> 18).chr(0x80 $intHex2Dec >> 12 & 0x3F).chr(0x80 $intHex2Dec >> 6 & 0x3F).chr(0x80 $intHex2Dec & 0x3F);
        }
    } else {
        return chr($intHex2Dec);
    }
}

2008年2月13日 星期三

AzDG可逆加密演算法 for PHP

AzDG For PHP網路上就找的到
但下面的版本作了一些調整
加入字元編碼轉換、演算法優化、使用PHP5語法
使用方法


$a = new AzDG();; $q = $a->Crypt('許功蓋中文測試','0123456789','big5'); echo $a->decrypt($q,'0123456789' );
class AzDG {

    private $strPrivateKey = '0123456789';
    private $strDefaultCharset = 'big5';
    private $strTargetCharset = '';
    private $strSourceText = '';
    private $intSourceLength = 0;

    function __construct() {

    }

    private function private_key_crypt($arg_strSourceText) {
        $strEncryptKey = md5($this - > strPrivateKey);
        $intCRCLength = 0;
        $strReturn = "";
        $intSourceLength = strlen($arg_strSourceText);

        for ($i = 0; $i < $intSourceLength; ++$i) {
            if ($intCRCLength > 31) {
                $intCRCLength = 0;
            }
            $strReturn. = ($arg_strSourceText[$i] ^ $strEncryptKey[$intCRCLength++]);
        }

        return $strReturn;
    }

    public function crypt($arg_strSourceText, $arg_strPrivateKey = null, $arg_strCharset = null) {
        $this - > strSourceText = $arg_strSourceText;
        $this - > intSourceLength = strlen($this - > strSourceText);

        if (!is_null($arg_strPrivateKey)) {
            $this - > strPrivateKey = $arg_strPrivateKey;
        }

        if (!is_null($arg_strCharset) AND!empty($arg_strCharset)) {
            $this - > strTargetCharset = $arg_strCharset;
            if (!$this - > convert_encoding()) {
                die('編碼轉換失敗');
            }
        }

        $strCRCKey = md5(microtime());
        $intCRCLength = 0;
        $strTmp = "";

        for ($i = 0; $i < $this - > intSourceLength; ++$i) {
            if ($intCRCLength > 31) {
                $intCRCLength = 0;
            }
            $strTmp. = $strCRCKey[$intCRCLength].($this - > strSourceText[$i] ^ $strCRCKey[$intCRCLength++]);
        }
        return base64_encode($this - > private_key_crypt($strTmp));
    }

    public function decrypt($arg_strSourceText, $arg_strPrivateKey = null, $arg_strCharset = null) {
        if (!is_null($arg_strPrivateKey)) {
            $this - > strPrivateKey = $arg_strPrivateKey;
        }

        if (!is_null($arg_strCharset) AND!empty($arg_strCharset)) {
            $this - > strTargetCharset = $arg_strCharset;
            if (!$this - > convert_encoding()) {
                die('編碼轉換失敗');
            }
        }

        $this - > strSourceText = $this - > private_key_crypt(base64_decode($arg_strSourceText));
        $this - > intSourceLength = strlen($this - > strSourceText);
        $strReturn = '';

        for ($i = 0; $i < $this - > intSourceLength; ++$i) {
            $strReturn. = ($this - > strSourceText[$i++] ^ $this - > strSourceText[$i]);
        }
        return $strReturn;
    }

    private function convert_encoding() {
        //預設轉碼失敗
        $blnIconvPassed = false;
        if (function_exists('iconv') AND $strEncodedData = iconv($this - > strDefaultCharset, $this - > strTargetCharset.
            '//TRANSLIT', $this - > strSourceText)) {
            $blnIconvPassed = true;
            $this - > strSourceText = $strEncodedData;
        }
        //如果 iconv 轉換失敗,再嚐試用mb_convert_encoding編碼
        else if (!$blnIconvPassed AND function_exists('mb_convert_encoding') AND $strEncodedData = @mb_convert_encoding($this - > strSourceText, $this - > strTargetCharset, $this - > strDefaultCharset)) {
            $blnIconvPassed = true;
            $this - > strSourceText = $strEncodedData;
        }
        return $blnIconvPassed;
    }
}

2007年11月16日 星期五

PHP 將ImageMagick載入PHP核心

昨天弄了一個下午
將ImageMagick溶入PHP的核心之中
直接使用ImageMagick而不透過exec來執行圖片處理
順道記錄一下昨天安裝的過程備查

要將ImageMagick編PHP當然得先準備所需要的套件
1.ImageMagick
2.imagick php的擴展庫 http://pecl.php.net/package/
3.php
最低需求版本 imageMagick 6.2.4+ 、 PHP 5.1.3+.
本次使用的版本
ImageMagick-6.3.5.9
imagick-2.0.1
PHP.5.2.5
Linux-2.6.23.1-49.fc8
如果ImageMagick是以RPM或者APT方式安裝者
需要再安裝ImageMagick-devel開發套件(版本要和ImageMagick相同)

1. 首先將解開的imagick放到PHP源碼中的 ext 資料夾中(php/ext/imagick)
2. 刪除configure檔,指令 # rm configure
3. # ./buildconf --force 重新建構 configure 檔
4. 重新編譯PHP加上參數 --with-imagick=ImageMagick路徑
本例ImageMagick採用RPM安裝故參數為
--with-imagick=/usr/lib/ImageMagick-6.3.5
5. gmake;gmake install

接著重啟Apache即完成

2007年11月14日 星期三

PHP 使用ImageMagick製作縮圖

之前在PHP環境中大都使用GD庫來處理圖片
這幾天找到了另一個處理圖片的工具ImageMagick
ImageMagick可以用來讀、寫和處理超過數十種的圖片格式
要在PHP中使用當然得先安裝ImageMagick
http://www.imagemagick.org/script/index.php
到官方下載最新的版本,安裝後如何在PHP引用製作縮圖呢?
Linux用法
exec or system('/usr/bin/convert -quality 85 -geometry 245x360 來源圖片路徑 目的圖片路徑');
相比由GD庫來處理簡單多了
不過GD庫處理的速度三倍於ImageMagick
最可能的原因在於 呼叫 exec 或 system 執行系統命令所造成的延遲
題外話 convert 似乎會自動辨識圖片的尺寸
製作縮圖時如果傳入的數據有誤
ImageMagick會自動換算比例後製作縮圖
例如原始圖片 1024X1505 要製成 480X360 比例明顯有誤
不過經由ImageMagick處理後則變成 245x360 很聰明吧 XD

2007年11月10日 星期六

PHP 5.2.5 Released

剛剛PHP開放團隊放出PHP5.2.5版
本次釋放版本主要改進於PHP5.2系列的
穩定性及超過60個bug的修正其中包含數個
安全性相關的問題
主要修正列表
Fixed dl() to only accept filenames. Reported by Laurent Gaffie.
Fixed dl() to limit argument size to MAXPATHLEN (CVE-2007-4887). Reported by Laurent Gaffie.
Fixed htmlentities/htmlspecialchars not to accept partial multibyte sequences. Reported by Rasmus Lerdorf
Fixed possible triggering of buffer overflows inside glibc implementations of the fnmatch(), setlocale() and glob() functions. Reported by Laurent Gaffie.
Fixed "mail.force_extra_parameters" php.ini directive not to be modifiable in .htaccess due to the security implications. Reported by SecurityReason.
Fixed bug #42869 (automatic session id insertion adds sessions id to non-local forms).
Fixed bug #41561 (Values set with php_admin_* in httpd.conf can be overwritten with ini_set())

下載
http://www.php.net/downloads.php#v5

2007年11月4日 星期日

PHP 使用GD庫處理PNG圖片縮圖之問題

最近使用GD庫(imagepng)PNG圖片突然顯示錯誤
gd-png: fatal libpng error: zlib error
怪怪,之前還運行好好的一瞬間就出錯
偵錯了一天總算解開這個錯誤的源頭
起因在於圖片的壓縮比率
PHP5.1版起GD庫 imagepng 接受壓縮比參數,參數值為 0-9
此問題就錯在於使用了 imagejpeg 所用的 0 - 100 的格式
傳遞給 imagepng 使用
壓縮比太高GD庫當但報錯
記住....
imagepng 是 0-9 imagjpeg 是 0 - 100