如何在Laravel 5.4中验证文件名

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. 使用正则表达式格式:

- 我收到了错误信息:“图像格式无效”。

 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:

- 我收到了错误消息:“图像只能包含字母,数字和破折号”。

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

Please help, Thank you!

3个回答

9
  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

你需要使用自定义正则表达式

^[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没有问题,但仍然可以排除它。

文档

编辑3

有关自定义验证,请参见此处


好的 @ Mr. Pyramid - Jona
请再次检查答案,根据文档,您可能会因为 JPG 文件而遇到此问题。 - Mr. Pyramid
谢谢。但我收到了相同的错误。是否可以为此进行自定义验证?我不知道如何在Laravel中进行自定义验证。 - Jona
是的,你可以查看我在答案中提供的参考资料。 - Mr. Pyramid
我尝试使用自定义验证,但是我无法将原始文件名传递为它本身。文件名被更改为另一个名称。 - Jona
显示剩余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');
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接