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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?