【发布时间】:2014-03-29 05:11:22
【问题描述】:
我一直在尝试将图像文件上传到我的数据库中,该文件作为属性 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,
));
}
【问题讨论】:
-
您的问题根本没有给出任何信息。问题是什么?会发生什么?
-
它似乎没有在数据库中创建新记录,也没有将图像文件路径或图像存储在其位置..
-
阅读documentation 可能会有所帮助:“调用
getInstance检索上传文件的实例,然后使用saveAs将其保存在服务器上。” -
我看过这个,我的代码似乎是正确的.. 只是似乎无法深入了解它......
-
您的表单标签上的经典 enctype="multipart/form-data" 也许?
标签: php image yii image-upload