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

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法