3

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

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

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

So here's what my class looks like so far ($this->archive is an instance of ZipArchive):

/**
 * 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 already guessed it, it's laravel.

I'm only looking to validate structure right now. Once I'm done with the structure I'll need to do some simple file validation (some tips on getting the file extensions?).

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

davidxd33
  • 1,186
  • 4
  • 22
  • 38
  • So what is the exact question? You need to know for sure that you have 2 files with those exact two names and a directory named `src` with the `fire.jar` in it? – ziGi Dec 16 '14 at 00:49
  • I need to make sure each zip contains a /src/file.jar, thumbnail.png, and a meta.json – davidxd33 Dec 16 '14 at 00:54
  • Well I guess just use `$this->archive->locateName('fire.jar')` etc. to check those 3 files. Reference here: http://php.net/manual/en/ziparchive.locatename.php – ziGi Dec 16 '14 at 01:00
  • Would it hurt if I told you that the file name for file.jar won't be the same each time? So that makes looking it up like that a no-no. – davidxd33 Dec 16 '14 at 01:02
  • Well that is why I asked if the file names are going to be the same explicitly. What I can think of, is that you can extract the zip in a temp folder and check whether the file exists using `glob()` with wildcards. Example: `$files = glob('src/*.jar');` and after you finish the validation just wipe out the temp dir. Of course validate the file size before doing such things. – ziGi Dec 16 '14 at 01:05
  • That's what I had in mind, just wasn't too hype about generating temp directories and files. – davidxd33 Dec 16 '14 at 01:08
  • Well there is another way, you can use `getFromIndex()` to get the first 3 files in the zip and then check their names but that limits the users to have exactly that structure and no other, otherwise it will not work, because it is a bit of guessing, so I don't recommend it. With `locateName()` it is possible to skip case check and directory check but unfortunately you can't do wildcard guessing. – ziGi Dec 16 '14 at 01:11
  • I'd choose the temp directory approach. Just make sure you clean up afterwards. – lukasgeiter Dec 16 '14 at 07:46

1 Answers1

-1

Retrieving The Extension Of An Uploaded 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 the file extension in PHP?

Community
  • 1
  • 1
Eduardo Cruz
  • 617
  • 6
  • 18