阳光

08月 13th, 2008

阳光

Fckeditor 修改上传路径以及上传自动更改名称

08月 5th, 2008

测试版本:Fckeditor 2.6.3 beta

1.一般情况下,fckeditor上传文件是指定目录(我们这里假设上传到uploads目录中),但是在实际开发中,可能程序会运行在二级目录,http://www.domain.com/app/ 下,在一级目录文件以及目录不能增加或修改的情况下,那我们又得去修改fckeditor里的config.php文件设置上传文件夹,这在很多开源程序中意义重大,拿一个程序来说,我上传的文件永远会在这个目录里中的 uploads 文件夹中。不管我们把这个程序移动到任何子目录中都不需要修改,当然如果你要把uploads文件夹改成其他名字,也很方便。代码如下:

1
2
3
4
5
6
$tmp_workroot = '../../../../../../'; //设置此虚拟目录根路径
$tmp_path = realpath($tmp_workroot).'\uploads\\'; //得到绝对路径和合并上传目录
 
//$tmp_abs_path = str_replace('\\','\\\\',$tmp_path); //得到fckeditor接受的绝对路径地址,如果要使用绝对路径上传,程序运行在windows操作系统要用到此代码
$tmp_siteroot = str_replace('/','\\',$_SERVER['DOCUMENT_ROOT']);	//得到根目录并转换
$tmp_relative_path = str_replace('\\','/',str_replace($tmp_siteroot,'',$tmp_path)); //得到相对上传路径

把上面这段代码添加到 fckeditor\editor\filemanager\connectors\php\config.php 顶部,然后把
$Config['UserFilesPath'] = ‘/uploads/’ ;
改成:$Config['UserFilesPath'] = $tmp_relative_path ;
工作就完成了

2.自动更名
打开fckeditor\editor\filemanager\connectors\php\commands.php 文件
修改$sFileName = time().mt_rand().’.’.$sExtension; //这是我的文件名,格式是:unix时间戳+随机数+扩展名
你可以展开你天马行空的想象力去命令,例如按年、月、日目录分类

乌云

08月 4th, 2008

乌云

把Fckeditor整合到Zend Framework View_Helper里

08月 4th, 2008

看到过把fckeditor放进Controller和直接放在模板里的,虽然能成功运行,但总感觉都和Zend Framework规范有点不太相符,个人觉得放在视图助手里科学一点。

Zend Framework v1.52
Fckeditor v2.6.3 beta

其中fckeditor放在public/js/fckeditor目录下

代码如下:

[-]View Code LANGUAGE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
< ?php
/**************************
@Author  : evila
@Update  :
@Content : Fckeditor 视图辅助类
**************************/
 
class Zend_View_Helper_Fckeditor
{
 function fckeditor($InstanceName='',$value='',$width=800,$height=500,$toolbar='Default')
 {
  require_once 'js/fckeditor/fckeditor.php'
  $iFCKeditor = new FCKeditor($InstanceName)
  $iFCKeditor-&gtBasePath = './js/fckeditor/'
  $iFCKeditor-&gtWidth  = "$width"
  $iFCKeditor-&gtHeight = "$height"
        $iFCKeditor-&gtValue = "$value"
  $iFCKeditor-&gtToolbarSet = "$toolbar"
 
  return $iFCKeditor-&gtCreateHtml()
 }
}

在模板文件里只需 <?php echo $this->fckeditor(’InstanceName’,'test_text’);?> 调用即可(参数自行输入)。
插件fckeditor引导文件 require_once ‘js/fckeditor/fckeditor.php’; 也可用Zend::Loader()加载,但是根据官方手册介绍,效率是一样的

用PHP现实J2ee持久化访问模式-DAO

04月 4th, 2008

通过“用户”来举例:

< ?php
#模型
class Member
{
private $uid;
private $username;
private $password;

public function setUid($_uid)
{
$this->uid = $_uid;
}

public function getUid()
{
return $this->uid;
}

public function setUsername($_username)
{
$this->username = $_username;
}
public function getUsername()
{
return $this->username;
}

public function setPassword($_password)
{
$this->password = $_password;
}
public function getPassword()
{
return $this->password;
}
}

#DAO
class MemberDAO
{
#基本变量
public $dbhost;
public $dbuser;
public $dbpassword;
public $dbname;

//构造函数为连接数据库
public function __construct($_dbhost,$_dbuser,$_dbpassword,$_dbname)
{
$this->dbhost=$_dbhost;
$this->dbuser=$_dbuser;
$this->dbpassword=$_dbpassword;
$this->dbname=$_dbname;
$this->conn= mysql_connect($this->dbhost,$this->dbuser,$this->dbpassword) or die(’连接数据库失败!’);
mysql_select_db($this->dbname);
}

//查询用户
public function getMember($_uid=”)
{
$members = array();

//如果参数uid为空,则查询全部
if(empty($_uid))
{
$res=mysql_query(”select * From members”);
}
else
{
$res=mysql_query(”select * From members WHERE uid = $uid”);
}

while($rows=mysql_fetch_assoc($res))
{
$member = new Member();
$member->setUid($row['uid']);
$member->setUsername($row['username']);
$member->setPassword($row['password']);

//追加到成员列表,如果不考虑查询多个用户则可不必这么麻烦
array_push($members,$member);
}
return $members;
}

//添加用户
public function addMember(Member $_member)
{
$uid = $_member->getUid();
$username = $_member->getUsername();
$password = md5($_member->getPassword());

$sql = “INSERT INTO members(uid,username,password) VALUES($uid,’$username’,'$password’)”;
mysql_query($sql_str) or die(mysql_error());
}

//修改资料
public function updateMember(Member $_member);
{
$uid = $_member->getUid();
$username = $_member->getUsername();
$password = md5($_member->getPassword());

$sql = “UPDATE members SET username=’$username’,password=’$password’ WHERE uid = $uid limit 1″;
mysql_query($sql_str) or die(mysql_error());
}

//删除用户………..
//其他操作用户的方法,不一一列举了
}

下面开始使用DAO

include ‘member.php’;
include ‘memberDAO.php’;
#创建DAO
$memberDAO = new memberDAO(’localhost’,'root’,'159753′,’test’);

#创建一个用户
$member = $new Member();
$member->setUid(911);
$member->setUsername(’tester’);
$member->setPassword(’321′);

#test 1
#添加到数据库
$memberDAO->addMember($member);

#test 2
#修改这个用户的密码
$member->setPassword(’123′);
$memberDAO->updateMember($member);

#test 3
#得到所有用户的列表
$members = $memberDAO->getMember();
#显示,这里假如不使用任何模版,只是测试显示数据,如果使用smrty模版引擎,更加方便,直接把$members传过去

foreach($members as $TmpMember)
{
echo ‘Uid:’.$TmpMember->getUid().’
‘;
echo ‘Username:’.$TmpMember->getUsername().’
‘;
echo ‘Password:’.$TmpMember->getPassword().’

‘;
}
?>

注意,示例代码仅供参考测试用,并不能直接使用在生产用途中。
推荐阅读:J2ee中DAO设计模式