2014-11-23 17 views
0

我被困在一個問題,在這裏我需要上傳兩個不同的文件(例如,類型jpg和另一個類型pdf/epub)以及其他表單數據。如何將Yii上的文件和表單數據一起上傳?

表單數據應該與文件的路徑一起上傳到數據庫,並且文件應保存在目錄中。

任何幫助,將不勝感激。

BooksController.php

<?php 

    namespace backend\controllers; 

    use backend\models\Books; 
    use Yii; 
    use yii\data\ActiveDataProvider; 
    use yii\filters\VerbFilter; 
    use yii\web\Controller; 
    use yii\web\NotFoundHttpException; 
    use yii\web\UploadedFile; 

    /** 
     * BooksController implements the CRUD actions for Books model. 
     */ 
    class BooksController extends Controller 
    { 
     public function behaviors() 
     { 
      return [ 
       'verbs' => [ 
        'class' => VerbFilter::className(), 
        'actions' => [ 
         'delete' => ['post'], 
        ], 
       ], 
      ]; 
     } 

     /** 
      * Lists all Books models. 
      * @return mixed 
      */ 
     public function actionIndex() 
     { 
      $dataProvider = new ActiveDataProvider([ 
       'query' => Books::find(), 
      ]); 

      return $this->render('index', [ 
       'dataProvider' => $dataProvider, 
      ]); 
     } 

     /** 
      * Displays a single Books model. 
      * @param integer $id 
      * @return mixed 
      */ 
     public function actionView($id) 
     { 
      return $this->render('view', [ 
       'model' => $this->findModel($id), 
      ]); 
     } 

     /** 
      * Creates a new Books model. 
      * If creation is successful, the browser will be redirected to the 'view' page. 
      * @return mixed 
      */ 
     public function actionCreate() 
     { 
      $model = new Books(); 

      $path = Yii::$app->basePath . '../../uploads/'; 
      if (!is_dir($path)) { 
       mkdir($path); 
      } 

      if (Yii::$app->request->post()) { 
       $book_file = UploadedFile::getInstance($model, 'book'); 
       $cover_file = UploadedFile::getInstance($model, 'cover'); 

       $book_file->saveAs($path . $book_file->baseName . '.' . $book_file->extension); 
       $cover_file->saveAs($path . $book_file->baseName . '_' . $cover_file->baseName .   '.' . $cover_file->extension); 

       return $this->redirect(['view', 'id' => $model->id]); 
      } 

      return $this->render('create', ['model' => $model]); 
     } 

     /** 
      * Updates an existing Books model. 
      * If update is successful, the browser will be redirected to the 'view' page. 
      * @param integer $id 
      * @return mixed 
      */ 
     public function actionUpdate($id) 
     { 
      $model = $this->findModel($id); 

      if ($model->load(Yii::$app->request->post()) && $model->save()) { 
       return $this->redirect(['view', 'id' => $model->id]); 
      } else { 
       return $this->render('update', [ 
        'model' => $model, 
       ]); 
      } 
     } 

     /** 
      * Deletes an existing Books model. 
      * If deletion is successful, the browser will be redirected to the 'index' page. 
      * @param integer $id 
      * @return mixed 
      */ 
     public function actionDelete($id) 
     { 
      $this->findModel($id)->delete(); 

      return $this->redirect(['index']); 
     } 

     /** 
      * Finds the Books model based on its primary key value. 
      * If the model is not found, a 404 HTTP exception will be thrown. 
      * @param integer $id 
      * @return Books the loaded model 
      * @throws NotFoundHttpException if the model cannot be found 
      */   
     protected function findModel($id) 
     { 
      if (($model = Books::findOne($id)) !== null) { 
       return $model; 
      } else { 
       throw new NotFoundHttpException('The requested page does not exist.'); 
      } 
     } 
    } 

Books.php(型號)

<?php 

namespace backend\models; 

use Yii; 

/** 
* This is the model class for table "books". 
* 
* @property integer $id 
* @property string $title 
* @property string $subtitle 
* @property string $description 
* @property string $author 
* @property string $isbn 
* @property integer $page 
* @property string $year 
* @property string $publisher 
* @property string $cover 
* @property string $link 
*/ 
class Books extends \yii\db\ActiveRecord 
{ 
    public $book; 
    public $cover; 

    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'books'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['title', 'page', 'cover', 'book'], 'required'], 
      [['description'], 'string'], 
      [['isbn', 'page'], 'integer'], 
      ['year', 'date'], 
      [['title', 'subtitle', 'author', 'publisher'], 'string', 'max' => 255], 
      ['book', 'file', 'extensions' => 'pdf, epub', 'maxSize' => 1024 * 1024 * 1024], 
      ['cover', 'file', 'extensions' => 'jpg, jpeg, png', 'maxSize' => 1024 * 1024 * 10] 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'id' => 'ID', 
      'title' => 'Title', 
      'subtitle' => 'Subtitle', 
      'description' => 'Description', 
      'author' => 'Author', 
      'isbn' => 'Isbn', 
      'page' => 'Page', 
      'year' => 'Year', 
      'publisher' => 'Publisher', 
      'cover' => 'Cover', 
      'book' => 'Book' 
     ]; 
    } 
} 

_form.php這個(查看)

<?php 

use yii\helpers\Html; 
use yii\widgets\ActiveForm; 

/* @var $this yii\web\View */ 
/* @var $model backend\models\Books */ 
/* @var $form yii\widgets\ActiveForm */ 
?> 

<div class="books-form"> 

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> 

    <?= $form->field($model, 'title')->textInput(['maxlength' => 255]) ?> 

    <?= $form->field($model, 'subtitle')->textInput(['maxlength' => 255]) ?> 

    <?= $form->field($model, 'description')->textarea(['rows' => 6]) ?> 

    <?= $form->field($model, 'author')->textInput(['maxlength' => 255]) ?> 

    <?= $form->field($model, 'isbn')->textInput(['maxlength' => 13]) ?> 

    <?= $form->field($model, 'page')->textInput() ?> 

    <?= $form->field($model, 'year')->textInput() ?> 

    <?= $form->field($model, 'publisher')->textInput(['maxlength' => 255]) ?> 

    <?= $form->field($model, 'book')->fileInput() ?> 

    <?= $form->field($model, 'cover')->fileInput() ?> 

    <div class="form-group"> 
     <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
    </div> 

    <?php ActiveForm::end(); ?> 

</div> 

回答

1

什麼不起作用 - 將文件保存到文件系統,o保存模型屬性?看起來您並沒有在您的actionCreate中致電$model->save()。另外,如果模型屬性名爲file並且它用於處理上傳的文件,則在保存模型時將會出現問題。可能您只想將文件名/文件路徑保存到數據庫。在這種情況下,您需要額外的屬性來處理這些值。

0

可以使用CMultiFileUpload部件。見example in docs。而且不要忘記添加multipart/form-data到您的窗體:

$form = $this->beginWidget('CActiveForm', array(
    'id'=>'user-form', 
    'htmlOptions' => array('enctype' => 'multipart/form-data'), 
)); 

和文件可以「用另一種形式的數據一起」進行處理。另請參閱CUploadedFile

+0

我試過這個,但它不適用於yii 2。 – 2014-11-23 07:49:35

相關問題