简体   繁体   中英

Unzip a file until i get a JPG,JPEG/PNG

This is my function to unzip files. Now it is working for this, if i have a zip file contains only images and it is not working for if i have a zipped file with a folder contain images

public function uploadZipImages($Orderpid, $OrderId)
{
    $valid_image_type = array('jpg,png');
    $valid_zip_type   = array('zip');
    $folder = DIR_WS_IMAGES_ORDERS . $OrderId . '/block_images';

    (isset($_POST['clearImages']) && ($_POST['clearImages'] == 1 || $_POST['clearImages'] == '1')) ? (exec('rm -rf '.$folder.'/*')) : (true);
    (!is_dir($folder)) ? (mkdir($folder, 0777)) : (true);

    foreach ($_POST['bulk_images'] as $block)
    {
        $total_count     = count($block);
        $zipimagename    = $block;
        $extension= substr(strrchr($zipimagename,'.'),1);
        for($i = 0; $i < $total_count; $i++)
        {
            $str_filename = preg_replace("[^A-Za-z0-9.-]", "_", $zipimagename);
            $file_name = $folder . '/' . $str_filename;
                move_uploaded_file(DIR_WS_IMAGES_ORDERS.$OrderId.'/'.$zipimagename, $file_name);
                 if (in_array($extension, $valid_image_type) || in_array($extension, $valid_zip_type))
                {   
                    if (in_array($extension, $valid_zip_type))
                    {
                        exec('unzip ' . DIR_WS_IMAGES_ORDERS.$OrderId.'/'.$zipimagename . ' -d ' . $folder . '/');
                        unlink($file_name);

                    }
                }
        }
    }
}

I know i have to change somewhere here exec('unzip ' . DIR_WS_IMAGES_ORDERS.$OrderId.'/'.$zipimagename . ' -d ' . $folder . '/'); By checking like to unzip until i found any jp,jpeg/png. How to do this?

您可以尝试添加参数-j进行解压缩

exec('unzip ' . DIR_WS_IMAGES_ORDERS.$OrderId.'/'.$zipimagename . ' -j -d ' . $folder . '/');

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