dongyan5239 2018-10-08 16:47
浏览 77
已采纳

在laravel部分工作中验证图像

I knew this question may sound duplicate but am just in a mess.

Iam new to Laravel. coming from php background. I am using the code below to validate image in laravel 5.7.8. it does validation by checking the file extension name, size etc. Now I want to check the mimetype as a way securing the file upload hence the code

        $rules= [
             'file' => 'mimes:jpeg,bmp,png,gif'
        ];
        $x = $request->all();
        $validator=Validator::make($x, $rules);
        if ($validator->passes()){

 Session::flash('message','File Uploads successful.');
        }else{
         Session::flash('message','Invalid File type .');

        }

My problem is that its not returning error message eg Invalid File type when an invalid file is uploaded.

Below is how I added it to my main controller. All other checking are okay. I just want the code to print Invalid File type whenever a file that is not image is being uploaded

<?php

namespace App\Http\Controllers;

use Session;
use Validator;
use Illuminate\Http\Request;

class PictureController extends Controller{

   public function picture(){
     return view('picture');
   }

   public function uploadFile(Request $request){

     if ($request->input('submit') != null ){

        $file = $request->file('file');

        // File Details
        $filename = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension();
        $tempPath = $file->getRealPath();
        $fileSize = $file->getSize();
        $mimeType = $file->getMimeType();

 //dd($mimeType = $file->getMimeType());


        // Valid File Extensions
        $valid_extension = array("jpg","jpeg","png");



//validate files uploads mimetype

        $rules= [
             'file' => 'mimes:jpeg,bmp,png,gif'
        ];
        $x = $request->all();
        $validator=Validator::make($x, $rules);





 // 2MB in Bytes
 $maxFileSize = 2097152;

        // Check file extension
        if(in_array(strtolower($extension),$valid_extension)){

      // validate mimetype
        if ($validator->passes()){

          // Check file size
          if($fileSize <= $maxFileSize){

             // File upload location
             $location = 'images';

             // Upload file
             $file->move($location,$filename);

             Session::flash('message','Upload Successful.');
          }else{
             Session::flash('message','File too large. File must be less than 2MB.');
          }
//validator starts
}else{
 Session::flash('message','Invalid File type');
}
//validator ends
        }else{
           Session::flash('message','Invalid File Extension1.');
        }

      }

      // Redirect to index
      return redirect()->action('PictureController@picture');
   }
}
  • 写回答

2条回答 默认 最新

  • dongshi9526 2018-10-13 13:38
    关注

    My problem with this is the way I was returning the session flash response call. If I echo the response message with dd() function as per dd(....) it works hence this works now. Since I was working with angularjs, I now return the server response as json call and it works fine... Thanks

    $rules= [
                 'file' => 'mimes:jpeg,bmp,png,gif'
            ];
            $x = $request->all();
            $validator=Validator::make($x, $rules);
            if ($validator->passes()){
    
    dd('File Uploads successful.');
            }else{
             dd('Invalid File type .');
    
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看