Adakalanya kita membutuhkan script untuk mendowload beberapa file menjadi satu file zip untuk mempermudah dalam melakukan download pada halaman website.
Berikut cara membuat script untuk proses pengabungan file dari SFTP menjadi satu file zip sebelum memulai pastikan library phpseclib ada di folder project website jika tidak silahkan download disini :
<?php
// set location phpseclib
set_include_path(get_include_path().PATH_SEPARATOR.'./phpseclib0.3.0');
include("Net/SFTP.php");
// connection SFTP
$sftp = new Net_SFTP('ip_address');
if (!$sftp->login('user', 'password'))
{
die("Cannot connect");
}
// Location directory
$path = "url_address";
$list = $sftp->nlist($path);
// Check directory
if ($list === false)
{
die("Error listing directory ".$path);
}
$file_name = "file_name.zip";
$matches = preg_grep("/^$file_name.*/i", $list);
// Check file
if (count($matches) != 1)
{
die("No file or more than one file matches the pattern: ".implode(",", $matches));
}
$matches = array_values($matches);
$filename = $matches[0];
$filepath = $path."/".$filename;
if (!$sftp->get($filepath, $filename))
{
die("Error downloading file ".$filepath);
}
$file = ($file_name);
$filetype=filetype($file);
$filename=basename($file);
header ("Content-Type: ".$filetype);
header ("Content-Length: ".filesize($file));
header ("Content-Disposition: attachment; filename=".$filename);
readfile($file);
unlink($file_name);
?>
Selamat mencoba, terima kasih