codeigniter(代码igniter的优点)

admin 511 0

大家好,关于codeigniter很多朋友都还不太明白,今天小编就来为大家分享关于代码igniter的优点的知识,希望对各位有所帮助!

一、如何在codeigniter中集成swfupload

首先准备需要的文件, swfupload.js以及 handlers.js这两个文件。

class Photo extends Controller

$this->load->view("photo");

<table width="98%" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#DDDDDD">

<form action="<?=site_url("admin/photo/save")?>" method="post" name="form1" enctype="multipart/form-data">

<td width="18%" height="26" align="right" bgcolor="#FFFFFF">上传图片:</td>

<td width="82%" align="left" bgcolor="#FFFFFF"><div style="display: inline; border: solid 1px#7FAAFF; background-color:#C5D9FF; padding: 2px;"><span id="spanButtonPlaceholder"></span></div></td>

<td height="26" align="right" bgcolor="#FFFFFF"></td>

<td align="left" bgcolor="#FFFFFF"><div id="divFileProgressContainer" style="height: 75px;"></div>

<div id="thumbnails"></div></td>

<script type="text/javascript" src="/js/swfupload/swfupload.js"></script>

<script type="text/javascript" src="/js/swfupload/handlers.js"></script>

<script type="text/javascript">

upload_url:"/admin/photo/previw",

post_params:{"PHPSESSID":"<?php echo session_id();?>"},

file_types:"*.jpg;*.png;*.gif;",

file_types_description:"Image files",

// Event Handler Settings- these functions as defined in Handlers.js

// The handlers are not part of SWFUpload but are part of my website and control how

// my website reacts to the SWFUpload events.

file_queue_error_handler: fileQueueError,

file_dialog_complete_handler: fileDialogComplete,

upload_progress_handler: uploadProgress,

upload_error_handler: uploadError,

upload_success_handler: uploadSuccess,

upload_complete_handler: uploadComplete,

button_image_url:"/images/SmallSpyGlassWithTransperancy_17x18.png",

button_placeholder_id:"spanButtonPlaceholder",

button_text:'<span class="button">Select Images<span class="buttonSmall">(8 MB Max)</span></span>',

button_text_style:'.button{ font-family: Helvetica, Arial, sans-serif; font-size: 12pt;}.buttonSmall{ font-size: 10pt;}',

button_text_left_padding: 18,

button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,

button_cursor: SWFUpload.CURSOR.HAND,

flash_url:"/js/swfupload/swfupload.swf",

upload_target:"divFileProgressContainer"

注意upload_url以及flash_url。还有swfupload.js,handlers.js的路径不要弄错了。然后页面上必须有这个:

<div id="divFileProgressContainer" style="height: 75px;"></div>

<div id="thumbnails"></div>

参数中,upload_url指向了photo的privew方法

所以我们还得继续修改photo类,添加一个privew方法,这个方法代码是做什么的呢?本文开头提到了是以那个demo为基础,在codeigniter中集成。

我们看看demo中的代码是怎么写的:

demo中,upload_url指向了upload.php,因此我们只需要将upload的代码复制到privew中去。此处就不贴出代码了。然后怎么返回最后的缩略图呢?

看看 handlers.js,其中有个自定义函数uploadSuccess,这个函数中间有这么一句话:

addImage("thumbnail.php?id="+ serverData.substring(7));

addImage("/admin/photo/thumbnail/"+ serverData.substring(7));

因此继续修改photo类,添加一个thumbnail方法,将原来的thumbnail.php代码复制进去即可。

class Photo extends Controller

$this->conf["file"]['upload_path']='./attachment/';

$this->conf["file"]['allowed_types']='gif|jpg|png|jpeg';

$this->conf["img"]['image_library']='gd2';

$this->conf["img"]['source_image']="";

$this->conf["img"]['create_thumb']= false;

$this->conf["img"]['maintain_ratio']= TRUE;

$this->conf["img"]['width']= 800;

$this->conf["img"]['height']= 600;

unset($_SESSION["file_info"]);

