function xiazai($file_dir,$file_name)
//参数说明:
//file_dir:文件所在目录
//file_name:文件名
{
$file_dir = chop($file_dir);//去掉路径中多余的空格
//得出要下载的文件的路径
if($file_dir != '')
{
$file_path = $file_dir;
if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
$file_path .= '/';
$file_path .= $file_name;
}
else
$file_path = $file_name;
//判断要下载的文件是否存在
if(!file_exists($file_path))
{
alert('对不起,你要下载的文件不存在');
return false;
}
$file_size = filesize($file_path);
header("Content-type: application/octet-stream;charset=gbk");
header("Accept-Ranges: bytes");
header("Accept-Length: $file_size");
header("Content-Disposition: attachment; filename=".$file_name);
$fp = fopen($file_path,"r");
$buffer_size = 1024;
$cur_pos = 0;
while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
{
$buffer = fread($fp,$buffer_size);
echo $buffer;
$cur_pos += $buffer_size;
}
$buffer = fread($fp,$file_size-$cur_pos);
echo $buffer;
fclose($fp);
return true;
}
我们这个做法是把搜索结果出来,与搜索关键词相同的替换成高亮的字, 我们会用到str_replace(你找的关键字,<带有高亮的html标签>你找的关键字带有高亮的html标签>,$str); 就这么容易了,好了下面我们来看一个实例吧。
先创建一个数据库教程 create database 'searchKey';
再创建表
CREATE TABLE `fangke_cc`.`search` (
`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,
`keyword` VARCHAR( 20 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM
我们导入一些数据
INSERT INTO `search` (`id`, `keyword`) VALUES
(1, '中国WEB第一站www.111cn.net'),
(2, '中国WEB第一站www.111cn.net'),
(3, '中国WEB第一站www.111cn.net'),
(4, '中国WEB第一站www.111cn.net');
好了下面我们就来执行查询操作
*/
if( $_POST) {
$db ='fangke_cc';
mysql教程_pconnect('localhost','root','root') or die(mysql_error());
mysql_select_db($db);
mysql_query("set names 'gbk'");
$key = $_POST['keyword'];
$sql = "Select * from search where keyword like '%$key%'";
$query = mysql_query($sql);
while( $rs = mysql_fetch_array( $query ) )
{
echo str_replace($key,"<b>$key</b>",$rs['keyword']),'<br />';
}
/*
中国WEB第一站www.<b>111cn</b>.net<br />
中国WEB第一站www.<b>111cn</b>.net<br />
中国WEB第一站www.<b>111cn</b>.net<br />
中国WEB第一站
*/
}
?>
<form name="form1" method="post" action="">
<label>
<input name="keyword" type="text" id="keyword">
</label>
<label>
<input type="submit" name="Submit" value="提交">
</label>
</form>
注明:本文章原创于www.111cn.net 转载注明出处
<?php教程
function vcurl($url, $post = '', $cookie = '', $cookiejar = '', $referer = ''){
$tmpInfo = '';
$cookiepath = getcwd().'./'.$cookiejar;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
if($referer) {
curl_setopt($curl, CURLOPT_REFERER, $referer);
} else {
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
}
if($post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
if($cookie) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
if($cookiejar) {
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiepath);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiepath);
}
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 100);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($curl);
if (curl_errno($curl)) {
echo '<pre><b>错误:</b><br />'.curl_error($curl);
}
curl_close($curl);
return $tmpInfo;
}
?>
header("Content-type: image/jpeg");
$filename = 'book_rabbit_rule.jpg';
/* 读取图档 */
$im = imagecreatefromjpeg($filename);
/* 图片要截多少, 长/宽 */
$new_img_width = 120;
$new_img_height = 42;
/* 先建立一个 新的空白图档 */
$newim = imagecreate($new_img_width, $new_img_height);
// 输出图要从哪边开始 x, y ,原始图要从哪边开始 x, y ,要画多大 x, y(resize) , 要抓多大 x, y
imagecopyresampled($newim, $im, 0, 0, 7, 174, 120, 42, $new_img_width, $new_img_height);
/* 放大成 500 x 500 的图 */
// imagecopyresampled($newim, $im, 0, 0, 100, 30, 500, 500, $new_img_width, $new_img_height);
/* 将图印出来 */
imagejpeg($newim);
/* 资源回收 */
imagedestroy($newim);
imagedestroy($im);
?>
档案内大概该有的注解都写好了(Source code 放在此篇最下面),解释一下主要参数即可:
imagecopyresampled($newim, $im, 0, 0, 7, 174, 120, 42, $new_img_width, $new_img_height); // 原始尺寸 120 x 42
imagecopyresampled($newim, $im, 0, 0, 100, 30, 500, 500, $new_img_width, $new_img_height); // 图截出来后放到 500 x 500
imagecopyresampled($newim, $im, 0, 0, 100, 30, 10, 10, $new_img_width, $new_img_height); // 图截出来后缩小到 10 x 10
大概主要是这个 function 的操作而已,主要只看第一个原尺寸的那个参数对照就好了,下述是 imagecopyresampled 的参数,一个一个对照如下:
$newim: imagecreate($new_img_width, $new_img_height); 建立的空白图层
$im: imagecreatefromjpeg($filename); 把原本的图片读进来
0: 输出图要从哪边开始的 x 点(图片输出的点是可以指定的,没指定到的位置就会是你 create 的图层颜色或图)
0: 输出图要从哪边开始的 y 点(同上)
7: 原始图要从哪边开始 x (指定要从哪个 x 点开始截取)
174: 原始图要从哪边开始 y (指定要从哪个点开始截取,截取区块大小由下面两个参数去定)
120: 截图要截多大 width ,从上面的指定的位置,开始截取 120px 宽(如果要放大缩小也是由此处决定)
42: 截图要截多大 height ,从上面的指定的位置,开始截取 42px 高(如果要放大缩小也是由此处决定)
$new_img_width: 这是新的图,从上面写的第二组参数 0, 0 ,要开始画多宽(在此程式目前是指定截图的大小)
$new_img_height: 这是新的图,从上面的第二组参数 0, 0 ,要开始画多高(在此程式目前是指定截图的大小)
<?php教程
002 /**
003 * <SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%CE%C4%BC%FE">文件</SPAN>传输,支持断点续传。
004 * 2g以上超大文件也有效
005 * @author MoXie
006 */
007 class Transfer {
008 /**
009 * 缓冲单元
010 */
011 const BUFF_SIZE = 5120; // 1024 * 5
012 /**
013 * 文件地址
014 * @var <String>
015 */
016 private $filePath;
017 /**
018 * 文件大小
019 * @var <String> Php超大数字 <SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%D7%D6%B7%FB">字符</SPAN>串形式描述
020 */
021 private $fileSize;
022 /**
023 * 文件类型
024 * @var <String>
025 */
026 private $mimeType;
027 /**
028 * 请求区域(范围)
029 * @var <String>
030 */
031 private $range;
032 /**
033 * 是否写入日志
034 * @var <Boolean>
035 */
036 private $isLog = false;
037 /**
038 *
039 * @param <String> $filePath 文件路径
040 * @param <String> $mimeType 文件类型
041 * @param <String> $range 请求区域(范围)
042 */
043 function __construct($filePath, $mimeType = null , $range = null) {
044 $this->filePath = $filePath;
045 $this->fileSize = sprintf('%u',filesize($filePath));
046 $this->mimeType = ($mimeType != null)?$mimeType:"application/octet-stream"; // bin
047 $this->range = trim($range);
048 }
049 /**
050 * 获取文件区域
051 * @return <Map> {'start':long,'end':long} or null
052 */
053 private function getRange() {
054 /**
055 * Range: bytes=-128
056 * Range: bytes=-128
057 * Range: bytes=28-175,382-399,510-541,644-744,977-980
058 * Range: bytes=28-175n380
059 * type 1
060 * RANGE: bytes=1000-9999
061 * RANGE: bytes=2000-9999
062 * type 2
063 * RANGE: bytes=1000-1999
064 * RANGE: bytes=2000-2999
065 * RANGE: bytes=3000-3999
066 */
067 if (!empty($this->range)) {
068 $range = preg_replace('/[s|,].*/','',$this->range);
069 $range = explode('-',substr($range,6));
070 if (count($range) < 2 ) {
071 $range[1] = $this->fileSize; // Range: bytes=-100
072 }
073 $range = array_combine(array('start','end'),$range);
074 if (empty($range['start'])) {
075 $range['start'] = 0;
076 }
077 if (!isset ($range['end']) || empty($range['end'])) {
078 $range['end'] = $this->fileSize;
079 }
080 return $range;
081 }
082 return null;
083 }
084 /**
085 * 向客户端发送文件
086 */
087 public function send() {
088 $fileHande = fopen($this->filePath, 'rb');
089 if ($fileHande) {
090 // setting
091 ob_end_clean();// clean cache
092 ob_start();
093 ini_set('output_buffering', 'Off');
094 ini_set('zlib.output_compression', 'Off');
095 $magicQuotes = get_magic_quotes_gpc();
096 set_magic_quotes_runtime(0);
097 // init
098 $lastModified = gmdate('D, d M Y H:i:s', filemtime($this->filePath)).' GMT';
099 $etag = sprintf('w/"%s:%s"',md5($lastModified),$this->fileSize);
100 $ranges = $this->getRange();
101 // headers
102 header(sprintf('Last-Modified: %s',$lastModified));
103 header(sprintf('ETag: %s',$etag));
104 header(sprintf('Content-Type: %s',$this->mimeType));
105 $disposition = 'attachment';
106 if (strpos($this->mimeType,'image/') !== FALSE) {
107 $disposition = 'inline';
108 }
109 header(sprintf('Content-Disposition: %s; filename="%s"',$disposition,basename($this->filePath)));
110
111 if ($ranges != null) {
112 if ($this->isLog) {
113 $this->log(json_encode($ranges).' '.$_SERVER['HTTP_RANGE']);
114 }
115 header('HTTP/1.1 206 Partial Content');
116 header('Accept-Ranges: bytes');
117 header(sprintf('Content-Length: %u',$ranges['end'] - $ranges['start']));
118 header(sprintf('Content-Range: bytes %s-%s/%s', $ranges['start'], $ranges['end'],$this->fileSize));
119 //
120 fseek($fileHande, sprintf('%u',$ranges['start']));
121 }else {
122 header("HTTP/1.1 200 OK");
123 header(sprintf('Content-Length: %s',$this->fileSize));
124 }
125 // read file
126 $lastSize = 0;
127 while(!feof($fileHande) && !connection_aborted()) {
128 $lastSize = sprintf("%u", bcsub($this->fileSize,sprintf("%u",ftell($fileHande))));
129 if (bccomp($lastSize,self::BUFF_SIZE) > 0) {
130 $lastSize = self::BUFF_SIZE;
131 }
132 echo fread($fileHande, $lastSize);
133 flush();
134 ob_flush();
135 }
136 set_magic_quotes_runtime($magicQuotes);
137 ob_end_flush();
138 }
139 if ($fileHande != null) {
140 fclose($fileHande);
141 }
142 }
143 /**
144 * 设置记录
145 * @param <Boolean> $isLog 是否记录
146 */
147 public function setIsLog($isLog = true) {
148 $this->isLog = $isLog;
149 }
150 /**
151 * 记录
152 * @param <String> $msg 记录信息
153 */
154 private function log($msg) {
155 try {
156 $handle = fopen('transfer_log.txt', 'a');
157 fwrite($handle, sprintf('%s : %s'.<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=PHP">PHP</SPAN>_EOL,date('Y-m-d H:i:s'),$msg));
158 fclose($handle);
159 }catch(Exception $e) {
160 // null;
161 }
162 }
163 }
164 date_default_timezone_set('Asia/Shanghai');
165 error_reporting(E_STRICT);
166 function errorHandler($errno, $errstr, $errfile, $errline) {
167 echo '<p>error:',$errstr,'</p>';
168 exit();
169 }
170 set_error_handler('errorHandler');
171 define('IS_DEBUG',true);
172
173 //
174 //
175 $filePath = '/Movie/The.Hurt.Locker.2008.x264.AC3-WAF.mkv';
176 $mimeType = 'audio/x-matroska';
177 $range = isset($_SERVER['HTTP_RANGE'])?$_SERVER['HTTP_RANGE']:null;
178 if (IS_DEBUG) {
179 // $range = "bytes=1000-1999n2000";
180 // $range = "bytes=1000-1999,2000";
181 // $range = "bytes=1000-1999,-2000";
182 // $range = "bytes=1000-1999,2000-2999";
183 }
184 set_time_limit(0);
185 $transfer = new Transfer($filePath,$mimeType,$range);
186 if (IS_DEBUG) {
187 $transfer->setIsLog(true);
188 }
189 $transfer->send();
190 ?>