Search code examples
phpphp-ziparchive

PHP ZipArchive, disable outputs/echos/logs


I am running PHP ZipArchive in an API and it outputs like adding: uploads/AUFIC/ps.csv (stored 0%) in the body of the response when it runs, how can I disable this output from getting printed?

Here's the function that contains ZipArchive, it arranges some files around then zips those files and upload it to S3 then return the URL of that zip file:

    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;
}

Solution

  • I would start by changing this:

    return $ret;
    

    To this:

    return;