2013-04-07 77 views
2

我真的被它困住了。我做了一個如下的函數(一個wp插件)。這處理下載請求。下載簡歷和zip文件問題 - PHP

下載URL格式是這樣的:

http://mysite.com/?download=2f547re8w9qasd547g8tr52e15469879w

文件會在瀏覽器中保存如常(與彈出「另存爲...」框)。

的問題是:

1 -Orbit嘗試下載器下載頁面!和IDM只是下載一個整個文件大小的低百分比(例如7%),然後通過恢復停止&,它從第一個開始下載。 (瀏覽器完全下載文件並恢復正常)

2 -當下載.zip文件(通過瀏覽器)時,出現「意外的歸檔結束」錯誤。 (CRC32中的zip文件的詳細信息顯示爲0個字符

function save_file() 
{ 
    global $wpdb; 
    global $uid; 
    global $each_download; 
    $hash = @$_GET["download"]; 
    if(preg_match('/^[a-z0-9]{32}$/i',$hash)) 
    { 
     if($row = $wpdb->get_row("SELECT * FROM wp_dlurl WHERE hash = '{$hash}'",ARRAY_A)) 
     { 
      if(is_user_logged_in()) 
      { 
       if($row['price'] != 0) 
        $each_download = $row['price']; 
       if(get_user_meta($uid, 'revo_credits', true) >= $each_download) 
       { 
        $parts = pathinfo($row['url']); 
        $url = $parts['dirname'] . '/' . urlencode($parts['basename']); 
        $file = pathinfo($row['filename']); 
        $ext = $file['extension']; 
        /* List of File Types */ 
        $fileTypes['swf'] = 'application/x-shockwave-flash'; 
        $fileTypes['pdf'] = 'application/pdf'; 
        $fileTypes['exe'] = 'application/octet-stream'; 
        $fileTypes['zip'] = 'application/zip'; 
        $fileTypes['doc'] = 'application/msword'; 
        $fileTypes['xls'] = 'application/vnd.ms-excel'; 
        $fileTypes['ppt'] = 'application/vnd.ms-powerpoint'; 
        $fileTypes['gif'] = 'image/gif'; 
        $fileTypes['png'] = 'image/png'; 
        $fileTypes['jpeg'] = 'image/jpg'; 
        $fileTypes['jpg'] = 'image/jpg'; 
        $fileTypes['rar'] = 'application/rar';  

        $fileTypes['ra'] = 'audio/x-pn-realaudio'; 
        $fileTypes['ram'] = 'audio/x-pn-realaudio'; 
        $fileTypes['ogg'] = 'audio/x-pn-realaudio'; 

        $fileTypes['wav'] = 'video/x-msvideo'; 
        $fileTypes['wmv'] = 'video/x-msvideo'; 
        $fileTypes['avi'] = 'video/x-msvideo'; 
        $fileTypes['asf'] = 'video/x-msvideo'; 
        $fileTypes['divx'] = 'video/x-msvideo'; 

        $fileTypes['mp3'] = 'audio/mpeg'; 
        $fileTypes['mp4'] = 'audio/mpeg'; 
        $fileTypes['mpeg'] = 'video/mpeg'; 
        $fileTypes['mpg'] = 'video/mpeg'; 
        $fileTypes['mpe'] = 'video/mpeg'; 
        $fileTypes['mov'] = 'video/quicktime'; 
        $fileTypes['swf'] = 'video/quicktime'; 
        $fileTypes['3gp'] = 'video/quicktime'; 
        $fileTypes['m4a'] = 'video/quicktime'; 
        $fileTypes['aac'] = 'video/quicktime'; 
        $fileTypes['m3u'] = 'video/quicktime'; 

        $contentType = $fileTypes[$ext]; 
        //ob_end_clean(); 
        header("Cache-Control: public"); 
        header('Content-Type: $contentType'); 
        header("Content-Transfer-Encoding: binary"); 
        $new_name = $row['filename'];//rand(1000,999999).".".$ext; 
        $contentDisposition = 'attachment'; 
        if(strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) 
        { 
         $new_name = preg_replace('/\./', '%2e', $new_name, substr_count($new_name,'.') - 1); 
        } 
        $new_name = urlencode($new_name); 
        header("Content-Disposition: $contentDisposition; filename=\"$new_name\""); 
        header("Accept-Ranges: bytes"); 
        $range = 0; 
        $size = $row['size']; 
        if(isset($_SERVER['HTTP_RANGE'])) 
        { 
         list($a, $range) = explode("=",$_SERVER['HTTP_RANGE']); 
         str_replace($range, "-", $range); 
         $size2 = $size-1; 
         $new_length = $size-$range; 
         header("HTTP/1.1 206 Partial Content"); 
         header("Content-Length: $new_length"); 
         header("Content-Range: bytes $range$size2/$size"); 
        } 
        else 
        { 
         update_user_meta($uid, 'revo_credits', get_user_meta($uid, 'revo_credits', true)-$each_download); 
         $size2 = $size-1; 
         header("Content-Range: bytes 0-$size2/$size"); 
         header("Content-Length: ".$size); 
        } 
        if ($size == 0) 
        { 
         showMessage('aborted. zero file size'); 
        } 
        set_magic_quotes_runtime(0); 
        $maxSpeed = 200;    
        $fp = fopen($url,"rb"); 
        fseek($fp,$range); 
        while(!feof($fp) and (connection_status()==0)) 
        { 
         set_time_limit(0); 
         print(fread($fp,1024*$maxSpeed)); 
         flush(); 
         ob_flush(); 
         sleep(1); 
        } 
        fclose($fp); 
        return((connection_status()==0) and !connection_aborted()); 
       } 
       else 
        showMessage("not enough credit."); 
      } 
      else 
       showMessage("login to download."); 
     } 
     else 
     { 
      wp_redirect(get_bloginfo('url')); 
      exit; 
     } 
    } 
    else 
    { 
     wp_redirect(get_bloginfo('url')); 
     exit; 
    } 
} 

我認爲頭是罪魁禍首!


Accept-rangesContent-length頭恢復方法已經完成,當文件被下載瀏覽器的工作原理。

+0

所以你用一根燙針將腳本分成一組,現在抱怨它不穩定?那麼接受或修復腳本。再次檢查服務器配置。並不是說ZIP ZIP是輸出,然後瀏覽器或下載器不知道從哪裏開始以及在哪裏停止。 - 通過首先進行訪問檢查,然後處理下載,您可以開始簡化您的生活 - 並非全部都在一個。一個接一個。 – hakre 2013-04-07 20:04:40

+0

@hakre,在評論中注意到zip gz是有用的。 – revo 2013-04-07 20:09:39

+0

一些下載管理器使用你的代碼不支持的multipart – Baba 2013-04-07 20:09:44

回答

1

我不確定其他下載如何工作,但您的範圍實現是錯誤的爲什麼?

你得到range這樣

$_SERVER['HTTP_RANGE'] = "bytes=1-200"; //sample range 
$size = 1000; //sample size 

list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']); 
str_replace($range, "-", $range); 
$size2 = $size - 1; 
$new_length = $size - $range; 

// Used Print Insted of headers 
print("HTTP/1.1 206 Partial Content\n"); 
print("Content-Length: $new_length\n"); 
print("Content-Range: bytes $range$size2/$size\n"); 

輸出

HTTP/1.1 206 Partial Content 
Content-Length: 999 
Content-Range: bytes 1-200999/1000 <------ This is an issue 

我希望這樣的事情

list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']); 
list($offset, $length) = explode("-", $range); 
$length = $length - $offset; 

// Used Print Insted of headers 
print("HTTP/1.1 206 Partial Content\n"); 
print("Content-Length: $length\n"); 
printf('Content-Range: bytes %d-%d/%d', $offset, ($offset + $length), $size) 

輸出

HTTP/1.1 206 Partial Content 
Content-Length: 199 
Content-Range: bytes 1-200/1000 <---- Properly Displayed 
+0

這是什麼$範圍!哦,是的,這是正確的!如何「意外結束歸檔」錯誤?當沒有以前的簡歷下載文件時! – revo 2013-04-07 21:14:20

+0

'fseek($ fp,$ range);'???我希望你自動地知道 – Baba 2013-04-07 21:30:49