Get directory structure of extracted zip file using PHP

I am working on a bundling system for my applications where a user can download a compressed zip file with the following structure:

name_of_file.zip
  src/
    file.jar
  thumbnail.png
  meta.json

      

I would like to test this structure after extraction with PHP. After the structure is checked, I will move thumbnail.png and /src/source.jar to my server. I will also parse meta.json and return data from it.

So this is what my class looks like so far ($ this-> archive is a ZipArchive instance):

/**
 * Process and parse a bundle.
 * 
 * @param  \UploadedFile  $zip
 * @param  array $paths
 * 
 * @return boolean
 */
private function bundle($zip, array $paths)
{
   $status = $this->archive->open($zip->getRealPath());

    if($status === false)
        return false;

    // Before getting here I need to verify the files exist...

    $this->archive->extractTo($paths['software'], ['src/file.jar']);
    $this->archive->extractTo($paths['thumbnail'], ['thumbnail.png']);

    // Cool, files moved now read the meta.json file...
}

      

and if you guessed it already, it is laravel.

I just want to check the structure right now. Once I'm done with the structure, I will need to do some simple file validation (some tips for getting file extensions?).

At the end of this file, file.jar and thumbnail.png will be moved to their locations, and meta.json will be parsed and returned from it (the file should not be moved).

+3


source to share


1 answer


Getting the extension of the downloaded file

$extension = Input::file('photo')->getClientOriginalExtension();

      

Source: http://laravel.com/docs/4.2/requests



or

$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);

      

Source: How to get file extension in PHP?

-1


source







All Articles