[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 

[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 

FreeBSD + Lighttpd + php + mysql 安裝過程

系統環境 :FreeBSD 6.0-RELEASE 安裝過程如下:

  1. mysql-server-5.0.24a
  2. php5-5.1.6
  3. lighttpd-1.4.11_1 先安裝

lighttpd

cd /usr/ports/www/lighttpd/ make config [X] OPENSSL Enable SSL support [ ] OPENLDAP Enable LDAP support [X] MYSQL Enable MYSQL support [X] IPV6 Enable IPV6 support [X] CML Enable Cache Meta Language support make install clean 安裝 mysql cd /usr/ports/databases/mysql50-server make install clean WITH_CHARSET=utf8 WITH_LINUXTHREADS=yes 安裝 php cd /usr/ports/lang/php5 cd /usr/ports/lang/php5-extensions/ make config 選擇你想要的 extensions 灌好之後 就可以用了 然後打開 家目錄設定 userdir.path = “public_html” userdir.basepath = “/home/” 重點來了 如果執行網頁 http://localhost/phpinfo.php 出現 550 error 則你忘記執行fastcgi php-cgi -v PHP 5.1.6 (cgi-fcgi) (built: Sep 19 2006 22:42:28) (DEBUG) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies 然後執行 php-cgi -b 127.0.0.1:81 & 即可 port部份 隨機bind一個即可 lighttpd 設定檔{#p83}

[Read More]