├── README.md
├── config
└── config.php
├── controller
└── homeController.php
├── error
├── 2011-12-28_SQL.txt
├── 2011-12-30_SQL.txt
└── download.error
├── index.php
├── lib
└── my_test.php
├── model
└── homeModel.php
├── system
├── app.php
├── core
│ ├── controller.php
│ └── model.php
└── lib
│ ├── lib_cache.php
│ ├── lib_crypt.php
│ ├── lib_download.php
│ ├── lib_mysql.php
│ ├── lib_route.php
│ ├── lib_template.php
│ └── lib_thumbnail.php
├── view
└── home.php
└── 自己动手写PHP MVC框架教程.txt
/README.md:
--------------------------------------------------------------------------------
1 | tiny-php-framework
2 | ==================
3 |
4 | This is a tiny php framework for learning MVC
5 |
6 | by yuansir-web.com
7 |
8 | 这是一个自己动手写PHP MVC框架的教程源码
9 | 具体教程请查看 http://www.yuansir-web.com/?p=438
10 |
--------------------------------------------------------------------------------
/config/config.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 |
9 | /*数据库配置*/
10 | $CONFIG['system']['db'] = array(
11 | 'db_host' => 'localhost',
12 | 'db_user' => 'root',
13 | 'db_password' => '',
14 | 'db_database' => 'app',
15 | 'db_table_prefix' => 'app_',
16 | 'db_charset' => 'urf8',
17 | 'db_conn' => '', //数据库连接标识; pconn 为长久链接,默认为即时链接
18 |
19 | );
20 |
21 | /*自定义类库配置*/
22 | $CONFIG['system']['lib'] = array(
23 | 'prefix' => 'my' //自定义类库的文件前缀
24 | );
25 |
26 | $CONFIG['system']['route'] = array(
27 | 'default_controller' => 'home', //系统默认控制器
28 | 'default_action' => 'index', //系统默认控制器
29 | 'url_type' => 1 /*定义URL的形式 1 为普通模式 index.php?c=controller&a=action&id=2
30 | * 2 为PATHINFO index.php/controller/action/id/2(暂时不实现)
31 | */
32 | );
33 |
34 | /*缓存配置*/
35 | $CONFIG['system']['cache'] = array(
36 | 'cache_dir' => 'cache', //缓存路径,相对于根目录
37 | 'cache_prefix' => 'cache_',//缓存文件名前缀
38 | 'cache_time' => 1800, //缓存时间默认1800秒
39 | 'cache_mode' => 2, //mode 1 为serialize ,model 2为保存为可执行文件
40 | );
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/controller/homeController.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 | class homeController extends Controller {
9 |
10 | public function __construct() {
11 | parent::__construct();
12 | }
13 |
14 | public function index() {
15 | $object = $this->load('download',FALSE);
16 | var_dump($object);
17 | exit();
18 | //
19 | // $object->set_byfile('http://localhost/framework/error/2011-12-28_SQL.txt');//服务器文件名,包括路径
20 | // $object->filename = "2011-12-28_SQL.txt";//下载另存为的文件名
21 | // $object->download();
22 | }
23 | }
24 |
25 |
--------------------------------------------------------------------------------
/error/2011-12-28_SQL.txt:
--------------------------------------------------------------------------------
1 | 数据库不可用:
2 |
3 | 客户IP:127.0.0.1
4 | 时间 :2011-12-28 07:43:18
5 |
6 |
--------------------------------------------------------------------------------
/error/2011-12-30_SQL.txt:
--------------------------------------------------------------------------------
1 | 错误SQL语句:
2 | show tables
3 | 客户IP:127.0.0.1
4 | 时间 :2011-12-30 02:54:18
5 |
6 | SQL语句错误
7 | show tables
8 | 客户IP:127.0.0.1
9 | 时间 :2011-12-30 02:54:18
10 |
11 | 错误SQL语句:
12 | show tables
13 | 客户IP:127.0.0.1
14 | 时间 :2011-12-30 02:54:42
15 |
16 | SQL语句错误
17 | show tables
18 | 客户IP:127.0.0.1
19 | 时间 :2011-12-30 02:54:42
20 |
21 |
--------------------------------------------------------------------------------
/error/download.error:
--------------------------------------------------------------------------------
1 | begin download
2 | end download
3 | begin download
4 | end download
5 | begin download
6 | end download
7 | begin download
8 | end download
9 | begin download
10 | end download
11 | begin download
12 | end download
13 | begin download
14 | end download
15 | begin download
16 | end download
17 | begin download
18 | end download
19 | begin download
20 | end download
21 | begin download
22 | end download
23 | begin download
24 | end download
25 | begin download
26 | end download
27 | begin download
28 | end download
29 | begin download
30 | end download
31 | begin download
32 | end download
33 | begin download
34 | begin download
35 | begin download
36 | begin download
37 | begin download
38 | begin download
39 | begin download
40 | begin download
41 | begin download
42 | begin download
43 | end download
44 | begin download
45 | end download
46 | begin download
47 | end download
48 | begin download
49 | end download
50 | begin download
51 | end download
52 | begin download
53 | end download
54 | begin download
55 | end download
56 | begin download
57 | end download
58 | begin download
59 | end download
60 | begin download
61 | end download
62 | begin download
63 | end download
64 | begin download
65 | end download
66 | begin download
67 | end download
68 | begin download
69 | end download
70 | begin download
71 | end download
72 | begin download
73 | end download
74 | begin download
75 | end download
76 | begin download
77 | end download
78 | begin download
79 | end download
80 | begin download
81 | end download
82 | begin download
83 | end download
84 | begin download
85 | end download
86 | begin download
87 | end download
88 | begin download
89 | end download
90 | begin download
91 | end download
92 | begin download
93 | end download
94 | begin download
95 | begin download
96 | begin download
97 | begin download
98 | begin download
99 | begin download
100 | end download
101 | begin download
102 | end download
103 | begin download
104 | end download
105 | begin download
106 | end download
107 | begin download
108 | end download
109 | begin download
110 | end download
111 | begin download
112 | end download
113 | begin download
114 | end download
115 | begin download
116 | end download
117 | begin download
118 | end download
119 | begin download
120 | end download
121 | begin download
122 | end download
123 | begin download
124 | end download
125 | begin download
126 | end download
127 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 | require dirname(__FILE__).'/system/app.php';
9 | require dirname(__FILE__).'/config/config.php';
10 | Application::run($CONFIG);
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/lib/my_test.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 | class homeModel extends Model{
9 | function test(){
10 | echo "this is test homeModel";
11 | }
12 |
13 | function testResult(){
14 | $this->db->show_databases();
15 | }
16 | }
17 |
18 |
19 |
--------------------------------------------------------------------------------
/system/app.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 | define('SYSTEM_PATH', dirname(__FILE__));
9 | define('ROOT_PATH', substr(SYSTEM_PATH, 0,-7));
10 | define('SYS_LIB_PATH', SYSTEM_PATH.'/lib');
11 | define('APP_LIB_PATH', ROOT_PATH.'/lib');
12 | define('SYS_CORE_PATH', SYSTEM_PATH.'/core');
13 | define('CONTROLLER_PATH', ROOT_PATH.'/controller');
14 | define('MODEL_PATH', ROOT_PATH.'/model');
15 | define('VIEW_PATH', ROOT_PATH.'/view');
16 | define('LOG_PATH', ROOT_PATH.'/error/');
17 | final class Application {
18 | public static $_lib = null;
19 | public static $_config = null;
20 | public static function init() {
21 | self::setAutoLibs();
22 | require SYS_CORE_PATH.'/model.php';
23 | require SYS_CORE_PATH.'/controller.php';
24 |
25 | }
26 | /**
27 | * 创建应用
28 | * @access public
29 | * @param array $config
30 | */
31 | public static function run($config){
32 | self::$_config = $config['system'];
33 | self::init();
34 | self::autoload();
35 | self::$_lib['route']->setUrlType(self::$_config['route']['url_type']); //设置url的类型
36 | $url_array = self::$_lib['route']->getUrlArray(); //将url转发成数组
37 | self::routeToCm($url_array);
38 | }
39 | /**
40 | * 自动加载类库
41 | * @access public
42 | * @param array $_lib
43 | */
44 | public static function autoload(){
45 | foreach (self::$_lib as $key => $value){
46 | require (self::$_lib[$key]);
47 | $lib = ucfirst($key);
48 | self::$_lib[$key] = new $lib;
49 | }
50 | //初始化cache
51 | if(is_object(self::$_lib['cache'])){
52 | self::$_lib['cache']->init(
53 | ROOT_PATH.'/'.self::$_config['cache']['cache_dir'],
54 | self::$_config['cache']['cache_prefix'],
55 | self::$_config['cache']['cache_time'],
56 | self::$_config['cache']['cache_mode']
57 | );
58 | }
59 | }
60 | /**
61 | * 加载类库
62 | * @access public
63 | * @param string $class_name 类库名称
64 | * @return object
65 | */
66 | public static function newLib($class_name){
67 | $app_lib = $sys_lib = '';
68 | $app_lib = APP_LIB_PATH.'/'.self::$_config['lib']['prefix'].'_'.$class_name.'.php';
69 | $sys_lib = SYS_LIB_PATH.'/lib_'.$class_name.'.php';
70 |
71 | if(file_exists($app_lib)){
72 | require ($app_lib);
73 | $class_name = ucfirst(self::$_config['lib']['prefix']).ucfirst($class_name);
74 | return new $class_name;
75 | }else if(file_exists($sys_lib)){
76 | require ($sys_lib);
77 | return self::$_lib['$class_name'] = new $class_name;
78 | }else{
79 | trigger_error('加载 '.$class_name.' 类库不存在');
80 | }
81 | }
82 | /**
83 | * 自动加载的类库
84 | * @access public
85 | */
86 | public static function setAutoLibs(){
87 | self::$_lib = array(
88 | 'route' => SYS_LIB_PATH.'/lib_route.php',
89 | 'mysql' => SYS_LIB_PATH.'/lib_mysql.php',
90 | 'template' => SYS_LIB_PATH.'/lib_template.php',
91 | 'cache' => SYS_LIB_PATH.'/lib_cache.php',
92 | 'thumbnail' => SYS_LIB_PATH.'/lib_thumbnail.php'
93 | );
94 | }
95 | /**
96 | * 根据URL分发到Controller和Model
97 | * @access public
98 | * @param array $url_array
99 | */
100 | public static function routeToCm($url_array = array()){
101 | $app = '';
102 | $controller = '';
103 | $action = '';
104 | $model = '';
105 | $params = '';
106 |
107 | if(isset($url_array['app'])){
108 | $app = $url_array['app'];
109 | }
110 |
111 | if(isset($url_array['controller'])){
112 | $controller = $model = $url_array['controller'];
113 | if($app){
114 | $controller_file = CONTROLLER_PATH.'/'.$app.'/'.$controller.'Controller.php';
115 | $model_file = MODEL_PATH.'/'.$app.'/'.$model.'Model.php';
116 | }else{
117 | $controller_file = CONTROLLER_PATH.'/'.$controller.'Controller.php';
118 | $model_file = MODEL_PATH.'/'.$model.'Model.php';
119 | }
120 | }else{
121 | $controller = $model = self::$_config['route']['default_controller'];
122 | if($app){
123 | $controller_file = CONTROLLER_PATH.'/'.$app.'/'.self::$_config['route']['default_controller'].'Controller.php';
124 | $model_file = MODEL_PATH.'/'.$app.'/'.self::$_config['route']['default_controller'].'Model.php';
125 | }else{
126 | $controller_file = CONTROLLER_PATH.'/'.self::$_config['route']['default_controller'].'Controller.php';
127 | $model_file = MODEL_PATH.'/'.self::$_config['route']['default_controller'].'Model.php';
128 | }
129 | }
130 | if(isset($url_array['action'])){
131 | $action = $url_array['action'];
132 | }else{
133 | $action = self::$_config['route']['default_action'];
134 | }
135 |
136 | if(isset($url_array['params'])){
137 | $params = $url_array['params'];
138 | }
139 | if(file_exists($controller_file)){
140 | if (file_exists($model_file)) {
141 | require $model_file;
142 | }
143 | require $controller_file;
144 | $controller = $controller.'Controller';
145 | $controller = new $controller;
146 | if($action){
147 | if(method_exists($controller, $action)){
148 | isset($params) ? $controller ->$action($params) : $controller ->$action();
149 | }else{
150 | die('控制器方法不存在');
151 | }
152 | }else{
153 | die('控制器方法不存在');
154 | }
155 | }else{
156 | die('控制器不存在');
157 | }
158 | }
159 |
160 |
161 |
162 | }
163 |
164 |
165 |
--------------------------------------------------------------------------------
/system/core/controller.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 | class Controller{
9 |
10 | public function __construct() {
11 | // header('Content-type:text/html;chartset=utf-8');
12 | }
13 | /**
14 | * 实例化模型
15 | * @access final protected
16 | * @param string $model 模型名称
17 | */
18 | final protected function model($model) {
19 | if (empty($model)) {
20 | trigger_error('不能实例化空模型');
21 | }
22 | $model_name = $model . 'Model';
23 | return new $model_name;
24 | }
25 | /**
26 | * 加载类库
27 | * @param string $lib 类库名称
28 | * @param Bool $my 如果FALSE默认加载系统自动加载的类库,如果为TRUE则加载非自动加载类库
29 | * @return type
30 | */
31 | final protected function load($lib,$auto = TRUE){
32 | if(empty($lib)){
33 | trigger_error('加载类库名不能为空');
34 | }elseif($auto === TRUE){
35 | return Application::$_lib[$lib];
36 | }elseif($auto === FALSE){
37 | return Application::newLib($lib);
38 | }
39 | }
40 | /**
41 | * 加载系统配置,默认为系统配置 $CONFIG['system'][$config]
42 | * @access final protected
43 | * @param string $config 配置名
44 | */
45 | final protected function config($config){
46 | return Application::$_config[$config];
47 | }
48 | /**
49 | * 加载模板文件
50 | * @access final protect
51 | * @param string $path 模板路径
52 | * @return string 模板字符串
53 | */
54 | final protected function showTemplate($path,$data = array()){
55 | $template = $this->load('template');
56 | $template->init($path,$data);
57 | $template->outPut();
58 | }
59 | }
60 |
61 |
62 |
--------------------------------------------------------------------------------
/system/core/model.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 | class Model {
9 | protected $db = null;
10 |
11 | final public function __construct() {
12 | header('Content-type:text/html;chartset=utf-8');
13 | $this->db = $this->load('mysql');
14 | $config_db = $this->config('db');
15 | $this->db->init(
16 | $config_db['db_host'],
17 | $config_db['db_user'],
18 | $config_db['db_password'],
19 | $config_db['db_database'],
20 | $config_db['db_conn'],
21 | $config_db['db_charset']
22 | ); //初始话数据库类
23 | }
24 | /**
25 | * 根据表前缀获取表名
26 | * @access final protected
27 | * @param string $table_name 表名
28 | */
29 | final protected function table($table_name){
30 | $config_db = $this->config('db');
31 | return $config_db['db_table_prefix'].$table_name;
32 | }
33 | /**
34 | * 加载类库
35 | * @param string $lib 类库名称
36 | * @param Bool $my 如果FALSE默认加载系统自动加载的类库,如果为TRUE则加载自定义类库
37 | * @return type
38 | */
39 | final protected function load($lib,$my = FALSE){
40 | if(empty($lib)){
41 | trigger_error('加载类库名不能为空');
42 | }elseif($my === FALSE){
43 | return Application::$_lib[$lib];
44 | }elseif($my === TRUE){
45 | return Application::newLib($lib);
46 | }
47 | }
48 | /**
49 | * 加载系统配置,默认为系统配置 $CONFIG['system'][$config]
50 | * @access final protected
51 | * @param string $config 配置名
52 | */
53 | final protected function config($config=''){
54 | return Application::$_config[$config];
55 | }
56 |
57 |
58 |
59 |
60 | }
61 |
62 |
63 |
--------------------------------------------------------------------------------
/system/lib/lib_cache.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 | final class Cache{
9 | private $cache_dir = null;
10 | private $cache_prefix = null;
11 | private $cache_time = null;
12 | private $cache_mode = null;
13 | /**
14 | * 初始化参数
15 | * @param string $cache_dir 缓存存放的目录
16 | * @param int $cache_time 缓存时间
17 | * @param int $cache_mod 缓存模式
18 | */
19 | public function init($cache_dir = 'cache',$cache_prefix = 'cache_',$cache_time = 1800,$cache_mod = 1) {
20 | $this->cache_dir = $cache_dir;
21 | $this->cache_prefix = $cache_prefix;
22 | $this->cache_time = $cache_time;
23 | $this->cache_mode = $cache_mod;
24 | }
25 | /**
26 | * 设置缓存
27 | * @param string 缓存名
28 | * @param array 缓存内容
29 | */
30 | public function set($id,$data){
31 | if(!isset($id)){
32 | return false;
33 | }
34 | $cache = array(
35 | 'file' => $this->getFileName($id, $this->cache_dir),
36 | 'data' => $data
37 | );
38 | return $this->writeCache($cache);
39 | }
40 | /**
41 | * 获取缓存
42 | * @param string $id 缓存名称
43 | * @return bool/array
44 | */
45 | public function get($id){
46 | if(!$this->hasCache($id)){
47 | return false;
48 | }
49 | $data = $this->getCacheData($id);
50 | return $data;
51 | }
52 | /**
53 | * 获取缓存目录
54 | * @return string
55 | */
56 | public function getCacheDir(){
57 | return $cache_dir = trim($this->cache_dir,'/');
58 | }
59 | /**
60 | * 获取完整缓存文件名称
61 | * @param string $id 缓存名
62 | * @return string
63 | */
64 | public function getFileName($id){
65 | return $this->getCacheDir().'/'.$this->cache_prefix.$id.'.php';
66 | }
67 | /**
68 | * 根据缓存文件返回缓存名称
69 | * @param type $file
70 | */
71 | public function getCacheName($file){
72 | if(!file_exists($file)){
73 | return FALSE;
74 | }
75 | $filename = basename($file);
76 | preg_match('/^'.$this->cache_prefix.'(.*).php$/i', $filename,$matches);
77 | return $matches[1];
78 | }
79 | /**
80 | * 写入缓存
81 | * @param array $cache 缓存数据
82 | */
83 | public function writeCache($cache = array()){
84 | if(!is_dir($this->getCacheDir())){
85 | mkdir($this->getCacheDir(),0777);
86 | }elseif(!is_writable($this->getCacheDir())){
87 | chmod($this->getCacheDir(), 0777);
88 | }
89 |
90 | if($this->cache_mode == 1){
91 | $content = serialize($cache['data']);
92 | }else{
93 | $content = "getFileName($id))){
121 | if(time() > filemtime($this->getFileName($id))+$this->cache_time){
122 | unlink($this->getFileName($id));
123 | }
124 | }
125 | return file_exists($this->getFileName($id)) ? TRUE : FALSE;
126 | }
127 | /**
128 | * 删除一条缓存
129 | * @param string $id 缓存名
130 | * @return bool
131 | */
132 | public function deleteCache($id){
133 | if($this->hasCache($id)){
134 | return unlink($this->getFileName($id));
135 | }else{
136 | trigger_error('缓存不存在');
137 | }
138 | }
139 | /**
140 | * 获取缓存数据
141 | * @param string $id 缓存名
142 | * @return array
143 | */
144 | public function getCacheData($id){
145 | if(!$this->hasCache($id)){
146 | return false;
147 | }
148 | if($this->cache_mode == 1){
149 | $fp = @fopen($this->getFileName($id), r);
150 | $data = @fread($fp, filesize($this->getFileName($id)));
151 | @fclose($fp);
152 | return unserialize($data);
153 | }else{
154 | return include $this->getFileName($id);
155 | }
156 | }
157 | /**
158 | * 清空缓存
159 | * @return bool
160 | */
161 | public function flushCache(){
162 | $glob = @glob($this->getCacheDir().'/'.$this->cache_prefix.'*');
163 | if($glob){
164 | foreach($glob as $item){
165 | $id = $this->getCacheName($item);
166 | $this->deleteCache($id);
167 | }
168 | }
169 | return TRUE;
170 | }
171 |
172 | }
173 |
174 |
175 |
--------------------------------------------------------------------------------
/system/lib/lib_crypt.php:
--------------------------------------------------------------------------------
1 |
7 | * @version 1.0
8 | */
9 | class Crypt {
10 |
11 | public function encrypt($string, $key) {
12 | $str_len = strlen($string);
13 | $key_len = strlen($key);
14 | for ($i = 0; $i < $str_len; $i++) {
15 | for ($j = 0; $j < $key_len; $j++) {
16 | $string[$i] = $string[$i] ^ $key[$j];
17 | }
18 | }
19 | return $string;
20 | }
21 |
22 | public function decrypt($string, $key) {
23 | $str_len = strlen($string);
24 | $key_len = strlen($key);
25 | for ($i = 0; $i < $str_len; $i++) {
26 | for ($j = 0; $j < $key_len; $j++) {
27 | $string[$i] = $key[$j] ^ $string[$i];
28 | }
29 | }
30 | return $string;
31 | }
32 |
33 | }
--------------------------------------------------------------------------------
/system/lib/lib_download.php:
--------------------------------------------------------------------------------
1 |
7 | * @version 1.0
8 | */
9 | class download {
10 |
11 | var $debug = true;
12 | var $errormsg = '';
13 | var $Filter = array();
14 | var $filename = '';
15 | var $mineType = 'text/plain';
16 | var $xlq_filetype = array();
17 |
18 | /**
19 | * 初始化
20 | * @param string $file_filter 禁止下载的文件,文件后缀
21 | * @param type $isdebug
22 | */
23 | function init($file_filter='', $isdebug=true) {
24 | $this->setFilter($fileFilter);
25 | $this->setDebug($isdebug);
26 | $this->setFileType();
27 | }
28 |
29 | function setFilter($fileFilter) {
30 | if (empty($fileFilter))
31 | return;
32 | $this->Filter = explode(',', strtolower($fileFilter));
33 | }
34 |
35 | function setDebug($debug) {
36 | $this->debug = $debug;
37 | }
38 |
39 | function setFileName($filename) {
40 | $this->filename = $filename;
41 | }
42 |
43 | function downloadFile($filename) {
44 | $this->setFileName($filename);
45 | if ($this->filecheck()) {
46 | $fn = array_pop(explode('/', strtr($this->filename, '', '/')));
47 | header("Pragma: public");
48 | header("Expires: 0"); // set expiration time
49 | header("Cache-Component: must-revalidate, post-check=0, pre-check=0");
50 | header("Content-type:" . $this->mineType);
51 | header("Content-Length: " . filesize($this->filename));
52 | header("Content-Disposition: attachment; filename=\"$fn\"");
53 | header('Content-Transfer-Encoding: binary');
54 | readfile($this->filename);
55 | return true;
56 | } else {
57 | return false;
58 | }
59 | }
60 |
61 | function getErrorMsg() {
62 | return $this->errormsg;
63 | }
64 |
65 | function fileCheck() {
66 | $filename = $this->filename;
67 | if (file_exists($filename)) {
68 | $filetype = strtolower(array_pop(explode('.', $filename)));
69 | if (in_array($filetype, $this->Filter)) {
70 | $this->errormsg.=$filename . '不允许下载!';
71 | if ($this->debug)
72 | exit($filename . '不允许下载!');
73 | return false;
74 | }else {
75 | if (function_exists("mime_content_type")) {
76 | $this->mineType = mime_content_type($filename);
77 | }
78 | if (empty($this->mineType)) {
79 | if (isset($this->xlq_filetype[$filetype]))
80 | $this->mineType = $this->xlq_filetype[$filetype];
81 | }
82 | if (!empty($this->mineType))
83 | return true;
84 | else {
85 | $this->errormsg.='获取文件类型时候发生错误,或者不存在预定文件类型内';
86 | if ($this->debug)
87 | exit('获取文件类型出错');
88 | return false;
89 | }
90 | }
91 | }else {
92 | $this->errormsg.='SORRY,你要下载的文件不存在!!';
93 | if ($this->debug)
94 | exit($filename . '不存在!');
95 | return false;
96 | }
97 | }
98 |
99 | function setFileType() {
100 | $this->xlq_filetype['chm'] = 'application/octet-stream';
101 | $this->xlq_filetype['ppt'] = 'application/vnd.ms-powerpoint';
102 | $this->xlq_filetype['xls'] = 'application/vnd.ms-excel';
103 | $this->xlq_filetype['doc'] = 'application/msword';
104 | $this->xlq_filetype['exe'] = 'application/octet-stream';
105 | $this->xlq_filetype['rar'] = 'application/octet-stream';
106 | $this->xlq_filetype['js'] = "javascrīpt/js";
107 | $this->xlq_filetype['css'] = "text/css";
108 | $this->xlq_filetype['hqx'] = "application/mac-binhex40";
109 | $this->xlq_filetype['bin'] = "application/octet-stream";
110 | $this->xlq_filetype['oda'] = "application/oda";
111 | $this->xlq_filetype['pdf'] = "application/pdf";
112 | $this->xlq_filetype['ai'] = "application/postsrcipt";
113 | $this->xlq_filetype['eps'] = "application/postsrcipt";
114 | $this->xlq_filetype['es'] = "application/postsrcipt";
115 | $this->xlq_filetype['rtf'] = "application/rtf";
116 | $this->xlq_filetype['mif'] = "application/x-mif";
117 | $this->xlq_filetype['csh'] = "application/x-csh";
118 | $this->xlq_filetype['dvi'] = "application/x-dvi";
119 | $this->xlq_filetype['hdf'] = "application/x-hdf";
120 | $this->xlq_filetype['nc'] = "application/x-netcdf";
121 | $this->xlq_filetype['cdf'] = "application/x-netcdf";
122 | $this->xlq_filetype['latex'] = "application/x-latex";
123 | $this->xlq_filetype['ts'] = "application/x-troll-ts";
124 | $this->xlq_filetype['src'] = "application/x-wais-source";
125 | $this->xlq_filetype['zip'] = "application/zip";
126 | $this->xlq_filetype['bcpio'] = "application/x-bcpio";
127 | $this->xlq_filetype['cpio'] = "application/x-cpio";
128 | $this->xlq_filetype['gtar'] = "application/x-gtar";
129 | $this->xlq_filetype['shar'] = "application/x-shar";
130 | $this->xlq_filetype['sv4cpio'] = "application/x-sv4cpio";
131 | $this->xlq_filetype['sv4crc'] = "application/x-sv4crc";
132 | $this->xlq_filetype['tar'] = "application/x-tar";
133 | $this->xlq_filetype['ustar'] = "application/x-ustar";
134 | $this->xlq_filetype['man'] = "application/x-troff-man";
135 | $this->xlq_filetype['sh'] = "application/x-sh";
136 | $this->xlq_filetype['tcl'] = "application/x-tcl";
137 | $this->xlq_filetype['tex'] = "application/x-tex";
138 | $this->xlq_filetype['texi'] = "application/x-texinfo";
139 | $this->xlq_filetype['texinfo'] = "application/x-texinfo";
140 | $this->xlq_filetype['t'] = "application/x-troff";
141 | $this->xlq_filetype['tr'] = "application/x-troff";
142 | $this->xlq_filetype['roff'] = "application/x-troff";
143 | $this->xlq_filetype['shar'] = "application/x-shar";
144 | $this->xlq_filetype['me'] = "application/x-troll-me";
145 | $this->xlq_filetype['ts'] = "application/x-troll-ts";
146 | $this->xlq_filetype['gif'] = "image/gif";
147 | $this->xlq_filetype['jpeg'] = "image/pjpeg";
148 | $this->xlq_filetype['jpg'] = "image/pjpeg";
149 | $this->xlq_filetype['jpe'] = "image/pjpeg";
150 | $this->xlq_filetype['ras'] = "image/x-cmu-raster";
151 | $this->xlq_filetype['pbm'] = "image/x-portable-bitmap";
152 | $this->xlq_filetype['ppm'] = "image/x-portable-pixmap";
153 | $this->xlq_filetype['xbm'] = "image/x-xbitmap";
154 | $this->xlq_filetype['xwd'] = "image/x-xwindowdump";
155 | $this->xlq_filetype['ief'] = "image/ief";
156 | $this->xlq_filetype['tif'] = "image/tiff";
157 | $this->xlq_filetype['tiff'] = "image/tiff";
158 | $this->xlq_filetype['pnm'] = "image/x-portable-anymap";
159 | $this->xlq_filetype['pgm'] = "image/x-portable-graymap";
160 | $this->xlq_filetype['rgb'] = "image/x-rgb";
161 | $this->xlq_filetype['xpm'] = "image/x-xpixmap";
162 | $this->xlq_filetype['txt'] = "text/plain";
163 | $this->xlq_filetype['c'] = "text/plain";
164 | $this->xlq_filetype['cc'] = "text/plain";
165 | $this->xlq_filetype['h'] = "text/plain";
166 | $this->xlq_filetype['html'] = "text/html";
167 | $this->xlq_filetype['htm'] = "text/html";
168 | $this->xlq_filetype['htl'] = "text/html";
169 | $this->xlq_filetype['rtx'] = "text/richtext";
170 | $this->xlq_filetype['etx'] = "text/x-setext";
171 | $this->xlq_filetype['tsv'] = "text/tab-separated-values";
172 | $this->xlq_filetype['mpeg'] = "video/mpeg";
173 | $this->xlq_filetype['mpg'] = "video/mpeg";
174 | $this->xlq_filetype['mpe'] = "video/mpeg";
175 | $this->xlq_filetype['avi'] = "video/x-msvideo";
176 | $this->xlq_filetype['qt'] = "video/quicktime";
177 | $this->xlq_filetype['mov'] = "video/quicktime";
178 | $this->xlq_filetype['moov'] = "video/quicktime";
179 | $this->xlq_filetype['movie'] = "video/x-sgi-movie";
180 | $this->xlq_filetype['au'] = "audio/basic";
181 | $this->xlq_filetype['snd'] = "audio/basic";
182 | $this->xlq_filetype['wav'] = "audio/x-wav";
183 | $this->xlq_filetype['aif'] = "audio/x-aiff";
184 | $this->xlq_filetype['aiff'] = "audio/x-aiff";
185 | $this->xlq_filetype['aifc'] = "audio/x-aiff";
186 | $this->xlq_filetype['swf'] = "application/x-shockwave-flash";
187 | }
188 |
189 | }
190 |
191 | //$download = new download();
192 | //$download->downloadFile('./logo.png');
193 |
--------------------------------------------------------------------------------
/system/lib/lib_mysql.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 | final class Mysql {
9 | private $db_host; //数据库主机
10 | private $db_user; //数据库用户名
11 | private $db_pwd; //数据库用户名密码
12 | private $db_database; //数据库名
13 | private $conn; //数据库连接标识;
14 | private $result; //执行query命令的结果资源标识
15 | private $sql; //sql执行语句
16 | private $row; //返回的条目数
17 | private $coding; //数据库编码,GBK,UTF8,gb2312
18 | private $bulletin = true; //是否开启错误记录
19 | private $show_error = true; //测试阶段,显示所有错误,具有安全隐患,默认关闭
20 | private $is_error = false; //发现错误是否立即终止,默认true,建议不启用,因为当有问题时用户什么也看不到是很苦恼的
21 |
22 | /*构造函数*/
23 | // public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) {
24 | // $this->db_host = $db_host;
25 | // $this->db_user = $db_user;
26 | // $this->db_pwd = $db_pwd;
27 | // $this->db_database = $db_database;
28 | // $this->conn = $conn;
29 | // $this->coding = $coding;
30 | // $this->connect();
31 | // }
32 | /*初始化函数*/
33 | public function init($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) {
34 | $this->db_host = $db_host;
35 | $this->db_user = $db_user;
36 | $this->db_pwd = $db_pwd;
37 | $this->db_database = $db_database;
38 | $this->conn = $conn;
39 | $this->coding = $coding;
40 | $this->connect();
41 | }
42 | /*数据库连接*/
43 | public function connect() {
44 | if ($this->conn == "pconn") {
45 | //永久链接
46 | $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
47 | } else {
48 | //即时链接
49 | $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
50 | }
51 |
52 | if (!mysql_select_db($this->db_database, $this->conn)) {
53 | if ($this->show_error) {
54 | $this->show_error("数据库不可用:", $this->db_database);
55 | }
56 | }
57 | mysql_query("SET NAMES $this->coding");
58 | }
59 |
60 | /*数据库执行语句,可执行查询添加修改删除等任何sql语句*/
61 | public function query($sql) {
62 | if ($sql == "") {
63 | $this->show_error("SQL语句错误:", "SQL查询语句为空");
64 | }
65 | $this->sql = $sql;
66 |
67 | $result = mysql_query($this->sql, $this->conn);
68 |
69 | if (!$result) {
70 | //调试中使用,sql语句出错时会自动打印出来
71 | if ($this->show_error) {
72 | $this->show_error("错误SQL语句:", $this->sql);
73 | }
74 | } else {
75 | $this->result = $result;
76 | }
77 | return $this->result;
78 | }
79 |
80 | /*创建添加新的数据库*/
81 | public function create_database($database_name) {
82 | $database = $database_name;
83 | $sqlDatabase = 'create database ' . $database;
84 | $this->query($sqlDatabase);
85 | }
86 |
87 | /*查询服务器所有数据库*/
88 | //将系统数据库与用户数据库分开,更直观的显示?
89 | public function show_databases() {
90 | $this->query("show databases");
91 | echo "现有数据库:" . $amount = $this->db_num_rows();
92 | echo "
";
93 | $i = 1;
94 | while ($row = $this->fetch_array()) {
95 | echo "$i $row[Database]";
96 | echo "
";
97 | $i++;
98 | }
99 | }
100 |
101 | //以数组形式返回主机中所有数据库名
102 | public function databases() {
103 | $rsPtr = mysql_list_dbs($this->conn);
104 | $i = 0;
105 | $cnt = mysql_num_rows($rsPtr);
106 | while ($i < $cnt) {
107 | $rs[] = mysql_db_name($rsPtr, $i);
108 | $i++;
109 | }
110 | return $rs;
111 | }
112 |
113 | /*查询数据库下所有的表*/
114 | public function show_tables($database_name) {
115 | $this->query("show tables");
116 | echo "现有数据库:" . $amount = $this->db_num_rows();
117 | echo "
";
118 | $i = 1;
119 | while ($row = $this->fetch_array()) {
120 | $columnName = "Tables_in_" . $database_name;
121 | echo "$i $row[$columnName]";
122 | echo "
";
123 | $i++;
124 | }
125 | }
126 |
127 | /*
128 | mysql_fetch_row() array $row[0],$row[1],$row[2]
129 | mysql_fetch_array() array $row[0] 或 $row[id]
130 | mysql_fetch_assoc() array 用$row->content 字段大小写敏感
131 | mysql_fetch_object() object 用$row[id],$row[content] 字段大小写敏感
132 | */
133 |
134 | /*取得结果数据*/
135 | public function mysql_result_li() {
136 | return mysql_result($str);
137 | }
138 |
139 | /*取得记录集,获取数组-索引和关联,使用$row['content'] */
140 | public function fetch_array() {
141 | return mysql_fetch_array($this->result);
142 | }
143 |
144 | //获取关联数组,使用$row['字段名']
145 | public function fetch_assoc() {
146 | return mysql_fetch_assoc($this->result);
147 | }
148 |
149 | //获取数字索引数组,使用$row[0],$row[1],$row[2]
150 | public function fetch_row() {
151 | return mysql_fetch_row($this->result);
152 | }
153 |
154 | //获取对象数组,使用$row->content
155 | public function fetch_Object() {
156 | return mysql_fetch_object($this->result);
157 | }
158 |
159 | //简化查询select
160 | public function findall($table) {
161 | $this->query("SELECT * FROM $table");
162 | }
163 |
164 | //简化查询select
165 | public function select($table, $columnName = "*", $condition = '', $debug = '') {
166 | $condition = $condition ? ' Where ' . $condition : NULL;
167 | if ($debug) {
168 | echo "SELECT $columnName FROM $table $condition";
169 | } else {
170 | $this->query("SELECT $columnName FROM $table $condition");
171 | }
172 | }
173 |
174 | //简化删除del
175 | public function delete($table, $condition, $url = '') {
176 | if ($this->query("DELETE FROM $table WHERE $condition")) {
177 | if (!empty ($url))
178 | $this->Get_admin_msg($url, '删除成功!');
179 | }
180 | }
181 |
182 | //简化插入insert
183 | public function insert($table, $columnName, $value, $url = '') {
184 | if ($this->query("INSERT INTO $table ($columnName) VALUES ($value)")) {
185 | if (!empty ($url))
186 | $this->Get_admin_msg($url, '添加成功!');
187 | }
188 | }
189 |
190 | //简化修改update
191 | public function update($table, $mod_content, $condition, $url = '') {
192 | //echo "UPDATE $table SET $mod_content WHERE $condition"; exit();
193 | if ($this->query("UPDATE $table SET $mod_content WHERE $condition")) {
194 | if (!empty ($url))
195 | $this->Get_admin_msg($url);
196 | }
197 | }
198 |
199 | /*取得上一步 INSERT 操作产生的 ID*/
200 | public function insert_id() {
201 | return mysql_insert_id();
202 | }
203 |
204 | //指向确定的一条数据记录
205 | public function db_data_seek($id) {
206 | if ($id > 0) {
207 | $id = $id -1;
208 | }
209 | if (!@ mysql_data_seek($this->result, $id)) {
210 | $this->show_error("SQL语句有误:", "指定的数据为空");
211 | }
212 | return $this->result;
213 | }
214 |
215 | // 根据select查询结果计算结果集条数
216 | public function db_num_rows() {
217 | if ($this->result == null) {
218 | if ($this->show_error) {
219 | $this->show_error("SQL语句错误", "暂时为空,没有任何内容!");
220 | }
221 | } else {
222 | return mysql_num_rows($this->result);
223 | }
224 | }
225 |
226 | // 根据insert,update,delete执行结果取得影响行数
227 | public function db_affected_rows() {
228 | return mysql_affected_rows();
229 | }
230 |
231 | //输出显示sql语句
232 | public function show_error($message = "", $sql = "") {
233 | if (!$sql) {
234 | echo "" . $message . "";
235 | echo "
";
236 | } else {
237 | echo "
"; 352 | for ($i = 0; $i < $total; $i++) { 353 | print_r(mysql_fetch_field($this->result, $i)); 354 | } 355 | echo ""; 356 | echo "