2014-11-11 2 views
0

안녕하세요, 지난 2 일 동안 저는 Yii의 폴더에 파일을 저장하는 방법에 대한 자습서를 많이 읽고 읽고있었습니다. 나는 folowing 형식을 가지고 :Yii Framework 이미지 폴더에 제출 된 파일 저장/저장

<div class="form"> 

<?php $form = $this->beginWidget('CActiveForm', array(
'htmlOptions' => array('enctype' => 'multipart/form-data') 
)); ?> 

<?php echo $form->errorSummary($model); ?> 

<div class="row"> 
    <?php echo $form->labelEx($model,'Binaryfile'); ?> 
    <?php echo $form->fileField($model,'uploadedFile'); ?> 
    <?php echo $form->error($model,'uploadedFile'); ?> 
</div> 

<div class="row buttons"> 
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> 
</div> 
endWidget(); ?>

파일 필드는 MySQL 데이터베이스에 BLOB 필드에 코드를 제출합니다.

public function actionCreate() 
{ 
    $model=new Estudos; 

    // Uncomment the following line if AJAX validation is needed 
    // $this->performAjaxValidation($model); 

    if(isset($_POST['Estudos'])) 
    { 
     $model->attributes=$_POST['Estudos']; 
     $model->binaryfile = CUploadedFile::getInstance($model,'binaryfile'); // grava na bd no campo binaryfile 
     // $model->binaryfile->saveAs(Yii::app()->params['uploadPath']); 

     if($model->save()) 
      $this->redirect(array('view','id'=>$model->id)); 
    } 

    $this->render('create',array(
     'model'=>$model, 
    )); 
} 

그리고 모델이 하나입니다 : 다음과 같이 컨트롤러는 인 코드는 BLOB 필드로 파일을 저장하기 위해 잘 작동

public function rules() 
{ 
    // NOTE: you should only define rules for those attributes that 
    // will receive user inputs. 
    return array(
     array('fileName', 'length', 'max'=>100), 
     array('fileType', 'length', 'max'=>50), 
     array('binaryfile', 'safe'), 
     // The following rule is used by search(). 
     // @todo Please remove those attributes that should not be searched. 
     array('id, fileName, fileType, binaryfile', 'safe', 'on'=>'search'), 
    ); 
} 

public $uploadedFile; 
// Gravar imagem na base de dados - cria blob field 
public function beforeSave() 
{ 

    if ($file = CUploadedFile::getInstance($this, 'uploadedFile')) 
    { 
     $this->fileName = $file->name; 
     $this->fileType = $file->type; 
     $this->binaryfile = file_get_contents($file->tempName); 
    } 

    return parent::beforeSave(); 
} 

,하지만 난 변경해야 코드는 이미지 폴더에 파일을 저장하고 모든 브라우저에서 파일 (pdf 파일)을 열 수있는 링크를 표시합니다. 이미지 폴더에 파일을 저장하려면 내 컨트롤러 actionCreate에서 saveAs()를 시도했지만 Yii가 멈추고 오류없이 웹 페이지가 비워졌습니다. 단지 비어 있습니다.

** 누구든지 나를 도울 수 있습니다 ... 나는 이것을 아주 많이 필요로합니다. 미리 감사드립니다. **

답변

0

마침내 나는 그것을 직접 알아 냈습니다. 대답은 오히려 간단했지만 작성하는 데 4 일이 걸렸습니다.

public function actionCreate() 
{ 
    $model=new Estudos; 


    // Uncomment the following line if AJAX validation is needed 
    // $this->performAjaxValidation($model); 

    if(isset($_POST['Estudos'])) 
    { 
     $model->attributes=$_POST['Estudos']; 
     $model->uploadedFile=CUploadedFile::getInstance($model,'uploadedFile'); 
     if($model->save()) 
      $model->uploadedFile->saveAs("pdfs/".$model->uploadedFile,true); 
      $this->redirect(array('view','id'=>$model->id));  
    } 

    $this->render('create',array(
     'model'=>$model, 
    )); 
} 

** 다른 이름으로 저장() 함수는 마법처럼 일 그 방법을 지금은 PDF 파일 폴더에 내 submited 파일을 저장합니다 : 내 actionCreate에서 은() 내가했다. 다음 단계는 pdfs 폴더에 제출 된 모든 파일에 대한 링크를 만드는 방법을 찾는 것입니다. foreach() 루프가있을 수 있습니다. 이 링크에서

감사합니다 ... **