$this->load->view("admin/photo");

if(isset($_POST["PHPSESSID"])){

session_id($_POST["PHPSESSID"]);

ini_set("upload_max_filesize","8M");

if(!isset($_FILES["Filedata"])||!is_uploaded_file($_FILES["Filedata"]["tmp_name"])||$_FILES["Filedata"]["error"]!= 0){

//上传文件,并重新设定图片尺寸

$this->load->library('upload',$this->conf["file"]);

$this->upload->do_upload("Filedata");

$file=$this->upload->data();

$thumb=$file["file_path"]."{$file["raw_name"]}_thumb.jpg";

$_SESSION["files"][]=array("path"=>$file["full_path"],"raw_name"=>$file["raw_name"],"thumb"=>$thumb);

$this->conf["img"]['source_image']=$file["full_path"];

$this->load->library('image_lib',$this->conf["img"]);

$this->image_lib->resize();

// Get the image and create a thumbnail

$img= imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);

echo"ERROR:could not create image handle".$_FILES["Filedata"]["tmp_name"];

echo"ERROR:Invalid width or height";

$target_ratio=$target_width/$target_height;

if($target_ratio>$img_ratio){

$new_width=$img_ratio*$target_height;

$new_height=$target_width/$img_ratio;

if($new_height>$target_height){

if($new_width>$target_width){

$new_img= ImageCreateTrueColor(150, 150);

$white=imagecolorallocate($new_img,255,255,255);

if(!@imagefilledrectangle($new_img, 0, 0,$target_width-1,$target_height-1,$white)){

echo"ERROR:Could not fill new image";

if(!@imagecopyresampled($new_img,$img,($target_width-$new_width)/2,($target_height-$new_height)/2, 0, 0,$new_width,$new_height,$width,$height)){

echo"ERROR:Could not resize image";

if(!isset($_SESSION["file_info"])){

$_SESSION["file_info"]= array();

// Use a output buffering to load the image into a variable

imagejpeg($new_img,$thumb,100);

$imagevariable= ob_get_contents();

$file_id= md5($_FILES["Filedata"]["tmp_name"]+ rand()*100000);

$_SESSION["file_info"][$file_id]=$imagevariable;

echo"FILEID:".$file_id;// Return the file id to the script

if(isset($_POST["PHPSESSID"])){

session_id($_POST["PHPSESSID"]);

$image_id=$id=$this->uri->segment(4);

header("HTTP/1.1 500 Internal Server Error");

if(!is_array($_SESSION["file_info"])||!isset($_SESSION["file_info"][$image_id])){

header("HTTP/1.1 404 Not found");

header("Content-type: image/jpeg");

header("Content-Length:".strlen($_SESSION["file_info"][$image_id]));

echo$_SESSION["file_info"][$image_id];

if(count($_SESSION["files"])){

show_error("你还木有上传任何图片吧?[<a href='javascript:history.go(-1)'>返回</a>]");

swfupload的upload_url指向了previw方法,在这个方法中,与原有的代码比较,稍加修改,加上了文件上传功能,以及从新设定图片大小,以及保存文件缩略图,并将上传的结果保存到$_SESSION["files"]中去。

thumbnail方法主要是thumbnail.php的代码,略有修改。

二、CodeIgniter 是什么

1、CodeIgniter是一个为用 PHP编写网络应用程序的人员提供的工具包。它的目标是实现让你比从零开始编写代码更快速地开发项目,为此,CI提供了一套丰富的类库来满足通常的任务需求,并且提供了一个简单的接口和逻辑结构来调用这些库。CodeIgniter可以将需要完成的任务代码量最小化,这样你就可以把更多的精力放到项目的开发上了。

2、CodeIgniter是经过 Apache/BSD-style开源许可授权的,只要你愿意就可以使用它。阅读许可协议可获得更多的信息。

3、真正的轻量级。我们的核心系统只需要一些非常小的库,这与那些需要更多资源的框架完全相反。额外的库文件只在请求的时候加载,依需求而定,所以核心系统是非常快而且轻的。

4、速度非常快。你要找到一个比 CodeIgniter表现更优的框架应该很难吧。

5、CodeIgniter使用了模型(Model)-视图(View)-控制器(Controllers)的方法,这样可以更好地使表现层和逻辑层分离。这对项目的模板设计者来说是非常有用的,它最小化了模板中的程序代码量。我们在 MVC各自的页面中对此做了更多的介绍。

6、CodeIgniter生成的 URL非常干净而且是对搜索引擎友好化的。不同于标准的字符串查询方法,CodeIgniter使用了基于段的方法:

7、example.com/news/article/345注意:index.php文件是被默认包含在 URL中的,但是可以通过更改.htaccess文件来改变这个设置。

8、CodeIgniter拥有全范围的类库,可以完成大多数通常需要的网络开发任务,包括:读取数据库、发送电子邮件、数据确认、保存 session、对图片的操作,以及支持 XML-RPC数据传输等。

9、这个系统可以非常简单的通过自定义类库、辅助函数来进行扩展,或者也可以通过扩展类、系统钩子来实现。

10、虽然 CodeIgniter确实自带了一个可选的模板解析器程序,但不要求你必须使用模板。模板引擎完全与本地化PHP代码的性能需求不符,使用模板引擎我们要学习其语法,这最低限度只比学PHP基础要容易一点点。考虑以下PHP代码:<ul

11、<?php endforeach;?</ul再来对比模板引擎所使用的伪代码:<ul

12、{foreach from=$addressbook item=name}

13、<li{$name}</li

14、{/foreach}</ul的确,例中模板引擎的代码比较清晰,但这带来一个性能问题,因为伪代码要先被转换成PHP才能运行。我们的目标是性能最大化

15、,所以我们选择不使用专用的模板引擎。

16、程序员都喜欢写代码讨厌写文档。当然我们也一样,但是既然文档和代码本身一样重要,我们就要完成它了。况且我们代码资源极其干净而且方便注释。

17、CodeIgniter拥有一个友好的用户社区

18、你可以在我们的社区论坛中看到一个成长中的积极活跃的用户社区。翻译贡献者:

三、CodeIgniter与ThinkPHP两个框架各自有什么优势

1、CodeIgniter是一个小巧但功能强大的 PHP框架,作为一个简单而“优雅”的工具包,它可以为开发者们建立功能完善的 Web应用程序。

2、ThinkPHP是一个开源的PHP框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。最早诞生于 2006年初,原名FCS,2007年元旦正式更名为ThinkPHP,并且遵循Apache2开源协议发布。早期的思想架构来源于Struts,后来经过不断改进和完善,同时也借鉴了国外很多优秀的框架和模式,使用面向对象的开发结构和MVC模式,融合了Struts的Action和DAO思想和JSP的TagLib(标签库)、RoR的ORM映射和ActiveRecord模式,封装了CURD和一些常用操作,单一入口模式等,在模版引擎、缓存机制、认证机制和扩展性方面均有独特的表现

3、具体的区别可以去php中文网,两个框架的视频教程都有,可以自己去看看,比较下,希望对你有帮助

四、php的Codeigniter有什么安全漏洞吗

1、例如,你可能写一个允许用户查看日历的如下代码,通过调用 UNIX 的 cal 命令来显示指定月份。

2、$month=$_GET['month'];

3、exec("cal$month$year",$result);

4、foreach($result as$r){ print"$r<BR>";}

5、此代码具有一个安全漏洞缝隙,因为没有以任何的方式来验证 $_GET[month] 和 $_GET[year] 变量。只要那个特定的月份是在 1 到 12 之间,并且提供一个合适的四位数年份,那这个应用程序将完美运行。然而,恶意用户可能追加 “; ls- la” 到年参数,从而看到您网站的 HTML目录列表。一个极端恶劣的用户可能追加 ";rm-rf*" 到年参数,且删除整个网站 !

好了,文章到此结束,希望可以帮助到大家。