$cache = new Cache();
$cache = new Cache(60, '/any_other_path/');
$value = $cache->get('data_key');
$value = $cache->put('data_key', 'data_value');
$cache = new Cache();
//从缓存从读取键值 $key 的数据
$values = $cache->get($key);
//如果没有缓存数据
if ($values == false) {
//insert code here...
//写入键值 $key 的数据
$cache->put($key, $values);
} else {
//insert code here...
}
<?php
/**
* 简单实用的php缓存类
* Edit: www.
*/
class Cache {
private $cache_path;//path for the cache
private $cache_expire;//seconds that the cache expires
//cache constructor, optional expiring time and cache path
public function Cache($exp_time=3600,$path="cache/"){
$this->cache_expire=$exp_time;
$this->cache_path=$path;
}
//returns the filename for the cache
private function fileName($key){
return $this->cache_path.md5($key);
}
//creates new cache files with the given data, $key== name of the cache, data the info/values to store
public function put($key, $data){
$values = serialize($data);
$filename = $this->fileName($key);
$file = fopen($filename, 'w');
if ($file){//able to create the file
fwrite($file, $values);
fclose($file);
}
else return false;
}
//returns cache for the given key
public function get($key){
$filename = $this->fileName($key);
if (!file_exists($filename) || !is_readable($filename)){//can't read the cache
return false;
}
if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired
$file = fopen($filename, "r");// read data file
if ($file){//able to open the file
$data = fread($file, filesize($filename));
fclose($file);
return unserialize($data);//return the values
}
else return false;
}
else return false;//was expired you need to create new
}
}
?>分享一个php上传单个文件到ftp服务器的简单例子。
代码:
<?php
/**
* php 文件上传简单范例
* by www.
*/
// FTP连接参数
$host = 'ftp.xxx.com';
$usr = 'user';
$pwd = 'password';
// 用于上传的文件:
$local_file = './example.txt';
$ftp_path = '/data/example.txt';
// 连接ftp
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
// 发送连接参数
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// turn on passive mode transfers (some servers need this)
// ftp_pasv ($conn_id, true);
// perform file upload
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
// 检测上传状态:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
/*
** Chmod the file (just as example)
*/
// If you are using PHP4 then you need to use this code:
// (because the "ftp_chmod" command is just available in PHP5+)
if (!function_exists('ftp_chmod')) {
function ftp_chmod($ftp_stream, $mode, $filename){
return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
}
}
// try to chmod the new file to 666 (writeable)
if (ftp_chmod($conn_id, 0666, $ftp_path) !== false) {
print $ftp_path . " chmoded successfully to 666\n";
} else {
print "could not chmod $file\n";
}
// close the FTP stream
ftp_close($conn_id);
?>在Linux中安装PHP的GD支持库,步骤如下:
一、下载
jpegsrc.v6b.tar.gz http://www.ijg.org/
libpng-1.2.7.tar.tar http://sourceforge.net/projects/libpng/
zlib-1.2.2.tar.gz http://sourceforge.net/projects/zlib/
freetype-2.1.9.tar.gzhttp://sourceforge.net/projects/freetype/
php-4.3.9.tar.gz http://www.php.net
二、安装
1.安装zlib
cd zlib-1.2.2
./configure
make
make install
2.安装libpng
cd libpng-1.2.7
cd scripts/
mv makefile.linux ../makefile
cd ..
make
make install
注意,这里的makefile不是用./configure生成,而是直接从scripts/里拷一个
3.安装freetype
cd freetype-2.1.9
./configure
make
make install
4.安装Jpeg
cd jpeg-6b/
./configure --enable-shared
make
make test
make install
注意,这里configure一定要带--enable-shared参数,不然,不会生成共享库
5.安装GD
cd gd-2.0.33
./configure --with-png --with-freetype --with-jpeg
make install
上面的安装步骤是没有设定 安装目录的,测试重新编译PHP时用上面方法均可。
若要指定安装地址,请采用如下方式,推荐定义安装目录。
1、安装 zlib
tar -zxf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure –prefix=/usr/local/zlib
make
make install
2、安装 jpeg
mkdir -p /usr/local/jpeg6
mkdir -p /usr/local/jpeg6/bin
mkdir -p /usr/local/jpeg6/lib
mkdir -p /usr/local/jpeg6/include
mkdir -p /usr/local/jpeg6/man
mkdir -p /usr/local/jpeg6/man1
mkdir -p /usr/local/jpeg6/man/man1
tar -zxf jpeg-6b.tar.gz
cd jpeg-6b
./configure –prefix=/usr/local/jpeg6 –enable-shared –enable-static
make
make install
安装完成提示:
Libraries have been installed in:
/usr/local/jpeg6/lib
3、安装 libpng
tar -zxf libpng-1.2.16.tar.gz
cd libpng-1.2.16
./configure –prefix=/usr/local/libpng
make
make install
4、安装 freetype
tar -zxf freetype-2.3.4.tar.gz
cd freetype-2.3.4
mkdir -p /usr/local/freetype
./configure –prefix=/usr/local/freetype
make
make install
5、安装 GD
tar -zxf gd-2.0.33.tar.gz
cd gd-2.0.33
mkdir -p /usr/local/gd2
./configure –prefix=/usr/local/gd2 –with-jpeg=/usr/local/jpeg6/ –with-png=/usr/local/lib/ –with-zlib=/usr/local/lib/ –with-freetype=/usr/local/freetype/
make
make install
安装jpg时,如果出错,需要先装libtool:
libtool-1.X.tar.gz //是我下载的版本
make
make install
再装jpegsrc.v6b.tar.gz
./configure --enable-shared --enable-static 这些lib这种包用默认路径就行。
不然如果其他的需要这个包还得指它的路径
这里可能会出错
checking host system type… Invalid configuration `x86_64-unknown-linux-gnu ‘: machine `x86_64-unknown ‘ not recognized
checking build system type… Invalid configuration `x86_64-unknown-linux-gnu ‘: machine `x86_64-unknown ‘ not recognized
configure: error: libtool configure failed
或者
/libtool --mode=compile gcc -O2 -I. -c ./jcapimin.c
make: ./libtool:命令未找到
make: *** [jcapimin.lo] 错误 127
./libtool --mode=compile gcc -O2 -I. -c ./cjpeg.c
make: ./libtool:命令未找到
make: *** [cjpeg.lo] 错误 127
解决方法
cp /usr/share/libtool/config/config.guess .
cp /usr/share/libtool/config/config.sub .
cp到jpeg的安装文件目录,注意后面的个“.”
make clean
再重新
./configure --prefix=/usr/local/libjpeg/ --enable-shared --enable-static
make
make install
--------------------------------
如果已经安装php,建议通过追加编译安装
进入“[php解压目录]/ext/gd”目录,执行如下命令:
[php安装目录]/bin/phpize
./configure --with-php-config=[php安装目录]/bin/php-config --with-jpeg=[jpeg-6b安装目录] --with-png=[libpng安装目录] --with-freetype=[freetype安装目录] --with-gettext=[gettext安装目录] --with-gd=[gd安装目录]
make
make install
安装成功后会在“[php安装目录]/lib/php/extensions/no-debug-non-zts-20060613”目录下生成gd.so文件,
然后cp [php安装目录]/lib/php/extensions/no-debug-non-zts-20060613/gd.so /opt/php/ext
修改php.ini文件加载gd组件,添加extension_dir=/opt/php/ext和extension=gd.so,如果有extension_dir=/opt/php/ext则不需要增加
extension_dir=/opt/php/ext
extension=gd.so