2014-02-26 2 views
-1

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, 
    )); 
} 
01 나는 ... 지금까지 사람이 좋은 것 올바른 방향으로 날 지점 수 있다면

감사

모델을 가지고

+0

귀하의 질문에 아무런 정보도 제공되지 않습니다. 문제가 무엇입니까? 무슨 일이야? – Jon

+0

데이터베이스에 새 레코드를 만들지 못하고 이미지 파일 경로 나 이미지를 해당 위치에 저장하지 않습니다. – user3355603

+0

[documentation] (http://www.yiiframework.com/doc/api) 읽기 /1.1/CUploadedFile)는 아마도 도움이 될 것입니다 : "getInstance를 호출하여 업로드 된 파일의 인스턴스를 검색 한 다음 서버에 saveAs를 사용하여 저장하십시오." – Jon

답변

0

코드 읽기, uploadedFile->saveAs() 메서드는 url이 아닌 파일의 전체 경로를 필요로합니다.

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

을이 예에서는 images 디렉토리를 (images 디렉토리는 URL을 통해 디스플레이 공공 이미지에 사용되는 경우에만) protected 디렉토리와 동일한 레벨에 있어야합니다 :

이와 시도, 전체 경로를 얻으려면

assets/ 
images/ 
protected/ 
themes/ 

Unix 환경에있는 경우 images dir에 적절한 읽기/쓰기 권한이 있어야합니다.

작동하지 않는 경우 Yii로 파일 업로드를 소개하는 데 도움이되는 review this useful sample tutorial step by step.

+0

올바른 방향으로 나를 가리켜 주셔서 감사합니다. – user3355603

관련 문제