CodeIgniter使用技巧
一、数据库备份与保存
$this->load->dbutil();
$backup =& $this->dbutil->backup();
$this->load->helper('file');
write_file('e:/mybackup.gz', $backup);
二、CI图像类 缩放,裁剪,旋转,水印(只可用在 GD/GD2)
$this->load->library('image_lib');
function do_image($image_name) {
$this->load->library('image_lib');
$config['image_library'] = 'GD';//选择图像库,还可以选ImageMagick
$config['source_image'] = "$image_name";
$config['maintain_ratio'] = TRUE;//当 maintain_ratio 选项设为可用时,生成的缩略图将在保持纵横比例的同时,尽可能的在宽度和高度上接近所设定的width和height
$config['width'] = 75;
$config['height'] = 50;
$config['new_image'] = 'newfolder/newname.png';//图片保存新名称
$config['create_thumb'] = TRUE;//是否生成缩略图
$this->image_lib->initialize($config);
if(!$this->image_lib->resize())
{echo "failed";}
else{echo 'success!';}
}
添加水印函数wm_image
function wm_image(){
$this->load->library('image_lib');
$config['source_image'] = 'uploads/waltzer.jpg';
$config['wm_text'] = 'Copyright 2007 - David Upton';
$config['wm_type'] = 'text';//添加文本水印,overlay为添加图片水印
$this->image_lib->initialize($config);
if(!$this->image_lib->watermark())
{echo 'failure to watermark';}
else
{echo 'success';}
}
三、CI ZIP类压缩文件 示例为压缩图片,并下载
function zip_image()
{
$this->load->library('zip');
$this->zip->archive('my_backup.zip');
$path = 'uploads/waltzer1.jpg';
$this->zip->read_file($path);
$this->zip->download('my_backup.zip');
}
四、文件下载
$data = file_get_contents("e:/mybackup.txt");//$data可为从数据库读取值
$name = 'backup.txt';
force_download($name, $data);
五、语言包加载
{$this->lang->load('welcome', 'german');}
elseif($user_language_pref == 'french')
{$this->lang->load('welcome', 'french');}
六、使用FTP类测试远程文件
$this->load->library('ftp');
$config['hostname'] = $hostname;
$config['username'] = $username;
$config['password'] = $password;
$config['debug'] = TRUE;
$this->ftp->connect($config);
$filelist = $this->ftp->list_files('/my_directory/');
$this->ftp->close();
return $list;
}
七、程序不报错
默认地, CI 在屏幕上显示所有的错误:error_reporting(E_ALL);
只要把这个改成:error_reporting(0);就不报错了