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 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误