简体   繁体   中英

Download repo from GitHub in PHP?

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.

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.

只需下载zipball并使用类似这样的东西,或使用命令行工具(例如unzip (存在于大多数系统上)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM