简体   繁体   中英

How to rename and save Unzip file in php

I am trying to unzip an uploaded file and rename and upload it in my server

here is my code

PHP

$fname = md5(rand()).'.csv';
                $full_path = Config::get('filesystems.disks.local.root');


                $zip = new ZipArchive;     

                if ($zip->open($excel_file) === TRUE) 
                {

                        $zip->renameName($zip->getNameIndex(0),$fname);
                        $zip->extractTo($full_path. '/exceluploads/');
                        $zip->close();
                } else {

                        return redirect()->back()->withErrors('File is not zipped');
                }

But this is not working , I am sure I am doing some mistake since I am using the ZipArchive for the first time .

You are changing the zip file's content, so you need to specify what you want to extract.

<?php
$fname = md5(rand()).'.csv';
$full_path = Config::get('filesystems.disks.local.root');

$zip = new ZipArchive;

if ($zip->open($excel_file) === TRUE) {
    $zip->renameName($zip->getNameIndex(0), $fname);
    // Please notice the $fname, passed as a parameter for extractTo
    $zip->extractTo($full_path . 'exceluploads/', $fname);
    $zip->close();
} else {
    return redirect()->back()->withErrors('File is not zipped');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM