我正在尝试从Yii框架中的注册表单上传图像。图像将保存在“img / avatar”文件夹中,图像名称应更改为用户名。我用于此的代码段如下:
//uploading avatar to the img/avatar folder
$upload_file = CUploadedFile::getInstance($personModel, 'picture');
$personModel->picture = $upload_file;
$picture_name = $userModel->username;
$personModel->picture = $picture_name;
if(isset($upload_file))
{
$upload_file->saveAs(Yii::app()->basePath.'/../img/avatar'.$picture_name);
}
$personModel->save();
//end of image uploading part
问题是:用户名已保存在数据库的图片行中。但图像未上传到该文件夹。我试图找出代码中的问题。但无法解决它。有什么建议吗?
答案 0 :(得分:0)
首先,您需要做的是在未保存图片时阻止数据库输入。
if(isset($uploadedfile))
{
if($upload_file->saveAs(Yii::app()->basePath.'/../img/avatar'.$picture_name)
{
$personModel->save();
}
else
{
//throw error
}
}
就代码中的问题而言。最常见的问题是目录不存在,它们的路径不正确。
答案 1 :(得分:0)
$upload_file = CUploadedFile::getInstance($personModel, 'picture');
$ext = pathinfo($upload_file->picture, PATHINFO_EXTENSION);
$picture_name = $userModel->username . '.' . $ext;
$personModel->picture = $picture_name;
if(isset($upload_file))
{
$upload_file->saveAs('/Your_correct_path/.../etc/'.$picture_name);
}
$personModel->save();
答案 2 :(得分:0)
select
chr(27)||'[35m'||chr(27)||'[1;'||to_char(rownum+29)||'mTest' d
, chr(27)||'[36m'||chr(27)||'[1;'||to_char(rownum+29)||'mTest' e
, chr(27)||'[37m'||chr(27)||'[1;'||to_char(rownum+29)||'mTest' f
, chr(27)||'[38m'||chr(27)||'[1;'||to_char(rownum+29)||'mTest' g
, chr(27)||'[39m'||chr(27)||'[1;'||to_char(rownum+29)||'mTest' h
from dual
connect by level<=5
您必须检查文件夹权限。
答案 3 :(得分:0)
问题已通过以下代码解决:
$uploadFile = CUploadedFile::getInstance($personModel, 'picture');
$extension = pathinfo($uploadFile, PATHINFO_EXTENSION);
$fileName = $userModel->username . '.' . $extension;
if (isset($uploadFile)) {
$personModel->picture = $fileName;
$uploadFile->saveAs(Yii::app()->basePath . '/../img/avatar/' . $fileName);
}