define ("tblPath",".");
define ("exten",".php");
define ("fileHead","<? echo "You are wellcome!"?".">This file only for class txtTbl");
class txtTbl {
var $innerName=""; //php名称
var $innerCount; //数据库记录数目
var $innerFields; //数据库字段列表数组
var $inner_F_Count; //数据库字段数目
var $fullName; //完整的文件名
var $isModify = false; //当前记录是否被修改
var $fileModify = false; //数据库是否被修改
var $innerRecorders; //数据库记录数组
var $curLine; //当前记录号
var $curArray; //当前行数组
var $stringDel; //保存被删除记录
var $sprt1; //数据库记录间的分隔符
var $sprt2; //数据库字段间的分隔符
var $innerBof = true;
var $innerEof = false;
php实现多线程
php实现多线程
服务器发送多个请求要实现多进程要方便很多。只能使用在cli模式。可以用在特殊场合,如邮件发送任务等。
资源的共享访问使用了文件锁,并不是很可靠,主要是为了能够在Windwos下使用,如果确实有必要可以考虑自己改用相应的信号灯机制(这个扩展只能用于xUNIX)。
php 导入csv数据到mysql数据库
class Import{
var $csv_fields=array(); //fields in csv to care about...
var $csv_file; //file to open
var $csv_data; //data in file
var $csv_array = array(); //array of data in file
var $csv_all_fields = array(); //all the field names in the csv file
var $csv_data_all = array(); //all the data
var $csv_data_keyed = array(); //all the data
var $tmp_array=array();
var $mysql_array=array();
var $table; //table to insert into
var $mysql_link; //database
var $instructions=array(); //sql and default settings
var $instruct_functions = array(); //function calls
var $debug=0; // show debug information - step by step messages
var $return_debug=0; // return debug information instead of echoing it
var $vals_per_insert=100; //number of values to add per insert statement of SQL
var $format="linux";
var $linebreak_char="n";
var $delimit_char = ",";
var $use_external_csv_header_file = 0;
var $external_headers="";
function Import(){
echo "n";
return(TRUE);
}
php 导入数据到excel
class CsvFieldDump {
var $headers;
var $fieldnum;
function CsvFieldDump($infile){
if(empty($infile)){
die("You must specify a csv file to readn");
}
if(!file_exists($infile)){
die("$infile doesnt exist!n");
}
$this->infile = $infile;
$this->fieldnum = $field;
$this->createFile=0;
return(true);
}
php 模仿ftp文件上传实例
$mymode = FTP_ASCII;
$delete = 0;
$local_dir = "/my_local_download_directory";
$host = "ftp.yahoo.de";
$remote_dir = ".";
$anonymous = 0;
$user = "myusername";
进程锁定问题分析研究
<?php
/**
* 进行写锁定的测试
* 打开线程1
*/
require("file_lock.php");
$lock = new File_Lock(dirname(dirname(__FILE__)) . "/FileLock.lock");
/** 单个线程锁定的速度 1s 钟 3万次。 **/
/** 两个线程写,两万的数据 大概要 7s 钟*/
/** 一个线程写,一万的数据 大概要 3.9s 钟,居然两个文件同时写,要快一点*/
/** 不进行锁定,一个进程 写大概要 2.8s 钟,加锁是有代价的。 */
/** 不进行锁定,两个进程 分布不是很均匀,而且大多数都冲突 */
$lock->writeLock();
$lock->increment();
$lock->unlock();
while ($lock->get() < 2) {
usleep(1000);
}
sleep(1);
echo "begin to runing n";
$t1 = microtime(true);
for ($i = 0; $i < 10000; $i++)
{
$lock->writeLock();
$lock->increment(1);
$lock->unlock();
}
$t2 = microtime(true) - $t1;
echo $t2;
?>
Memcache 中实现消息队列
class Memcache_Queue
{
private $memcache;
private $name;
private $prefix;
function __construct($maxSize, $name, $memcache, $prefix = "__memcache_queue__")
{
if ($memcache == null) {
throw new Exception("memcache object is null, new the object first.");
}
$this->memcache = $memcache;
$this->name = $name;
$this->prefix = $prefix;
$this->maxSize = $maxSize;
$this->front = 0;
$this->real = 0;
$this->size = 0;
}
function __get($name)
{
return $this->get($name);
}
function __set($name, $value)
{
$this->add($name, $value);
return $this;
}
function isEmpty()
{
return $this->size == 0;
}
function isFull()
{
return $this->size == $this->maxSize;
}
function enQueue($data)
{
if ($this->isFull()) {
throw new Exception("Queue is Full");
}
$this->increment("size");
$this->set($this->real, $data);
$this->set("real", ($this->real + 1) % $this->maxSize);
return $this;
}
function deQueue()
{
if ($this->isEmpty()) {
throw new Exception("Queue is Empty");
}
$this->decrement("size");
$this->delete($this->front);
$this->set("front", ($this->front + 1) % $this->maxSize);
return $this;
}
function getTop()
{
return $this->get($this->front);
}
function getAll()
{
return $this->getPage();
}
function getPage($offset = 0, $limit = 0)
{
if ($this->isEmpty() || $this->size < $offset) {
return null;
}
$keys[] = $this->getKeyByPos(($this->front + $offset) % $this->maxSize);
$num = 1;
for ($pos = ($this->front + $offset + 1) % $this->maxSize; $pos != $this->real; $pos = ($pos + 1) % $this->maxSize)
{
$keys[] = $this->getKeyByPos($pos);
$num++;
if ($limit > 0 && $limit == $num) {
break;
}
}
return array_values($this->memcache->get($keys));
}
function makeEmpty()
{
$keys = $this->getAllKeys();
foreach ($keys as $value) {
$this->delete($value);
}
$this->delete("real");
$this->delete("front");
$this->delete("size");
$this->delete("maxSize");
}
private function getAllKeys()
{
if ($this->isEmpty())
{
return array();
}
$keys[] = $this->getKeyByPos($this->front);
for ($pos = ($this->front + 1) % $this->maxSize; $pos != $this->real; $pos = ($pos + 1) % $this->maxSize)
{
$keys[] = $this->getKeyByPos($pos);
}
return $keys;
}
private function add($pos, $data)
{
$this->memcache->add($this->getKeyByPos($pos), $data);
return $this;
}
private function increment($pos)
{
return $this->memcache->increment($this->getKeyByPos($pos));
}
private function decrement($pos)
{
$this->memcache->decrement($this->getKeyByPos($pos));
}
private function set($pos, $data)
{
$this->memcache->set($this->getKeyByPos($pos), $data);
return $this;
}
private function get($pos)
{
return $this->memcache->get($this->getKeyByPos($pos));
}
private function delete($pos)
{
return $this->memcache->delete($this->getKeyByPos($pos));
}
private function getKeyByPos($pos)
{
return $this->prefix . $this->name . $pos;
}
}
php array_push 向数组增加值函数
php array_push 向数组增加值函数
public static function insert(&$array, $key, $newValue, $before = true) {
$result = false;
$size = sizeof($array);
for ($i=0; $i<$size; $i++) {
$value = array_shift($array);
if ($i==$key) {
if ($before) {
array_push($array, $newValue);
array_push($array, $value);
} else {
array_push($array, $value);
array_push($array, $newValue);
}
$result = true;
} else {
array_push($array, $value);
}
}
if (!$result) {
array_push($array, $newValue);
}
return;
}
php日期所在月的天数
php日期所在月的天数
public function daysOfMonth ($year=NULL,$month=NULL) {
if ($year===NULL) {
$year = $this->getPart(yy);
}
if ($month===NULL) {
$month = $this->getPart(mm);
}
if ($month==2)
{
if (($year % 4 == 0 && $year % 100 != 0) || $year %
php 缓存文件入门程序
class PageCache {
/**
* @var string $file 缓存文件地址
* @access public
*/
public $file;
/**
* @var int $cacheTime 缓存时间
* @access public
*/
public $cacheTime = 3600;
/**
* 构造函数
* @param string $file 缓存文件地址
* @param int $cacheTime 缓存时间
*/
function __construct($file, $cacheTime = 3600) {
$this->file = $file;
$this->cacheTime = $cacheTime;
}
/**
* 取缓存内容
* @param bool 是否直接输出,true直接转到缓存页,false返回缓存内容
* @return mixed
*/
public function get($output = true) {
if (is_file($this->file) && (time()-filemtime($this->file))<=$this->cacheTime && !$_GET[nocache]) {
if ($output) {
header(location: . $this->file);
exit;
} else {
return file_get_contents($this->file);
}
} else {
return false;
}
}
/**
* 设置缓存内容
* @param $content 内容html字符串
*/
public function set($content) {
$fp = fopen($this->file, w);
fwrite($fp, $content);
fclose($fp);
}
}
php 日期转换成日时截
php 日期转换成日时截
private function toTimeStamp ($dateTimeString = NULL) {
if (!$dateTimeString) {
$dateTimeString = time();
}
$numeric = ;
$add_space = false;
for($i=0;$i<strlen($dateTimeString);$i++) {
if(strpos(0123456789,$dateTimeString[$i])===false) {
if($add_space) {
$numeric .= ;
$add_space = false;
}
} else {
$numeric .= $dateTimeString[$i];
$add_space = true;
}
}
$numeric_array = explode( ,$numeric,6);
if(sizeof($numeric_array)<3 || ($numeric_array[0]==0 && $numeric_array[1]==0 && $numeric_array[2]==0)) {
throw new Exception($dateTimeString . is an invalid parameter, 5);
} else {
$result = mktime(intval($numeric_array[3]), intval($numeric_array[4]), intval($numeric_array[5]),
intval($numeric_array[1]), intval($numeric_array[2]), intval($numeric_array[0])) ;
}
return $result;
}
php google 风格分页代码
php google 风格分页代码
public function showCtrlPanel_g($halfPer = 5) {
$re = <div class="pageMore">
<ul>
<li><span>.$this->lineCount.条</span></li>
<li><span>.$this->currentPage./.$this->pageCount.页</span></li>;
if($this->currentPage-$halfPer >1){
$re .= <li><a href=".$this->fileName.pageno=1"><span>1</span></a></li>;
if($this->currentPage-$halfPer*2 >1){
$re .= <li><a href=".$this->fileName.pageno=.($this->currentPage-$halfPer*2)."><span>...</span></a></li>;
}else{
$re .= <li><a href=".$this->fileName.pageno=1"><span>...</span></a></li>;
}
}
for ( $i = $this->currentPage - $halfPer,$i > 1 || $i = 1 , $j = $this->currentPage + $halfPer, $j < $this->pageCount || $j = $this->pageCount;$i <= $j ;$i++ )
{
$re .= $i == $this->currentPage
? <li class="linkOn"><a href=".$this->fileName.pageno=.$i."><span>.$i.</span></a></li>." "
: <li><a href=".$this->fileName.pageno=.$i."><span>.$i.</span></a></li>." ";
}
if($this->currentPage+$halfPer < $this->pageCount){
if($this->currentPage+$halfPer*2 < $this->pageCount){
$re .= <li><a href=".$this->fileName.pageno=.($this->currentPage+$halfPer*2)."><span>...</span></a></li>;
}else{
$re .= <li><a href=".$this->fileName.pageno=.$this->pageCount."><span>...</span></a></li>;
}
$re .= <li><a href=".$this->fileName.pageno=.$this->pageCount."><span>.$this->pageCount.</span></a></li>;
}
$re .=
</ul>
</div>;
return $re;
}