dongyo7931 2016-11-30 06:16
浏览 80

我如何允许通过laravel中的文件上传来放置音频和音乐

I would like to allow the audio,doc and video file type in uploading file. My question is, how should I allow the audio,doc and video through file upload? In my code the file that allows are :png,gif,jpeg,txt,pdf,docx. This is my code

My controller

<?php 
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
use Validator;
use Redirect;
use Request;
use Session;
use App\Administrator;
class ApplyController extends Controller 
{
  public function multipleUpload()
  {
    return view('multiple_upload');
  }
  public function multipleUploadPost() {
    // getting all of the post data
    $files = Input::file('images');
    // Making counting of uploaded images
    $file_count = count($files);
    // start count how many uploaded
    $uploadcount = 0;
    foreach($files as $file) {
      $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,docx' 
      $validator = Validator::make(array('file'=> $file), $rules);

      if($validator->passes()){
        $destinationPath = 'uploads';
        $filename = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension()  ;
        if($extension == "jpg" || $extension == "PNG" )
        {
          $destinationPath = 'uploads/image';

        }
        else if ($extension == "docx" || $extension == "doc")
        {
          $destinationPath = 'uploads/documents';

        }
        else if ($extension == "pdf")
        {
          $destinationPath = 'uploads/pdf';

        }
        else if ($extension == "xls")
        {
          $destinationPath = 'uploads/excel';

        }
        $changeFileName = str_random(40).'.'.$extension;

        $upload_success = $file->move($destinationPath, $changeFileName);
        $uploadcount ++;
        Administrator::create([ // Administrator is the name of my model
            'original_filename' => $filename, // Insert the image to the database in column "image"
            'change_filename' => $changeFileName // Insert the image to the database in column "image"
        ]);
      }

    }
    if($uploadcount == $file_count){
      Session::flash('success', 'Upload successfully'); 
      return Redirect::to('multiple-upload');
    } 
    else {
      Session::flash('alert', 'Upload failed'); 

      return Redirect::to('multiple-upload')->withInput()->withErrors($validator);
    }
  }
}

My view

<div class="text-content">
  <div class="span7 offset1">
  @if(Session::has('success'))
    <div class="alert-box success">
      <h2>{!! Session::get('success') !!}</h2>
    </div>
  @endif
  <div class="secure">Upload form</div>
  {!! Form::open(array('url'=>'apply/multiple_upload','method'=>'POST', 'files'=>true)) !!}

    <div class="control-group">
      <div class="controls">
      {!! Form::file('images[]', array('multiple'=>true)) !!}
  <p class="errors">{!!$errors->first('images')!!}</p>
  @if(Session::has('error'))
  <p class="errors">{!! Session::get('error') !!}</p>
  @endif
     </div>
</div>
{!! Form::submit('Submit', array('class'=>'send-btn')) !!}
{!! Form::close() !!}
</div>
  • 写回答

1条回答 默认 最新

  • dsgk40568 2016-11-30 06:22
    关注

    use Mime validation which will allow only those format which you specify

    for ex:

        foreach ($files as $file){
            $validator = Validator::make(
                array(
                    'file'              =>      $file,
                    'extension'         =>      strtolower($file->getClientOriginalExtension()),
                ),
                [
                    'file'              =>      'required|max:100000',
                    'extension'         =>      'required|in:mp3,mp4,doc',
                ],[
                    'file.required' => 'File is Required',
                    'extension.in' => ' File Should be of type mp3,mp4 or doc'
                ]
            );
            if($validator->fails()){
                return response()->json(['status' => 'fail', 'data' => null, 'msg' => $validator->errors()], 200);
            }
            else{
                dd("success");
            }
        }
    

    List of mime types Available

    http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

    Hope this might help you

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度