如何在Yii中上传视频和音频?

时间:2019-01-02 12:16:41

标签: php yii yii2 yii2-advanced-app

我想以Yii格式上传视频

我尝试:

$dbimage = '';
if (null != $patientmodel->treatment_videos) {
    $filename = uniqid() . '.' . $patientmodel->treatment_videos->extension;
    $patient_videos->saveAs($patientmodel->patientImgPath . '/' . $filename);
    $dbimage .= $filename . ',';
    $dbimage = rtrim($dbimage, ',');
    $patientmodel->treatment_videos = $dbimage;
}

输出:我无法选择我的视频文件

1 个答案:

答案 0 :(得分:0)

来自UploadedFile class的简单示例代码是:

view.php

<?= Html::beginForm('', 'post', ['enctype' => 'multipart/form-data']) ?>
<?= Html::fileInput('attachment', '', ['id' => 'attachment']) ?>
<?= Html::submitButton('Submit') ?>
<?= Html::endForm() ?>

controller.php

if ($file = UploadedFile::getInstanceByName('attachment')) { // check if file uploaded
   if (in_array($file->extension, ['mp3', 'wav'])) { // check valid audio file extensions
      $file_size_limit = 1024 * 1024 * 10 // 10mb
      if ($file->size < $file_size_limit) { // check max file size
         $path = 'path/to/directory';
         if ($file->saveAs($path)) { 
             return 'file uploaded successfully';
         }else{
             return $file->error;
         }
      }  
   }        
}

也许您在文件上传中看到了以下问题:

或在模型中使用时,可以使用input Uploading Files document