[英]I cant set filename to a generated zip file from remote files and is too slow
我創建了一個網站,該網站允許用戶從遠程站點zip
多個文件,將它們組合成一個zip
文件,然后下載它們。 這部分工作很好。
我遇到的問題:
1)合並多個文件時,生成zip
所需的時間太長
2)我無法將zip
文件的名稱設置為$name
這就是我所擁有的:
<?php
$files = unserialize($_POST['filesend']);
$name = $_POST['name'];
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
foreach($files as $file){
$download_file = file_get_contents($file);
$zip->addFromString(basename($file),$download_file);
}
$zip->close();
function downloadfile ($file, $contenttype = 'application/octet-stream') {
@error_reporting(0);
if (!file_exists($file)) {
header("HTTP/1.1 404 Not Found");
exit;
}
if (isset($_SERVER['HTTP_RANGE'])) $range = $_SERVER['HTTP_RANGE'];
else if ($apache = apache_request_headers()) {
$headers = array();
foreach ($apache as $header => $val) $headers[strtolower($header)] = $val;
if (isset($headers['range'])) $range = $headers['range'];
else $range = FALSE;
} else $range = FALSE;
$filesize = filesize($file);
if ($range) {
$partial = true;
list($param,$range) = explode('=',$range);
if (strtolower(trim($param)) != 'bytes') {
header("HTTP/1.1 400 Invalid Request");
exit;
}
$range = explode(',',$range);
$range = explode('-',$range[0]);
if (count($range) != 2) {
header("HTTP/1.1 400 Invalid Request");
exit;
}
if ($range[0] === '') {
$end = $filesize - 1;
$start = $end - intval($range[0]);
} else if ($range[1] === '') {
$start = intval($range[0]);
$end = $filesize - 1;
} else {
$start = intval($range[0]);
$end = intval($range[1]);
if ($end >= $filesize || (!$start && (!$end || $end == ($filesize - 1)))) $partial = false;
}
$length = $end - $start + 1;
} else $partial = false;
header("Content-Type: $contenttype");
header("Content-Length: $filesize");
header("Content-Disposition: attachment; filename= $name .zip");
header('Accept-Ranges: bytes');
if ($partial) {
header('HTTP/1.1 206 Partial Content');
header("Content-Range: bytes $start-$end/$filesize");
if (!$fp = fopen($file, 'r')) {
header("HTTP/1.1 500 Internal Server Error");
exit;
}
if ($start) fseek($fp,$start);
while ($length) {
$read = ($length > 8192) ? 8192 : $length;
$length -= $read;
print(fread($fp,$read));
}
fclose($fp);
} else readfile($file);
exit;
}
downloadfile ($tmp_file, $contenttype = 'application/octet-stream');
?>
輸入樣例:
$files = array("http://img3.wikia.nocookie.net/__cb20100520131746/logopedia/images/5/5c/Google_logo.png","http://www.logobird.com/wp-content/uploads/2011/03/new-google-chrome-logo.jpg","http://steve-lovelace.com/wordpress/wp-content/uploads/2013/06/google-logo-in-chicago-font.png","http://fc08.deviantart.net/fs70/f/2013/111/d/6/applejack_google_logo__install_guide___by_thepatrollpl-d62gui3.png","http://www.sfweekly.com/binary/fb4a/go.jpg","https://www.google.com/logos/doodles/2014/world-cup-2014-16-5975619640754176.3-hp.gif");
$name = "Google Logo's 33";
要解決您的兩個問題:
1)緩慢的文件處理速度。 我不知道您能幫上忙。 就是這樣。
2)保存名稱。 我將其格式化為一類,只是為了使作業分開。 我發現類在這種情況下效果更好,因為它更易於閱讀。 話雖這么說,因為每個方法都可以使用受保護的$filename
您應該在此處復制所有內容,而不要將這些代碼與類混合在一起而不用將代碼分解出來:
class DownloadFiles
{
protected $filename;
protected $Zipper;
public function Initialize($filename = false)
{
// Set the file name in the construct
$this->filename = ($filename != false)? $filename:"file".date("YmdHis");
// New ZipArchive instance
$this->Zipper = new ZipArchive();
// Drop the filename here
$this->Zipper->open($this->filename, ZipArchive::CREATE);
// Return the method for optional chaining.
return $this;
}
public function AddFiles($array = array())
{
if(!isset($this->Zipper))
return $this;
// This is just if you wanted to also use a string
// separated by commas, it will convert to array
if(!is_array($array) && strpos($array,",") !== false)
$array = explode(",",$array);
// Loop through files (as you have it already)
if(is_array($array) && !empty($array)) {
foreach($array as $url) {
// Add Contents (as you have it)
$this->Zipper->addFromString(basename($url),file_get_contents($url));
}
}
// Close zip file
$this->Zipper->close();
// Return for method chaining
return $this;
}
public function DownloadZip($contenttype = 'application/octet-stream')
{
if(!isset($this->filename))
return $this;
// Nothing really changes in this function except
// you don't need the name as an option in this function
error_reporting(0);
if (!file_exists($this->filename)) {
header("HTTP/1.1 404 Not Found");
exit;
}
$range = false;
if(isset($_SERVER['HTTP_RANGE']))
$range = $_SERVER['HTTP_RANGE'];
elseif($apache = apache_request_headers()) {
$headers = array();
foreach ($apache as $header => $val)
$headers[strtolower($header)] = $val;
$range = (isset($headers['range']))? $headers['range']:false;
}
$filesize = filesize($this->filename);
$partial = false;
if($range) {
$partial = true;
list($param,$range) = explode('=',$range);
if(strtolower(trim($param)) != 'bytes') {
header("HTTP/1.1 400 Invalid Request");
exit;
}
$range = explode(',',$range);
$range = explode('-',$range[0]);
if(count($range) != 2) {
header("HTTP/1.1 400 Invalid Request");
exit;
}
if($range[0] === '') {
$end = $filesize - 1;
$start = $end - intval($range[0]);
}
elseif($range[1] === '') {
$start = intval($range[0]);
$end = $filesize - 1;
}
else {
$start = intval($range[0]);
$end = intval($range[1]);
if($end >= $filesize || (!$start && (!$end || $end == ($filesize - 1))))
$partial = false;
}
$length = $end - $start + 1;
}
header("Content-Type: $contenttype");
header("Content-Length: $filesize");
header('Content-Disposition: attachment; filename='.$this->filename.'.zip');
header('Accept-Ranges: bytes');
if($partial) {
header('HTTP/1.1 206 Partial Content');
header("Content-Range: bytes $start-$end/$filesize");
if(!$fp = fopen($this->filename, 'r')) {
header("HTTP/1.1 500 Internal Server Error");
exit;
}
if($start)
fseek($fp,$start);
while($length) {
$read = ($length > 8192) ? 8192 : $length;
$length -= $read;
print(fread($fp,$read));
}
fclose($fp);
}
else
readfile($this->filename);
exit;
}
}
?>
使用方法:
$files[] = "http://img3.wikia.nocookie.net/__cb20100520131746/logopedia/images/5/5c/Google_logo.png";
$files[] = "http://www.logobird.com/wp-content/uploads/2011/03/new-google-chrome-logo.jpg";
$files[] = "http://steve-lovelace.com/wordpress/wp-content/uploads/2013/06/google-logo-in-chicago-font.png";
$files[] = "http://fc08.deviantart.net/fs70/f/2013/111/d/6/applejack_google_logo__install_guide___by_thepatrollpl-d62gui3.png";
$files[] = "http://www.sfweekly.com/binary/fb4a/go.jpg";
$files[] = "https://www.google.com/logos/doodles/2014/world-cup-2014-16-5975619640754176.3-hp.gif";
$name = "Google Logo's 33";
// Initialize
$ZDownload = new DownloadFiles();
// Run downloader
$ZDownload->Initialize($name)->AddFiles($files)->DownloadZip();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.