Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我无法将文件名设置为从远程文件生成的zip文件,速度太慢_Php_Html_Zip_Simple Html Dom_File Management - Fatal编程技术网

Php 我无法将文件名设置为从远程文件生成的zip文件,速度太慢

Php 我无法将文件名设置为从远程文件生成的zip文件,速度太慢,php,html,zip,simple-html-dom,file-management,Php,Html,Zip,Simple Html Dom,File Management,我创建了一个网站,允许用户从远程站点下载多个文件,将它们组合成一个zip文件,然后下载它们。这部分工作得很好 我遇到的问题: $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","h

我创建了一个网站,允许用户从远程站点下载多个文件,将它们组合成一个
zip
文件,然后下载它们。这部分工作得很好

我遇到的问题:

$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";
$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();
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');
?>

要解决两个问题:

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 = 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";
$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();

是的,我不知道,因为文件必须先下载,这取决于与服务器之间的连接速度,所以我不知道你会对此产生多大影响。我尝试了一系列的代码,它有不同的速度,有些慢一些快一些。是否有可能通过获取标题??或者我可以在生成zip时显示进度条