如何在Laravel 5.4中验证文件名

时间:2017-09-26 07:00:26

标签: php regex laravel laravel-5 laravel-5.4

我有一个包含三个输入字段的表单。我想在处理它们之前验证输入值。我想在处理之前验证文件名的位置。我使用正则表达式和alpha_dash。但是我收到了有效文件名的错误。我希望我的文件名只包含小写字母,数字,下划线和短划线。如何检查文件的文件名验证?

HTML

 <form action="create" method="POST"  enctype="multipart/form-data">
          {{csrf_field()}}
      <table cellpadding="2" width="20%"  align="center"
         cellspacing="2">

         <tr>
           <td colspan=2>
           <center><font size=4><b>Add the iteams  please</b></font></center>
           </td>
         </tr>

         <tr>
           <td>Heading</td>
           <td><input type="text" name="heading" id="heading" size="30">
           {!! $errors->first('heading', '<p class="red">:message</p>') !!}
           </td>
         </tr>

         <tr>
           <td>Image</td>
           <td><input type="file" name="image" id="image" size="40">
           {!! $errors->first('image', '<p class="red">:message</p>') !!}
           </td>
         </tr>

         <tr>
           <td></td>
           <td colspan="2"><input type="submit" name="submit" value="Add Item" /></td>
         </tr>
     </table>
 </form>

控制器部分

  1. 使用正则表达式格式:
  2. - 我收到错误消息“图片格式无效”。

     public function store(){
        $this->validate(request(),[
              'heading'=>'required',
              'contentbody'=>'required',
               ‘image'=>['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048','regex:/^[a-z0-9-_]+$/' ]
    
            ]);
    
    }
    
    1. 使用Alpa_dash:
    2. - 我收到错误消息“图片可能只包含字母,数字和短划线”。

      public function store(){
       $this->validate(request(),[
                 'heading'=>'required',
                'contentbody'=>'required',
                'image'=>'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048|alpha_dash'
      }
      

      请帮忙, 谢谢!

3 个答案:

答案 0 :(得分:2)

  1. 创建自定义验证规则类,例如Filename,并将其放在路径app/Rules/Filename.php上。
<?php
namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class Filename implements Rule
{
    protected $regex;

    public function __construct(string $regex)
    {
        $this->regex = $regex;
    }

    public function passes($attribute, $value)
    {
        if (!($value instanceof UploadedFile) || !$value->isValid()) {
            return false;
        }

        return preg_match($this->regex, $value->getClientOriginalName()) > 0;
    }

    public function message()
    {
        return 'The :attribute name is invalid.';
    }
}
  1. 在您的请求规则中,添加以下自定义规则:
use App\Rules\Filename;

public function store() {
    $this->validate(request(), [
        'heading' => 'required',
        'contentbody' => 'required',
        'image' => [
            'required',
            'image',
            'mimes:jpeg,png,jpg,gif,svg',
            'max:2048',
            new Filename('/^[a-z0-9-_]+$/'), // This is your custom rule
        ],
    ]);
}

答案 1 :(得分:1)

你需要使用我认为的自定义正则表达式

^[a-z0-9_.-]*$

并在您的验证中使用

public function store(){
    $this->validate(request(),[
          'heading'=>'required',
          'contentbody'=>'required',
           ‘image'=>'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048|regex:/^[a-z0-9_.-]*$/'

        ]);

编辑2:

我不确定但是根据文档,laravel仅支持上述格式。然而,当我使用图像验证时,我发现jpg没有问题但仍然允许这样做..

docs

编辑3

自定义验证请参阅here

答案 2 :(得分:0)

如果其他人有像我这样的问题。我通过将文件名更改为当前时间戳而不是使用原始文件名解决了我的问题。这样,我就不必担心要保存在数据库中的原始文件名的验证。

 public function store(Request $request)
    {
        $this->validate($request,[
          'heading'=>'required',
          'contentbody'=>'required',
          'image'=>['required','image','mimes:jpg,png,jpeg,gif,svg','max:2048']

        ]);

         if($request->hasFile('image')){

            $inputimagename= time().'.'.$request->file('image')->getClientOriginalExtension();
            $request->image->storeAs('public/upload', $inputimagename);

          Post::create([
             'heading'=>request('heading'),
             'content'=>request('contentbody'),
               'image'=>$inputimagename,

            ]);


         }

       return redirect('/home');
    }