Zipper icon indicating copy to clipboard operation
Zipper copied to clipboard

subdirectories

Open wyklif opened this issue 9 years ago • 7 comments

hello i have an issue whereby i use code to create a folder with directories in it and each of this directories have files in them i can confirm the file structure after php creates the folder, however after i make a zip file and on inspecting the zip file i realise it is just one parrent folder with files in it the sub folders are ignored during zip creation

wyklif avatar Jan 07 '17 05:01 wyklif

I have noticed this as well.

itwasmattgregg avatar Jan 17 '17 20:01 itwasmattgregg

What do you do to add the files? can you show some code?

Chumper avatar Jan 18 '17 09:01 Chumper

//basiclally the $files is an array that can contain arrays like this one

`$files[$key][] = 'somedir/'.$somekey.'/somedir2/'.$omekey2.'/'.$somefile


    private function s3Download($files,$coupon)
    {

        

        //declare a base directory for zip files
          
                $dir_path = public_path().DIRECTORY_SEPARATOR.'downs'.DIRECTORY_SEPARATOR.$coupon.DIRECTORY_SEPARATOR;
               
                
                //iterate through the $files  multidimensional array creating a folder structure based on the "top level" key as top level folder name
               foreach ($files as $name=>$thefile) {
                      if (is_array($thefile)) {
                          foreach ($thefile as $k=>$file) {
                           $base_dir = $name;
                           $folder = $dir_path.$base_dir;
                            
                            if (!is_dir($folder)) {
                                    File::makeDirectory($folder,  $mode = 0775, $recursive = true);
                                }
                           
                               //download the file from s3 and save it in dir structure created
                                    $save_path = $folder.DIRECTORY_SEPARATOR.$original_name;
                                    if (!file_exists($save_path)) {
                                            $s3 = AWS::createClient('s3');
                                            try{ 
                                            $result = $s3->getObject(array(
                                                'Bucket'     => mybucket,
                                                'Key'        => $key,
                                                'SaveAs'  => $save_path,
                                            ));
                                            
                                            } catch (S3Exception $e) {
                                                return $e->getMessage() . "\n";
                                            }
                                    }

                                    
                          }
                      }
               }

               $download = $this->makeZip($dir_path,$coupon);

              
    }


    private function makeZip($filespath,$coupon)
    {
           $files = glob($filespath.'*');
        // zip it 
            $zip = 'downs'.DIRECTORY_SEPARATOR.$coupon.'.zip';
           

            $path = public_path().DIRECTORY_SEPARATOR.$zip;
            Zipper::make($path)->add($files);
            while (!$this->checkFile($path)) {
                if ($this->checkFile($path)) {
                    break;
                }
                
                Zipper::make($path)->add($files);
            }
            $this->remove_dir($filespath);
            
            $headers = array(
              'Content-Type: application/octet-stream',
            );
           

   
            
     
           return Response::download($path,$coupon.'.zip',$headers);

            

    }

wyklif avatar Jan 18 '17 12:01 wyklif

if you have any trouble reading the above code i would be glad to clarify

wyklif avatar Jan 25 '17 08:01 wyklif

This code looks fine, I need to write a test and investigate further.

Chumper avatar Jan 30 '17 11:01 Chumper

Hi @Chumper though i have not tested the solution i think tis issue is caused by the '/*' in the glob function

wyklif avatar Feb 10 '17 09:02 wyklif

Nope seems that was not the issue

wyklif avatar Feb 13 '17 09:02 wyklif