Yii将图像上传到数据库



我一直在尝试将一个图像文件上传到我的数据库中,作为属性image_name。我希望能够存储图像,然后将其文件路径存储到我的数据库中。。我在这里试了很多选择。。但似乎不知道怎么做…

这就是我到目前为止所得到的,如果有人能为我指明正确的方向,那就太好了。。。

感谢

型号-

public function attributeLabels()
{
return array(
'id' => 'ID',
'movie_name' => 'Movie Name',
'studio_id' => 'Studio',
'country_id' => 'Country',
'movie_rating_id' => 'Movie Rating',
'map_pin_id' => 'Map Pin',
'description' => 'Description',
'title_image' => 'Title Image',
'title_trailer_youtube_code' => 'Title Trailer Youtube Code',
'release_date' => 'Release Date',
'bg_colour_code' => 'Bg Colour Code',
'text_colour_code' => 'Text Colour Code',
'is_released' => 'Is Released',
'is_advanced_booking_allowed' => 'Is Advanced Booking Allowed',
'advanced_booking_start_date' => 'Advanced Booking Start Date',
'advanced_booking_end_date' => 'Advanced Booking End Date',
'domain_prefix' => 'Domain Prefix',
'facebook_app_id' => 'Facebook App',
'facebook_app_url' => 'Facebook App Url',
'facebook_icon_image_url' => 'Facebook Icon Image Url',
'facebook_text' => 'Facebook Text',
'twitter_text' => 'Twitter Text',
'booking_share_text' => 'Booking Share Text',
'home_tracking_url' => 'Home Tracking Url',
'shows_tracking_url' => 'Shows Tracking Url',
);
}

控制器-

public function actionCreate()
{
$model=new Movie;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Movie']))
{
$model->attributes=$_POST['Movie'];
$uploadedFile=CUploadedFile::getInstance($model,'title_image');
$fileName = $uploadedFile;
$model->title_image = $fileName;
if($model->save())
$uploadedFile->saveAs('http://localhost:8888/MY/Movies/title_image/'.$fileName);
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}

在阅读代码时,uploadedFile->saveAs()方法需要文件的完整路径,而不是url。

要获得完整路径,请尝试以下操作:

$uploadedFile->saveAs(Yii::getPathOfAlias('webroot').'/images/uploaded/'.$fileName);

在本例中,images目录应与protected目录处于同一级别(仅当images目录用于通过url显示公共图像时):

assets/
images/
protected/
themes/

如果您在Unix环境中,请确保对images目录提供正确的读/写访问权限。

如果不起作用,请一步一步地复习这个有用的示例教程,它有助于介绍使用Yii上传文件。