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条)

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了