上传图片的代码段
普通上传图片的代码段
RefundController->actionUploadImage()
public function actionUploadImage()
{
include_once(ROOT . '/gb_php/upload.class.php');
$oUpload = new Upload();
$oUpload->SetIsSmall(false);
//文件存储目录
$targetFolder = ROOT.'/pic/tumall/refund/day_'.date('ymd').'/';
$imgFolder = "tumall/refund/day_".date('ymd').'/';
$targetPath=$targetFolder;
if (!to8to_is_dir($targetPath)) // 判断是否是文件夹,以及权限
{
if(!@to8to_mkdir($targetPath, 0777,true));
}
if(count($_FILES['img']['name'])>5)//图片大于5张
{
echo "<script>window.parent.imgcount();</script>";
exit;
}
$isOk = $oUpload->UploadFile('img',$targetFolder);
$aPicInfo = $oUpload->GetInfo();
if($aPicInfo)
{
foreach($aPicInfo as $val)
{
$filename=$imgFolder.$val['file_name'];
echo "<script>window.parent.bankimag('".$filename."');</script>"; //返回图片在服务器的路径
}
}
exit();
}
异步上传图片的代码段
RefundmentOrderController->actionAjaxAddImg()
public function actionAjaxAddImg(){
$result = array();
require_once(ROOT.'/front/protected/modules/my/components/Upload.php');
$post = Fun::params($_POST);
// 1MB
$upload = new Upload(array(
'appPath' => '/tumall/refundcredence',
'maxSize' => 1024 * 1024,
));
if( isset($post['filename']) ){
$upload_result = $upload->save(html($post['filename']));
//上传失败
if(!$upload_result['status']){
$result ['status'] = 0;
$result ['error'] = 1;
}else{
$filepath = $upload_result['filepath']; //图片路径
$result ['status'] = 1;
$result ['error'] = 0;
$result ['imgsrc'] = 'http://pic.to8to.com'.'/'.trim($filepath,'/'); //图片访问地址
$result ['filepath'] = $filepath;
}
$result['message'] = $upload_result['message'];
//echo '<script type="text/javascript">document.domain = "to8to.com";</script>';
}
echo json_encode($result);
exit;
}