0

What is the best way to download a GitHub repo's files using PHP into a directory.

I have tried downloading the zipball and extracting it but I'm struggling to find a good way of extracting the zip file.

Jamesking56
  • 3,683
  • 5
  • 30
  • 61

2 Answers2

1

Dependency management

Use composer.

Composer is a dependency management tool for PHP (similar to node's npm). Please please please go to http://getcomposer.org/ and read up on composer if you don't already use it.

Programmatic downloading and unzipping

Since you've already mentioned you've been able to download the zip file i'll focus on extracting it. If you're using composer (like you should) you can search through the package repository at https://packagist.org. Doing a quick search for zip gave me a bunch of results from full featured archive management libraries to simple unzip utilities. This one seems pretty simple to use and would likely do the trick.

// https://github.com/vipsoft/Unzip/blob/master/README.md

use VIPSoft\Unzip\Unzip;

$unzipper  = new Unzip();
$filenames = $unzipper->extract($zipFilePath, $extractToThisDir);

Programmatic downloading and unzipping in PHP 5.2.6

Thanks to this question I learned about PHP's zip extension which was introduced in PHP 5.2.0.

It looks like using zip would follow something similar to the following code

$zip = new ZipArchive;
if (true === $zip->open($archiveFileName)) {
    $zip->extractTo($destination);
    $zip->close();
} else {
    // handle error
}

Hopefully your client isn't against having the php zip extension installed too.

Community
  • 1
  • 1
Will
  • 7,225
  • 2
  • 23
  • 14
0

Just download the zipball and use something like this, or a command-line tool such as unzip (exists on most systems).

Oscar Broman
  • 1,109
  • 8
  • 19