laravel:如何输出视频文件的文件路径

时间:2017-07-23 05:15:12

标签: php laravel-5.1 laravel-views

当我通过表单上传了一些视频时,它将我的视频保存在公共文件夹中,但在view我无法访问它们。

我的观点(输出文件路径的tyring)

    <td>{{ $media->video_ogg}} </td>
    <td>{{ $media->video_mp4}} </td>

结果

C:\Users\Jeffrey\AppData\Local\Temp\phpBB7C.tmp
C:\Users\Jeffrey\AppData\Local\Temp\phpBB7C.tmp

它应该在不同的路径上(我的项目路径)。

在我在视频文件夹上的本地计算机上,我可以看到视频已上传。

enter image description here

Mycontroller.php

    public function store(Request $request)
   {
      // Validation //
      $validation = Validator::make($request->all(), [
         'about_me' => 'required',
         'video_ogg'    => 'required|mimes:mp4,ogx,oga,ogv,ogg,webm|min:1|max:3240',
         'video_mp4'    => 'required|mimes:mp4,mov,ogg,qt,webm|min:1|max:3240'
      ]);

      // Check if it fails //
      if( $validation->fails() ){
         return redirect()->back()->withInput()
                          ->with('errors', $validation->errors() );
      }

      $profile = new Profile;

      // upload the media //
      $file = $request->file('video_ogg');
      $destination_path = 'videos/';
      $filename = str_random(6).'_'.$file->getClientOriginalName();
      $file->move($destination_path, $filename);

      // upload the media //
      $file = $request->file('video_mp4');
      $destination_path = 'videos/';
      $filename = str_random(6).'_'.$file->getClientOriginalName();
      $file->move($destination_path, $filename);

      // save media data into database //
      $profile->about_me = $request->input('about_me');
      $profile->video_mp4 = $request->file('video_mp4');
      $profile->video_ogg = $request->file('video_ogg');
      $profile->save();

      return redirect('/profile')->with('message','You just created your profile!');
   }

文件权限(在Windows PowerShell上)

enter image description here

那么我怎样才能在view上找到正确的路径?

因为它应该是:videos/QvWjJ2_mov_bbb.mp4videos/muWzE9_mov_bbb.ogg

2 个答案:

答案 0 :(得分:0)

试试这样:

<td>{{  url('/') }}{{ YOUR_FILE_PATH_AFTER_PUBLIC }}</td>

<td>{{  url('/') }}{{ YOUR_FILE_PATH_AFTER_PUBLIC }}</td>

例如: 使用契约将路径从控制器发送到视图:

   $path = '/videos/muWzE9_mov_bbb.ogg';

   $path1 = '/videos/QvWjJ2_mov_bbb.mp4';

   return view('VIEW_PATH',compact('path','path1'));

在视图中

<td>{{  url('/') }}{{ $path }}</td>

<td>{{  url('/') }}{{ $path1 }}</td>

答案 1 :(得分:0)

首先保存上传的文件,然后在文件名中添加随机字符串。您似乎没有在任何地方保存此随机文件名,因此以后很难找到这些文件。您可能希望将这些文件名添加到您保存在数据库中的配置文件对象中。

之后,您的控制器可以构建URL

servicesList

然后在视图中

$ogg_path = 'videos/' . $profile->ogg_filename;
$mp4_path = 'videos/' . $profile->mp4_filename;
相关问题