PHP readfile download timing out -
my php script keeps crashing/timing out, presumably because of readfile. goal generate zip, let user download it, , remove zip afterwards.
code:
<?php if(isset($_post['version']) && isset($_post['items']) && isset($_post['identifier'])) { $identifier = $_post['identifier']; $version = $_post['version']; $tmp = dirname(__file__) . "/download/"; $zipfile = $tmp . $identifier . ".zip"; $name = "program v" . $version . ".zip"; $path = dirname(__file__) . "\\download\\template\\" . $version; $files = new recursiveiteratoriterator( new recursivedirectoryiterator($path), recursiveiteratoriterator::leaves_only ); $zip = new ziparchive(); if ($zip->open($zipfile, ziparchive::create | ziparchive::overwrite) === true) { foreach ($files $name => $file) { // skip directories (they added automatically) if (!$file->isdir()) { // real , relative path current file $filepath = $file->getrealpath(); $relativepath = substr($filepath, strlen($path) + 1); //echo "adding " . $file . " " . $relativepath; // add current file archive $zip->addfile($file, $relativepath); } } $zip->close(); } else { die('error: unable create zip file'); } // stream file client header('content-type: application/zip'); header('content-length: ' . filesize($zipfile)); header('content-disposition: attachment; filename="'.$name.'"'); readfile($zipfile); exit; }
everything works until download part. zip file gets generated fine on server, when trying download it, crashes.
sometimes shows error in logs, telling me expects valid path , object given. though strictly puts out string (the path).
note: file less 300 kib highly doubt webserver running out of memory.
i'm pretty lost, highly appreciated.
the problem generation of zip file. addfile
method requires file path first parameter, not object. see documentation:
bool ziparchive::addfile ( string $filename [, string $localname = null [, int $start = 0 [, int $length = 0 ]]] )
http://php.net/manual/en/ziparchive.addfile.php
therefore in case correct expression be:
$zip->addfile($filepath, $relativepath);
Comments
Post a Comment