2010-06-07

PHP解压tar,gzip,bzip脚本

类归于: symfony — 标签:, , , szn000 @ 14:37

根据网上的一个 解压类写的 关于 tar gzip bzip 的解压脚本(php)
该脚本附加了 解压后权限的设定 (只能设定属于HTTP的文件和文件夹)

下载该解压脚本

该脚本包含 3个文件
index.php 用来处理请求(是修改权限还是解压文件)
lib/utile.class.php 修改权限所需要用到的类
lib/archive.class.php 解压文件所有用到的类(来源于网络 有部分修改)

2010-05-26

使用SYMFONY Filter 过滤URL

类归于: symfony — 标签:, , szn000 @ 09:30

这是一个过滤URL中 多个’/'的例子
修改 apps/myapp/config/filters.yml
添加代码
myFilter:
class: myFilter

(myFilter可自定义)
创建文件myFilter.class.php
创建位置apps/myapp/lib/
文件内容如下
使用preg_match 是因为 ereg 过时
注意preg_match 匹配正则时以’/'开始和结束
class myFilter extends sfFilter
{
public function execute ($filterChain)
{
//提取uri
$str = $_SERVER['REQUEST_URI'];
//判断是否匹配正则
if(preg_match("/(.*\/{2,}.*)((\.html)*)/",$str)){
//要跳转的页面
header("Location:/error.html");
//跳转后代码不执行
exit();
}
//执行下一个filter
$filterChain->execute();
}
}

修改apps/myapp/config/routing.yml
添加代码 此代码用来定义URL /error.html 要执行的模块和动作
Error:
url: /error.html
param: { module: index, action: error404}

2010-05-25

仿symfony机制实现不用require或者include来实例化类

类归于: PHP, symfony — 标签:, kthiz2006 @ 10:35


class MyClass {
public static function register() {
//如果解串行器发现有未定义类要被实例化,则设置spl_autoload_call函数加载请求类.
ini_set('unserialize_callback_func', 'spl_autoload_call');
//自动载入类,类似于__autoload
if (false === spl_autoload_register(array("MyClass","autoLoad"))) {
throw new Exception('wrong');
}
}
//处理自动载入类
public static function autoload($className) {
require realpath(dirname(__FILE__)).'/lib/'.strtolower($className).'.class.php';
}
}

只要在项目目录中创建一个类的目录,这里我创建的叫lib,往这里放入你要放的类的文件。
注:
1.类的文件名要统一规则,这样方便系统快速地进行查找,避免消耗更多的内存。
2.autoload的函数的载入类的规则可以依据需要进行设置,我这里规定要载入我的lib目录里文件的扩展名是class.php的类的文件。

以下是使用的例子:
在创建的lib目录里方了一个叫cache的类的文件。
接着在你要编写的程序文件里载入上边的说的自动载入类的文件,使用MyClass::register(),就可以不用载入要实例化的类的文件直接实例化刚才的cache的类的文件。

以上说的实现的效果必须是php的版本>=5.1.2

2010-05-24

在Emacs里使用 PHPCS

类归于: symfonybobhero @ 13:36

前文书说的好 MAKER 可以在VIM 里使用PHPCS ,VIM是很牛B 的编辑器,但是还有一个更牛B 的编辑器就是 EMACS。

具体有多牛,去GOOGLE 百度一下

PHPCS是什么 ,请参考前文

这里贴上 EMACS 下使用的代码


(defun php-lint ()
(interactive)
(let ((compilation-error-regexp-alist '(php))
(compilation-error-regexp-alist-alist ()))
(pushnew '(php "\\(syntax error.*\\) in \\(.*\\) on line \\([0-9]+\\)$" 2 3 nil nil 1)
compilation-error-regexp-alist-alist)
(compile (concat "php -l -f \"" (buffer-file-name) "\""))))
(define-key php-mode-map (kbd " ") 'php-lint)
(defun phpcs ()
"Performs a PHP code sniffer check on the current file."
(interactive)
(let ((compilation-error-regexp-alist '(gnu)))
;; (compile (format "phpcs --standard=PEAR --report=emacs \"%s\""
(compile (format "phpcs --standard=Zend --report=emacs \"%s\""
(buffer-file-name)))))
;; Check code style
(define-key php-mode-map (kbd " ") 'phpcs)

完成以后可以用 快键调用也可以用 命令行调用。
什么 你看不懂? 没有关系 ,看不懂就看不懂吧,早晚能看懂的

2010-05-21

symfony1.4 DateTime对于PHP低版本的兼容问题

类归于: symfony — 标签:, szn000 @ 13:17

在symfony1.4中使用DateTime 类 但是该类出现于 PHP 5.2.0以后 所以在使用(部分方法需要在PHP5.3.0以后版本才能使用)PHP5.2.0以前版本的服务器上 调用symfony调用该函数会出错 为了解决该类问题根据 PHP5.2.0 的DateTime类 写了俩个类 用以在低版本中使用该类
以重写方法
DateTime.class.php
__construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
string format ( string $format )
int getOffset ( void )
int getTimestamp ( void )
DateTimeZone getTimezone ( void )
DateTime setDate ( int $year , int $month , int $day )
DateTime setISODate ( int $year , int $week [, int $day = 1 ] )
DateTime setTime ( int $hour , int $minute [, int $second = 0 ] )
DateTime setTimestamp ( int $unixtimestamp )
DateTime setTimezone ( DateTimeZone $timezone )
DateTime __wakeup ( void )

DateTimeZone.class.php
__construct ( string $timezone )
string getName ( void )
int getOffset ( DateTime $datetime )

文件下载地址: 下载点击我
附件使用时把类名中的My删除

早前文章 »

WordPress 所驱动