codeigniter CodeIgniter 是什么
大家好,今天我将为大家揭秘codeigniter和CodeIgniter 是什么的奥秘,希望我的分享能给你带来新的启发和知识。
CodeIgniter 是什么
CodeIgniter是一个为用 PHP编写网络应用程序的人员提供的工具包。它的目标是实现让你比从零开始编写代码更快速地开发项目,为此,CI提供了一套丰富的类库来满足通常的任务需求,并且提供了一个简单的接口和逻辑结构来调用这些库。CodeIgniter可以将需要完成的任务代码量最小化,这样你就可以把更多的精力放到项目的开发上了。
CodeIgniter是免费的
CodeIgniter是经过 Apache/BSD-style开源许可授权的,只要你愿意就可以使用它。阅读许可协议可获得更多的信息。
CodeIgniter是轻量级的
真正的轻量级。我们的核心系统只需要一些非常小的库,这与那些需要更多资源的框架完全相反。额外的库文件只在请求的时候加载,依需求而定,所以核心系统是非常快而且轻的。
CodeIgniter是快速的
速度非常快。你要找到一个比 CodeIgniter表现更优的框架应该很难吧。
CodeIgniter使用 M-V-C模型
CodeIgniter使用了模型(Model)-视图(View)-控制器(Controllers)的方法,这样可以更好地使表现层和逻辑层分离。这对项目的模板设计者来说是非常有用的,它最小化了模板中的程序代码量。我们在 MVC各自的页面中对此做了更多的介绍。
CodeIgniter生成干净的 URL
CodeIgniter生成的 URL非常干净而且是对搜索引擎友好化的。不同于标准的字符串查询方法,CodeIgniter使用了基于段的方法:
example.com/news/article/345注意:index.php文件是被默认包含在 URL中的,但是可以通过更改.htaccess文件来改变这个设置。
CodeIgniter功能强大
CodeIgniter拥有全范围的类库,可以完成大多数通常需要的网络开发任务,包括:读取数据库、发送电子邮件、数据确认、保存 session、对图片的操作,以及支持 XML-RPC数据传输等。
CodeIgniter是可扩展的
这个系统可以非常简单的通过自定义类库、辅助函数来进行扩展,或者也可以通过扩展类、系统钩子来实现。
CodeIgniter不需要模板引擎
虽然 CodeIgniter确实自带了一个可选的模板解析器程序,但不要求你必须使用模板。模板引擎完全与本地化PHP代码的性能需求不符,使用模板引擎我们要学习其语法,这最低限度只比学PHP基础要容易一点点。考虑以下PHP代码:<ul
<li<?=$name?</li
<?php endforeach;?</ul再来对比模板引擎所使用的伪代码:<ul
{foreach from=$addressbook item=name}
<li{$name}</li
{/foreach}</ul的确,例中模板引擎的代码比较清晰,但这带来一个性能问题,因为伪代码要先被转换成PHP才能运行。我们的目标是性能最大化
,所以我们选择不使用专用的模板引擎。
CodeIgniter已彻底文档化
程序员都喜欢写代码讨厌写文档。当然我们也一样,但是既然文档和代码本身一样重要,我们就要完成它了。况且我们代码资源极其干净而且方便注释。
CodeIgniter拥有一个友好的用户社区
你可以在我们的社区论坛中看到一个成长中的积极活跃的用户社区。翻译贡献者:
php的Codeigniter有什么安全漏洞吗
例如,你可能写一个允许用户查看日历的如下代码,通过调用 UNIX 的 cal 命令来显示指定月份。
$month=$_GET['month'];
$year=$_GET['year'];
exec("cal$month$year",$result);
print"<PRE>";
foreach($result as$r){ print"$r<BR>";}
print"</PRE>";
此代码具有一个安全漏洞缝隙,因为没有以任何的方式来验证 $_GET[month] 和 $_GET[year] 变量。只要那个特定的月份是在 1 到 12 之间,并且提供一个合适的四位数年份,那这个应用程序将完美运行。然而,恶意用户可能追加 “; ls- la” 到年参数,从而看到您网站的 HTML目录列表。一个极端恶劣的用户可能追加 ";rm-rf*" 到年参数,且删除整个网站 !
如何在codeigniter中集成swfupload
首先准备需要的文件, swfupload.js以及 handlers.js这两个文件。
swfupload文件夹放在/js下。
1.首先我们创建一个controller
<?
class Photo extends Controller
{
function Photo()
{
parent::Controller();
}
function index()
{
$this->load->view("photo");
}
}
?>
然后为index方法创建一个视图,
photo.php
<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">
<tr>
<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>
</tr>
<tr>
<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>
</tr>
</form>
</table>
<style>
#thumbnails{ width:550px;}
</style>
<script type="text/javascript" src="/js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="/js/swfupload/handlers.js"></script>
<script type="text/javascript">
var swfu;
window.onload= function(){
swfu= new SWFUpload({
// Backend Settings
upload_url:"/admin/photo/previw",
post_params:{"PHPSESSID":"<?php echo session_id();?>"},
// File Upload Settings
file_size_limit:"8MB",// 2MB
file_types:"*.jpg;*.png;*.gif;",
file_types_description:"Image files",
//file_upload_limit:"0",
// 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 Settings
button_image_url:"/images/SmallSpyGlassWithTransperancy_17x18.png",
button_placeholder_id:"spanButtonPlaceholder",
button_width: 180,
button_height: 18,
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_top_padding: 0,
button_text_left_padding: 18,
button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
button_cursor: SWFUpload.CURSOR.HAND,
// Flash Settings
flash_url:"/js/swfupload/swfupload.swf",
custom_settings:{
upload_target:"divFileProgressContainer"
},
// Debug Settings
debug: false
});
};
</script>
注意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
{
var$conf=array();
function Photo()
{
parent::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;
}
function index()
{
unset($_SESSION["files"]);
unset($_SESSION["file_info"]);
$this->load->view("admin/photo");
}
function previw()
{
if(isset($_POST["PHPSESSID"])){
session_id($_POST["PHPSESSID"]);
}
ini_set("upload_max_filesize","8M");
// Check the upload
if(!isset($_FILES["Filedata"])||!is_uploaded_file($_FILES["Filedata"]["tmp_name"])||$_FILES["Filedata"]["error"]!= 0){
echo"ERROR:invalid upload";
exit(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"]);
if(!$img){
echo"ERROR:could not create image handle".$_FILES["Filedata"]["tmp_name"];
exit(0);
}
$width= imageSX($img);
$height= imageSY($img);
if(!$width||!$height){
echo"ERROR:Invalid width or height";
exit(0);
}
// Build the thumbnail
$target_width= 150;
$target_height= 150;
$target_ratio=$target_width/$target_height;
$img_ratio=$width/$height;
if($target_ratio>$img_ratio){
$new_height=$target_height;
$new_width=$img_ratio*$target_height;
} else{
$new_height=$target_width/$img_ratio;
$new_width=$target_width;
}
if($new_height>$target_height){
$new_height=$target_height;
}
if($new_width>$target_width){
$new_height=$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";
exit(0);
}
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";
exit(0);
}
if(!isset($_SESSION["file_info"])){
$_SESSION["file_info"]= array();
}
// Use a output buffering to load the image into a variable
ob_start();
//保存缩略图,并生成文件
imagejpeg($new_img,$thumb,100);
imagejpeg($new_img);
$imagevariable= ob_get_contents();
ob_end_clean();
$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
}
function thumbnail()
{
if(isset($_POST["PHPSESSID"])){
session_id($_POST["PHPSESSID"]);
}
session_start();
$image_id=$id=$this->uri->segment(4);
if($image_id=== false){
header("HTTP/1.1 500 Internal Server Error");
echo"No ID";
exit(0);
}
if(!is_array($_SESSION["file_info"])||!isset($_SESSION["file_info"][$image_id])){
header("HTTP/1.1 404 Not found");
exit(0);
}
header("Content-type: image/jpeg");
header("Content-Length:".strlen($_SESSION["file_info"][$image_id]));
echo$_SESSION["file_info"][$image_id];
exit(0);
}
function save()
{
if(count($_SESSION["files"])){
//save files....
}
else
{
show_error("你还木有上传任何图片吧?[<a href='javascript:history.go(-1)'>返回</a>]");
}
}
}
?>
swfupload的upload_url指向了previw方法,在这个方法中,与原有的代码比较,稍加修改,加上了文件上传功能,以及从新设定图片大小,以及保存文件缩略图,并将上传的结果保存到$_SESSION["files"]中去。
thumbnail方法主要是thumbnail.php的代码,略有修改。
如果你还想了解更多这方面的信息,记得收藏关注本站。