dtn51137 2017-12-02 09:22
浏览 161
已采纳

Laravel验证zip文件内容

I want to accept only file which has ".zip" extension. I added a rule mime-type application/zip to control this. But i dont know its content so i need to check zip file's content to avoid unknown files. How can I safely control the contents of a zip file?

I am using https://github.com/Chumper/Zipper to extract and laravel version is 5.5

public function store(Request $request, User $sharingUser) {
        $rules = [
            'patient_case_id'   => 'required|exists:patient_cases,id',
            'file'              => 'required|mimes:zip,rar',
        ];  
}

Uploaded zip file's content tree like this :

xxxx - MAIN FOLDER

|- aaaa.adf

|- bbbb.adf

|- cccc.adf

|- dddd.adf

|- aaaa.ocx

Folder can has only .adf and .ocx files... But i dont know how many files in folder.

note: it is a RESTful API

  • 写回答

2条回答 默认 最新

  • douzong5057 2017-12-02 09:55
    关注

    I would recommend you use PHP League's ZipArchive Adapter instead.

    Create a new validation rule. Set up the rule in the request and then define the logic.

    use League\Flysystem\Filesystem;
    use League\Flysystem\ZipArchive\ZipArchiveAdapter;
    
    $filesystem = new Filesystem(new ZipArchiveAdapter(getrealpath($value)));
    

    In the passes() method, set the file-system up to read the file. I imagine you may need to figure out the path of the file in temporary storage realpath($value).

    Then flesh out the logic for the check. You could loop through the files within like so:

    foreach ($filesystem->listContents() as $object) {
        if (!in_array($object['mimetype'], ['mimetype1', 'mimetype2'])) {
            return false;
        }
    
    }
    return true;
    

    I can't remember if listContents() recursively lists all files, but there may be an option to make it do that, check their documentation. You don't have to use the mimetype comparison, just use whatever makes the most sense.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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