我一直在试图上传一个图像文件到我的数据库中,作为属性image_name。我希望能够存储图像,然后它的文件路径保存到我的数据库..香港专业教育学院尝试了这里的许多选项..但似乎无法弄清楚如何做到这一点...Yii将图像上传到数据库
这是什么我走到这一步,如果有人可以点我这将是伟大正确的方向...
感谢
型号 -
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,
));
}
你的问题根本没有提供任何信息。问题是什么?怎么了? – Jon
它似乎并没有在数据库中创建新记录,也没有将图像文件路径或图像存储在其位置中。 – user3355603
阅读[documentation](http://www.yiiframework.com/doc/api /1.1/CUploadedFile)可能会有所帮助:“调用'getInstance'来检索上传文件的实例,然后使用'saveAs'将其保存在服务器上。” – Jon