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.
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.
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.
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);
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.
Just download the zipball and use something like this, or a command-line tool such as unzip
(exists on most systems).