[PHP] header下載檔案 搭配資料庫

剛剛在 ECstart 看到有人詢問 header檔案下載錯誤 發現網站義工有回答到這個function,所以就紀錄下來了

function dl_file($file){

   //First, see if the file exists
   if (!is_file($file)) { die("404 File not found!"); }

   //Gather relevent info about file
   $len = filesize($file);
   $filename = basename($file);
   $file_extension = strtolower(substr(strrchr($filename,"."),1));

   //This will set the Content-Type to the appropriate setting for the file
  switch( $file_extension ) {
     case "pdf": $ctype="application/pdf"; break;
     case "exe": $ctype="application/octet-stream"; break;
     case "zip": $ctype="application/zip"; break;
     case "doc": $ctype="application/msword"; break;
     case "xls": $ctype="application/vnd.ms-excel"; break;
     case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
     case "gif": $ctype="image/gif"; break;
     case "png": $ctype="image/png"; break;
     case "jpeg":
     case "jpg": $ctype="image/jpg"; break;
     case "mp3": $ctype="audio/mpeg"; break;
     case "wav": $ctype="audio/x-wav"; break;
     case "mpeg":
     case "mpg":
     case "mpe": $ctype="video/mpeg"; break;
     case "mov": $ctype="video/quicktime"; break;
     case "avi": $ctype="video/x-msvideo"; break;

     //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
     case "php":
     case "htm":
     case "html":
     case "txt": die("Cannot be used for ". $file_extension ." files!"); break;

     default: $ctype="application/force-download";
   }

   //Begin writing headers
   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Cache-Control: public");
   header("Content-Description: File Transfer");
   
   //Use the switch-generated Content-Type
   header("Content-Type: $ctype");

   //Force the download
   $header="Content-Disposition: attachment; filename=".$filename.";";
   header($header );
   header("Content-Transfer-Encoding: binary");
   header("Content-Length: ".$len);
   @readfile($file);
   exit;
}
php 

wordpress 留言被灌爆 – plugin – WP-ImgCode mod

今天有人問我,blog留言版被灌爆,都是廣告信,我想說 wordpress 不是有內建一個外掛 Akismet 這個外掛可以擋掉很多spam,讓你的留言版不至於有廣告信,不過還是可以加上留言版的驗證圖形,這樣會更安全一點。 今天找到一個國人改寫的 WordPress plugin – WP-ImgCode mod 這個還不錯用,安裝方式如下

安裝啟用
  1. 將解壓縮出來的 wp-imgcode 資料夾放到 WordPress 的 plugin 資料夾,預設為 wp-content/plugins。
  2. 在控制台中啟用這個 plugin。
加入驗證碼
  1. 開啟目前使用佈景的相關檔案,例如 comments.php。
  2. 在要顯示驗證碼的地方,加上這個程式碼:
ID); ?>
明天在去搞定別人的網站吧~ 先紀錄一下~

[PHP] 如何切割中文標題

今天看到酷!學園討論區,php版有人問說要如何切割中文字,結果我自己以前弄的一個function就貼了上去,如下

function cut_word($text, $num){
if(strlen($text) > $num) {
   for($i=0;$i<$num;$i++) {
      $ch=substr($text,$i,1);
      if(ord($ch)>127) $i++;
      }
      $text= substr($text,0,$i).".";
   }
   return $text;
}
[Read More]
php 

[Apache] 使用者只能使用 https 進入 mod_rewrite force ssl 進階設定

最近館內機器我想全部使用上ssl機制會比較安全,之前剛來到 國史館台灣文獻館 的時候,有研究助理對我不爽,說什麼我開發的薪資管理系統,沒有ssl機制,會出現漏洞,囧導至系統目前只有我在用,哈哈,不過這不是重點重點是接下來的介紹啦 首先要看看你的機器是否有支援 mod_ssl 目前我用的機器是 CentOS 4.4 所以指令如果有所改變,請自行debug

cat /etc/httpd/conf/httpd.conf | grep rewrite LoadModule rewrite_module modules/mod_rewrite.so 這樣代表有支援了,那如果沒有支援呢,請用下面指令來新增

yum install mod_ssl

[Read More]