header("Content-Type: $mime");
header("Content-Disposition: attachment; filename="$file"");
header("Content-Length: ".filesize($filename));
if(strstr($mime, "text")){
$file_handle = @fopen("$filename","r");
}else{
$file_handle = @fopen("$filename","rb");
}
php 实现文本文件下载
<?php
$conn = mysql_connect("localhost","root","root") or die("数据库连接失入");
mysql_select_db("new",$conn);
?>
分类:<select name="fid">
<?php
function mainfl()
{
global $conn;
$result=mysql_query("select id,fid,name from a where fid=0 order by id ",$conn);//这里默认的fid=0,表示这是第一级。通过这个可以查出所有fid=0的项,即所有的第一级主类
if ($myrow=mysql_fetch_array($result))
{
do {
?>
<option value=<?php echo $myrow["id"];?>><?php echo $myrow["name"];?></option>//$myrow["id"]输出主类的ID为查询子类做准备,$myrwo["name"]主类名字
<?php echo subfl($myrow["id"],"--");?>//调用子类函数subfl,使用--<?php
}
while ($myrow=mysql_fetch_array($result));
}
}
//定义子级分类
function subfl($fid,$tag)//$fid那是主类ID,$tag为连接符
{
global $conn;
$result1=mysql_query("select id,fid,name from a where fid=$fid order by id",$conn);//查询出fid=$fid的所有面,这里的$fid值为$myrow["id"]
if ($myrow1=mysql_fetch_array($result1))
{
do {
?>
<option value=<?php echo $myrow1["id"];?>><?php echo $tag.$myrow1["name"];?></option>//输出子类
<?php
subfl($myrow1["id"],"-".$tag);
}
while ($myrow1=mysql_fetch_array($result1));
}
}
echo mainfl();
?>
</select>
php 数据查询与连接类
<?php
/**
* Class program for yinghua05-2
* designer :songsong
*/
class MySQL {
var $link;
var $result;
var $querys;
function MySQL($host = '', $user = '', $pw = '', $db = '', $encode = 'UTF8') {
$this->querys = 0;
if($host != '' && $user != '' && $db != '') {
$this->connect($host,$user,$pw,$db,$encode);
}
}
/**
* connect to database
*
* @param unknown_type $host
* @param unknown_type $user
* @param unknown_type $pw
* @param unknown_type $db
* @return boolean
*/
function connect($host,$user,$pw,$db,$encode = 'UTF8') {
$resource = mysql_connect($host,$user,$pw);
if(is_resource($resource)) {
$this->link = $resource;
if(mysql_select_db($db,$this->link)) {
unset($resource);
if (floatval(mysql_get_server_info($this->link)) > 4.1 && isset($encode)) {
mysql_query('SET NAMES "'.$encode.'"');
}
return true;
} else {
unset($resource);
return false;
}
} else {
unset($resource);
return false;
}
}
/**
* query sql
*
* @param unknown_type $query
* @return unknown
*/
function query($query) {
$result = mysql_query($query,$this->link);
$this->querys ++;
if($result) {
$this->result = $result;
return true;
} else {
return false;
}
}
/**
* fetch a row
*
* @return mixed
*/
function fetch() {
if(is_resource($this->result)) {
return mysql_fetch_array($this->result);
} else {
return false;
}
}
/**
* fetch all result
*
* @return mixed
*/
function fetchAll() {
if(is_resource($this->result)) {
$temp = array();
while ($row = mysql_fetch_array($this->result)) {
$temp[] = $row;
}
return $temp;
} else {
return false;
}
}
/**
* return the querys
*
* @return integer
*/
function getQuerys() {
return $this->querys;
}
/**
* get the numbers of the result
*
* @return int
*/
function getNumberRow() {
if (is_resource($this->result)) {
return mysql_num_rows($this->result);
} else {
return 0;
}
}
/**
* free result
*
* @return boolean
*/
function free() {
if(is_resource($this->result)) {
mysql_free_result($this->result);
return true;
} else {
return false;
}
}
/**
* close mysql connect
*
* @return boolean
*/
function close() {
if(is_resource($this->link)) {
mysql_close($this->link);
return true;
} else {
return false;
}
}
}
?>
php类的使用实例教程
<?php
/**
* Class program for yinghua05-2
* designer :songsong
*/
class Template {
var $tpl_vars;
var $tpl_path;
var $_debug;
/**
* Construct for Template
* PHP5 or upper version
*/
function __construct() {
$this->Template();
}
/**
* Construct for Template
*
* @return Template
*/
function Template() {
$this->tpl_vars = array();
$this->tpl_path = '';
$this->_debug = false;
}
/**
* Set template path
*
* @param string $path
* @return boolean
*/
function setPath($path) {
if(is_dir($path)) {
$path = rtrim($path,'/').'/';
$this->tpl_path = $path;
return true;
} else {
if($this->_debug) {
$this->_debug('template path is not exists.');
}
return false;
}
}
/**
* Enter description here...
*
* @param mixed $var
* @param mixed $val
*/
function assign($var,$val) {
if(isset($var) && is_array($var)) {
$this->tpl_vars = $var;
} else if(isset($var) && $var != '') {
$this->tpl_vars[$var] = $val;
} else {
if($this->_debug == true) {
$this->_debug('set variable error.');
}
return false;
}
}
/**
* Display template file
*
* @param String $file_name
*/
function display($file_name) {
ob_start();
extract($this->tpl_vars);
$include_flie = $this->tpl_path . $file_name;
if(!file_exists($include_flie)) {
if($this->_debug)
$this->_debug('Template file "'.$include_flie.'" is not exists.');
else
exit('Template error, please check it.');
}
include($include_flie);
$content = ob_get_contents();
ob_end_clean();
echo $content;
}
/**
* Debuging
*
*/
function _debug($msg = '') {
die('Error :'.$msg);
}
}
?>
php常用文件上传类
<?php
/**
* flie class
* (jpg,gif,png)
*/
class Upload {
var $_file;
var $_fileType;
var $target = 'upload/';
/**
* construct for this class
*
* @param string $name
* @return Upload
*/
function Upload($name) {
if(isset($_FILES[$name])) {
$this->_file = &$_FILES[$name];
$this->_parseUploadFile();
} else {
die('No file upload.');
}
}
/**
* set upload target path
*
* @param string $path
* @return boolean
*/
function setTarget($path = 'upload/') {
if(is_dir($path)) {
$this->target = rtrim($path,'/').'/';
return true;
} else {
return false;
}
}
/**
* get the type of the file
*
*/
function _parseUploadFile() {
$type = $this->_file['type'];
if(isset($type) && $type != '') {
switch ($type) {
case 'image/gif':
$this->_fileType = 'gif';
break;
case 'image/png':
$this->_fileType = 'png';
break;
case 'image/jpeg':
$this->_fileType = 'jpg';
break;
case 'image/pjpeg':
$this->_fileType = 'jpg';
break;
default:
$this->_fileType = 'unknow';
break;
}
} else {
$filename = $this->_file['name'];
$filename = explode('.',$filename);
$filename = strtoupper($filename[sizeof($filename) - 1]);
switch ($filename) {
case 'PNG':
$this->_fileType = 'png';
break;
case 'JPEG':
$this->_fileType = 'jpg';
break;
case 'JPG':
$this->_fileType = 'jpg';
break;
case 'GIF':
$this->_fileType = 'gif';
break;
default:
$this->_fileType = 'unknow';
break;
}
unset($filename);
}
unset($type);
}
/**
* upload file
*
* @return array
*/
function load() {
if($this->_fileType == 'unknow') {
die('Can not upload this file,because the type is not allow.');
}
if(file_exists($this->_file['tmp_name']) && is_uploaded_file($this->_file['tmp_name'])) {
$new_file_name = $this->target.time().'.'.$this->_fileType;
move_uploaded_file($this->_file['tmp_name'],$new_file_name);
return array('name'=>$new_file_name,'size'=>$this->_file['size'],'type'=>$this->_fileType);
} else {
return false;
}
}
}
?>