我在 API 中运行 PHP ZipArchive,它在adding: uploads/AUFIC/ps.csv (stored 0%)
运行时像响应正文一样输出,如何禁用此输出打印?
这是包含 ZipArchive 的函数,它排列一些文件,然后压缩这些文件并将其上传到 S3,然后返回该 zip 文件的 URL:
private function downloadPackage($schoolPrefix,$schoolZipPassword,$csvFilePath) {
$ret = array('status' => false, "message" => "", "response" => array());
try {
$s3 = Aws\S3\S3Client::factory(array('region' => getenv('omitted_AWS_REGION_2')));
$s3->registerStreamWrapper();
$bucket = 'autoregistration.omitted.com';
// production needs to be read from env var
$prefix = 'production/PackageFiles/';
$objects = $s3->getIterator('ListObjects', array(
'Bucket' => $bucket,
'Prefix' => $prefix
));
$zip = new ZipArchive;
$zipfilename = $schoolPrefix.'_omitted'.date('YmdHis').'.zip';
$zipfilepath = 'uploads/'.$zipfilename;
$zip->open($zipfilepath, ZipArchive::CREATE);
foreach ($objects as $object) {
if(basename($object['Key']) != 'PackageFiles')
{
$contents = file_get_contents("s3://{$bucket}/{$object['Key']}");
$objectname = (basename($object['Key']) == 'SchoolPrefix.ps1') ? $schoolPrefix.'.ps1' : basename($object['Key']);
$zip->addFromString($objectname, $contents);
}
}
// pass the school prefix in params
$school_objects = $s3->getIterator('ListObjects', array(
'Bucket' => $bucket,
'Prefix' => 'production/'.$schoolPrefix
));
foreach ($school_objects as $school_object) {
if(basename($school_object['Key']) == 'ps.csv')
{
$contents = file_get_contents("s3://{$bucket}/{$school_object['Key']}");
$zip->addFromString(basename($school_object['Key']), $contents);
$foundcsv = true;
}
}
if(!$foundcsv) throw new Exception ('Can not find the csv file.', 400);
$zip->close();
$output = system("zip -P '".$schoolZipPassword."' ". $zipfilepath . " ".$csvFilePath);
$key = 'production/'.$schoolPrefix.'/'.$zipfilename;
$s3result = $s3->putObject(array('Bucket'=>$bucket,'Key' => $key, 'SourceFile' => $zipfilepath));
//$ret['s3_url'] = $s3result['ObjectURL'];
$ret['s3_url'] = $s3->getObjectUrl($bucket, $key, '+2 days');
$ret['status'] = true;
unlink($zipfilepath);
} catch (Exception $e) {
$ret['status'] = false;
$ret['message'] = $e->getMessage();
}
return $ret;
}