[个人补充] ThinkPHP可以很好地与新浪SAE融合
在向SAE上的服务器上传资源是,需要启用SAE的Storage服务,而且不需要对上传部分的代码做任何改动。
比如,本地运行的上传文件路径是 $upload->savePath='./Public/Uploads/'; //设置上传目录
则在SAE的Storage服务上需要建立一个名为public的Domain,并在此Domain下建立Uploads目录
下载文件时,需要作出修改,应该从Storage上下载,如:http://tibettour-public.stor.sinaapp.com/Upload/Sunset.jpg
-------------------以下是转载内容--------------------------
ThinkPHP中使用上传功能无需特别处理。
例如,下面是一个带有附件上传的表单提交:
<
form
action
=
"
__URL__/add
"
method
=
"
post
"
enctype
=
"
multipart/form-data
"
/>
文章标题: < input type = " text " name = " title " size = " 50 " />< br />
自定义属性: < input type = " checkbox " name = " char[] " value = " h " /> 头条[h] < input type = " checkbox " name = " char[] " value = " c " /> 推荐[c] < input type = " checkbox " name = " char[] " value = " a " /> 特荐[a] < br />
缩略图: < input type = " file " name = " thpic " />< br />
< input type = " submit " value = " 保存 " />
</ form >
文章标题: < input type = " text " name = " title " size = " 50 " />< br />
自定义属性: < input type = " checkbox " name = " char[] " value = " h " /> 头条[h] < input type = " checkbox " name = " char[] " value = " c " /> 推荐[c] < input type = " checkbox " name = " char[] " value = " a " /> 特荐[a] < br />
缩略图: < input type = " file " name = " thpic " />< br />
< input type = " submit " value = " 保存 " />
</ form >
控制器NewsAction.class.php:
<?php
class NewAction extends Action {
public function add (){
$news = D ( ' News ' ) ;
if ( $_FILES [ ' thpic ' ][ ' name ' ]){ //这里要判断一下 ,要不然upload()会返回null 导致写入数据库失败
$picinfo = $this -> upload () ;
$news -> thpic = $picinfo ; //保存上传的照片,根据需要自行组装
//thpic 为数据库缩略图字段
}
if ( $news -> add ()){
$this -> success ( ' 添加成功! ' ) ;
} else {
//dump($news->getLastSql());
//dump($news->getError());
$this -> error ( ' 添加失败 ' ) ;
}
}
public function upload (){
import ( ' @.ORG.UploadFile ' ) ; //导入上传类 一开始我写的是import('ORG.Util.UploadFile') 执行上传会报错: Fatal error: Class 'UploadFile' not found
$upload = new UploadFile () ; //实例化上传类
$upload -> maxSize = 3000000 ; //设置附件大小
$upload -> allowExts = array ( ' jpg ' , ' gif ' , ' png ' , ' jpeg ' ) ; //设置附件上传类型
$upload -> savePath = ' ./Public/Uploads/ ' ; //设置上传目录
$upload -> saveRule = uniqid () ; //上传文件的保存规则,有time、uniqid等等
$upload -> thumb = true ; //是否需要对图片文件进行缩略图处理,默认为false
$upload -> thumbMaxWidth = ' 160 ' ; //缩略图的最大宽度,多个使用逗号分隔
$upload -> thumbMaxHeight = ' 125 ' ; //缩略图的最大高度,多个使用逗号分隔
$upload -> thumbPrefix = ' th ' ; //缩略图前缀
if ( ! $upload -> upload ()){ //上传错误 提示错误信息
$this -> error ( $upload -> getErrorMsg ()) ;
} else { //上传成功 获取上传文件信息
$info = $upload -> getUploadFileInfo () ;
return $info [ 0 ][ savename ] ;
/**
* dump($info); //打印出上传info数组
* //打印出的结果如下
* array(1) {
* [0] => array(8) {
* ["name"] => string(19) "具体内容页.jpg"
* ["type"] => string(10) "image/jpeg"
* ["size"] => int(1456281)
* ["key"] => int(0)
* ["extension"] => string(3) "jpg"
* ["savepath"] => string(17) "./Public/Uploads/"
* ["savename"] => string(17) "5039acd0f20a6.jpg"
* ["hash"] => string(32) "d0f5560b26e2c0c451327d13d877ba73"
* }
* }
*/
}
}
}
?>
class NewAction extends Action {
public function add (){
$news = D ( ' News ' ) ;
if ( $_FILES [ ' thpic ' ][ ' name ' ]){ //这里要判断一下 ,要不然upload()会返回null 导致写入数据库失败
$picinfo = $this -> upload () ;
$news -> thpic = $picinfo ; //保存上传的照片,根据需要自行组装
//thpic 为数据库缩略图字段
}
if ( $news -> add ()){
$this -> success ( ' 添加成功! ' ) ;
} else {
//dump($news->getLastSql());
//dump($news->getError());
$this -> error ( ' 添加失败 ' ) ;
}
}
public function upload (){
import ( ' @.ORG.UploadFile ' ) ; //导入上传类 一开始我写的是import('ORG.Util.UploadFile') 执行上传会报错: Fatal error: Class 'UploadFile' not found
$upload = new UploadFile () ; //实例化上传类
$upload -> maxSize = 3000000 ; //设置附件大小
$upload -> allowExts = array ( ' jpg ' , ' gif ' , ' png ' , ' jpeg ' ) ; //设置附件上传类型
$upload -> savePath = ' ./Public/Uploads/ ' ; //设置上传目录
$upload -> saveRule = uniqid () ; //上传文件的保存规则,有time、uniqid等等
$upload -> thumb = true ; //是否需要对图片文件进行缩略图处理,默认为false
$upload -> thumbMaxWidth = ' 160 ' ; //缩略图的最大宽度,多个使用逗号分隔
$upload -> thumbMaxHeight = ' 125 ' ; //缩略图的最大高度,多个使用逗号分隔
$upload -> thumbPrefix = ' th ' ; //缩略图前缀
if ( ! $upload -> upload ()){ //上传错误 提示错误信息
$this -> error ( $upload -> getErrorMsg ()) ;
} else { //上传成功 获取上传文件信息
$info = $upload -> getUploadFileInfo () ;
return $info [ 0 ][ savename ] ;
/**
* dump($info); //打印出上传info数组
* //打印出的结果如下
* array(1) {
* [0] => array(8) {
* ["name"] => string(19) "具体内容页.jpg"
* ["type"] => string(10) "image/jpeg"
* ["size"] => int(1456281)
* ["key"] => int(0)
* ["extension"] => string(3) "jpg"
* ["savepath"] => string(17) "./Public/Uploads/"
* ["savename"] => string(17) "5039acd0f20a6.jpg"
* ["hash"] => string(32) "d0f5560b26e2c0c451327d13d877ba73"
* }
* }
*/
}
}
}
?>