Archive

Posts Tagged ‘函数’

PHP 读取 HTTP_RANGE 进行HTTP断点续传的函数

2011-07-27 留下评论

<?php

/**

* PHP-HTTP断点续传实现

* @param string $path: 文件所在路径

* @param string $file: 文件名

* @return void

*/

function download($path,$file) {

$real = $path.’/’.$file;

if(!file_exists($real)) {

return false;

}

$size = filesize($real);

$size2 = $size-1;

$range = 0;

if(isset($_SERVER[‘HTTP_RANGE’])) {

header(‘HTTP /1.1 206 Partial Content’);

$range = str_replace(‘=’,’-‘,$_SERVER[‘HTTP_RANGE’]);

$range = explode(‘-‘,$range);

$range = trim($range[1]);

header(‘Content-Length:’.$size);

header(‘Content-Range: bytes ‘.$range.’-‘.$size2.’/’.$size);

} else {

header(‘Content-Length:’.$size);

header(‘Content-Range: bytes 0-‘.$size2.’/’.$size);

}

header(‘Accenpt-Ranges: bytes’);

header(‘application/octet-stream’);

header(“Cache-control: public”);

header(“Pragma: public”);

//解决在IE中下载时中文乱码问题

$ua = $_SERVER[‘HTTP_USER_AGENT’];

if(preg_match(‘/MSIE/’,$ua)) {

$ie_filename = str_replace(‘+’,’%20′,urlencode($file));

header(‘Content-Dispositon:attachment; filename=’.$ie_filename);

} else {

header(‘Content-Dispositon:attachment; filename=’.$file);

}

$fp = fopen($real,’rb+’);

fseek($fp,$range);

while(!feof($fp)) {
set_time_limit(0);

print(fread($fp,1024));

flush();

ob_flush();

}

fclose($fp);

}

/*End of PHP*/

分类:PHP 标签:, , ,

php实现RC4加密算法的函数

2011-07-15 留下评论

/*
* rc4加密算法
* 解密方法:重新加密一次,便可还原。。
* $pwd 密钥
* $data 要加密的数据
*/
function rc4 ($pwd, $data)//$pwd密钥 $data需加密字符串
{
$key[] =””;
$box[] =””;

$pwd_length = strlen($pwd);
$data_length = strlen($data);

for ($i = 0; $i < 256; $i++)
{
$key[$i] = ord($pwd[$i % $pwd_length]);
$box[$i] = $i;
}

for ($j = $i = 0; $i < 256; $i++)
{
$j = ($j + $box[$i] + $key[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}

for ($a = $j = $i = 0; $i < $data_length; $i++)
{
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;

$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;

$k = $box[(($box[$a] + $box[$j]) % 256)];
$cipher .= chr(ord($data[$i]) ^ $k);
}

return $cipher;
}

分类:PHP 标签:, , ,

PHP使用正则替换全角字符的函数

2011-07-15 留下评论

function zo_replace(text) {
return text && text.replace(/[\u3000\uff01-\uff5f]/g, function($0) {
return $0 == “\u3000″ ? ” ” : String.fromCharCode($0.charCodeAt(0) – 0xfee0);
});
}

分类:PHP 标签:, , ,

PHP使用正则替换全角字符的函数

2011-07-15 留下评论

function zo_replace(text) {
return text && text.replace(/[\u3000\uff01-\uff5f]/g, function($0) {
return $0 == “\u3000″ ? ” ” : String.fromCharCode($0.charCodeAt(0) – 0xfee0);
});
}

分类:PHP 标签:, , ,

php文件下载的函数

2011-07-15 留下评论

public function downloads($name){
$name_tmp = explode(“_”,$name);
$type = $name_tmp[0];
$file_time = explode(“.”,$name_tmp[3]);
$file_time = $file_time[0];
$file_date = date(“Y/md”,$file_time);
$file_dir = SITE_PATH.”/data/uploads/$type/$file_date/”;

if (!file_exists($file_dir.$name)){
header(“Content-type: text/html; charset=utf-8”);
echo “File not found!”;
exit;
} else {
$file = fopen($file_dir.$name,”r”);
Header(“Content-type: application/octet-stream”);
Header(“Accept-Ranges: bytes”);
Header(“Accept-Length: “.filesize($file_dir . $name));
Header(“Content-Disposition: attachment; filename=”.$name);
echo fread($file, filesize($file_dir.$name));
fclose($file);
}
}

分类:PHP 标签:, ,

php读取csv的函数

2011-07-12 留下评论

[cc lang=”php”]

function read_csv($cvs) {

$shuang = false;

$str = file_get_contents($cvs);

for($i=0;$i<strlen($str);$i++) {

if($str{$i}=='”‘) {

if($shuang) {

if($str{$i+1}=='”‘) {

$str{$i} = ‘*’;

$str{$i+1} = ‘*’;

} else {

$shuang = false;

}

} else {

$shuang = true;

}

}

if($str{$i}==’,’) {

if($shuang) {

} else {

$str{$i} = ‘|’;

}

}

if($str{$i}==”\n”) {

if($shuang) {

$str{$i} = ‘^’;

} else {

}

}

}

$str = str_replace(array(‘”‘,’*’),array(”,'”‘),$str);

$a1 = explode(“\n”,$str);

$array = array();

foreach($a1 as $k=>$value) {

if($value) {

$value = str_replace(“^”,”\n”,$value);

$array[$k] = explode(“|”,$value);

}

}

return $array;

}
[/cc]