我是 Yii 新手,想集成文件上传。不幸的是,我无法让它工作。我已经下载插件并将其解压缩到 /extensions/ 文件夹,但现在我不知道该怎么做。
有人可以指导我吗?
这是扩展的链接
我是 Yii 新手,想集成文件上传。不幸的是,我无法让它工作。我已经下载插件并将其解压缩到 /extensions/ 文件夹,但现在我不知道该怎么做。
有人可以指导我吗?
这是扩展的链接
该扩展已过时且支持不佳。我改用这个扩展名:eajaxupload。
正如您在该页面上看到的那样,用法非常简单。将其上传到您的扩展目录并使用这几行代码安装它
首先,您将以下代码放入控制器操作中:
public function actionUpload()
{
Yii::import("ext.EAjaxUpload.qqFileUploader");
$folder='upload/';// folder for uploaded files
$allowedExtensions = array("jpg");//array("jpg","jpeg","gif","exe","mov" and etc...
$sizeLimit = 10 * 1024 * 1024;// maximum file size in bytes
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$result = $uploader->handleUpload($folder);
$result=htmlspecialchars(json_encode($result), ENT_NOQUOTES);
$fileSize=filesize($folder.$result['filename']);//GETTING FILE SIZE
$fileName=$result['filename'];//GETTING FILE NAME
echo $result;// it's array
}
之后转到该操作的关联视图文件并使用此代码生成上传表单:
<? $this->widget('ext.EAjaxUpload.EAjaxUpload',
array(
'id'=>'uploadFile',
'config'=>array(
'action'=>'/controller/upload',
'allowedExtensions'=>array("jpg");//array("jpg","jpeg","gif","exe","mov" and etc...
'sizeLimit'=>10*1024*1024,// maximum file size in bytes
'minSizeLimit'=>10*1024*1024,// minimum file size in bytes
//'onComplete'=>"js:function(id, fileName, responseJSON){ alert(fileName); }",
//'messages'=>array(
// 'typeError'=>"{file} has invalid extension. Only {extensions} are allowed.",
// 'sizeError'=>"{file} is too large, maximum file size is {sizeLimit}.",
// 'minSizeError'=>"{file} is too small, minimum file size is {minSizeLimit}.",
// 'emptyError'=>"{file} is empty, please select files again without it.",
// 'onLeave'=>"The files are being uploaded, if you leave now the upload will be cancelled."
// ),
//'showMessage'=>"js:function(message){ alert(message); }"
)
)); ?>
非常容易上手。搏一搏!
要在保存图像之前更改文件名,请取消注释行并提供您选择的名称。
./extensions/EAjaxUpload/qqFileUploader.php
$pathinfo = pathinfo($this->file->getName());
$filename = $pathinfo['filename'];
//$filename = md5(uniqid());
$ext = $pathinfo['extension'];