search
HomeBackend DevelopmentPHP Tutorial上载有关问题,小文件上载正常,上载大于100M的文件,进度条到100M以外的时候,进度条就不走了,代码什么地方出有关问题了

下载问题,小文件下载正常,下载大于100M的文件,进度条到100M以外的时候,进度条就不走了,代码什么地方出问题了?

本帖最后由 bxline 于 2012-12-17 06:31:43 编辑 我用的是下面的代码,我试验了两回一次下载到145M不走了,一次下载到129M就不走了
,这是怎么回事?小文件下载正常,就是大于100M的文件不正常
下面是代码:我是03+FASTCGI,不是apache,所以代码中有的行给注释掉了

if(file_exists($filename)){
$mtime = ($mtime = filemtime($filename)) ? $mtime : gmtime(); 
$size = intval(sprintf("%u", filesize($filename))); 
#if (intval($size + 1) > return_bytes(ini_get('memory_limit')) && intval($size * 1.5) #  ini_set('memory_limit', intval($size * 1.5)); 
#} 
[email protected]_setenv('no-gzip', 1); 
[email protected]_set('zlib.output_compression', 0);
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($filename));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header("Content-Length: " . $size); 
set_time_limit(300); 
$chunksize = 1 * (1024 * 1024);
 //   ob_clean();
   // flush();
if ($size > $chunksize) { 
    $handle = fopen($filename, 'rb'); 
    $buffer = ''; 
  while (!feof($handle)) { 
    $buffer = fread($handle, $chunksize); 
    echo $buffer; 
    ob_flush(); 
    flush(); 
  } 
  fclose($handle); 
} else { 
ob_flush(); 
    flush(); 
    readfile($filename); 

  exit;
}
------解决方案--------------------
echo 100 * 1024 / 300;
得 341 K/s
即需要平均带宽为 3M 以上

你的下载代码不具有断点续传能力,一旦出现传输错误,即可终止
------解决方案--------------------
我该写了一下,你看看
<br>Function down($file){<br>    IF(!$file Or !File_Exists($file) Or !Is_Readable($file)) {Return False;}<br><br>    IF($f = atr($file)) {<br>        Header("Content-Length:{$f['size']}");<br>        Header('Content-Description:File Transfer');<br>        Header('Content-Transfer-Encoding:binary');<br>        Header('Expires:0');<br>        Header('Cache-Control:must-revalidate');<br>        Header('Pragma:public');<br> <br>        IF(Function_exists("mime_content_type")) {<br>            Header("Content-Type: ".mime_content_type($f['name']));<br>        }Else{<br>            Header("Content-Type: application/octet-stream");<div class="clear">
                 
              
              
        
            </div>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools