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

How to upload video and audio in Yii?

我想以我的 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;
}

输出:我不能select我的视频文件

来自 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;
         }
      }  
   }        
}

也许您在上传文件时遇到了这个问题:

  • increase upload_max_filesize
  • increase max_execution_time

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