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

2011年10月24日 星期一

限制瀏覽器下載檔案,而不是在瀏覽器開啟的方法

作法就是在HTTP的Content-Disposition欄位動手腳

就可以讓瀏覽器下載檔案

例如你希望瀏覽器直接下載jpg檔,而不是在瀏覽器上看到jpg檔,則php程式碼如下:

<?php
        $filename = 'yui.jpg';
        header("Content-type: image/jpeg");
        header("Content-Disposition: attachment; filename=$filename");
        readfile($filename);
?>

接著再把檔案存成xxx.php

當瀏覽器打開xxx.php,就會自動下載yui.jpg

2011年5月23日 星期一

在Command line下執行php程式碼

在php檔的一開頭先加上:
#!/usr/bin/php

很類似寫shell script的感覺。

一些參考資料:
實用技巧:將PHP作為Shell腳本語言使用
http://ericbbs.blogspot.com/2007/11/phpshell.html
用 PHP 寫 Command-Line Script(CLI)
http://plog.longwin.com.tw/programming/2006/12/27/php_command_line_script_2006
PHP也可以當成Shell Script 
http://www.php5.idv.tw/modules.php?mod=books&act=show&shid=918

用PHP發HTTP request

透過PHP cURL就可以做到( http://tw.php.net/curl )

這邊有個自動發plurk的範例程式碼:
http://gist.github.com/53380

<?php
define('NICKNAME', 'abc');
define('PASSWORD', 'iamabc');
define('USER_ID', '123456');
 
$message = 'Hello World!';
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
 
// login
curl_setopt($ch, CURLOPT_URL, 'http://www.plurk.com/Users/login');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'nick_name='.NICKNAME.'&password='.PASSWORD);
curl_exec($ch);
 
// post
curl_setopt($ch, CURLOPT_URL, 'http://www.plurk.com/TimeLine/addPlurk');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'qualifier=says&content='.urlencode($message).'&lang=tr_ch&no_comments=0&uid='.USER_ID);
curl_exec($ch);
 
curl_close($ch);
?>

2011年5月21日 星期六

PHP許蓋功問題

這是一個很古老的問題
在產生SQL query之前,要先以addslashes函示處理。
 然後從資料庫取出資料時,要先以stripslashes函示來處理。

順道一提,php有提供iconv函示,可以轉換字串的編碼型態。
 像是這樣用:
echo iconv("big5", "UTF-8", "This is a test.");