2013-04-28 59 views
1

我查到了很多主題和帖子,並做了我自己的下載片段2功能如下!用簡歷下載

下載從無任何問題開始,當用戶信用不足時顯示自定義錯誤,所有內容都正確但簡歷!我認爲這可能是頭部,但我沒有想念他們。

我也讀了很多帖子在stackoverflow。

幫助需要!

function readfile_chunked($filename, $size = '', $uid, $pid, $retbytes = TRUE) 
{ 
    global $each_download; 
    $chunk_size = 1024*1024; 
    $buffer = ''; 
    $cnt =0; 
    $handle = fopen($filename, 'rb'); 
    if ($handle === false) 
    { 
     return false; 
    } 
     //check if http_range is sent by browser (or download manager) 
    if(isset($_SERVER['HTTP_RANGE'])) 
    { 
     list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']); 
     str_replace($range, "-", $range); 
     $size2=$size-1; 
     $new_length=$size2-$range; 
     header("HTTP/1.1 206 Partial Content"); 
     header("Content-Length: $new_length"); 
     header("Content-Range: bytes $range$size2/$size"); 
     fseek($fp,$range); 
    } 
    else 
    { 
     if(get_user_meta($uid, 'revo_dropped', true) != $pid) 
     { 
      update_user_meta($uid, 'dropped', $pid); 
      update_user_meta($uid, 'credits', get_user_meta($uid, 'credits', true)-$each_download); 
     } 
     $size2 = $size-1; 
     header("Content-Range: bytes 0-$size2/$size"); 
     header("Content-Length: ".$size); 
    } 
    while (!feof($handle) && connection_status()==0) 
    { 
     @set_time_limit(0); 
     $buffer = fread($handle, $chunk_size); 
     echo $buffer; 
     ob_flush(); 
     flush(); 
     if ($retbytes) 
     { 
      $cnt += strlen($buffer); 
     } 
    } 
    $status = fclose($handle); 
    if ($retbytes && $status) 
    { 
     return $cnt; 
    } 
    return $status; 
} 
function save_file() 
{ 
    global $wpdb; 
    global $uid; 
    global $each_download; 
    $hash = @$_GET["download"]; 
    ini_set('zlib.output_compression', 'Off'); 
    set_time_limit(0); 
    session_cache_limiter('none'); 
    set_magic_quotes_runtime(0); 
    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, 'credits', true) >= $each_download) 
       { 
        $parts = pathinfo($row['url']); 
        $url = $parts['dirname'] . '/' . $parts['basename'];//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(); 
        ob_start(); 
        header("Pragma: public"); 
        header("Expires: 0"); 
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        header("Cache-Control: public"); 
        header("Robots: none"); 
        header("Content-Type: ".$contentType.""); 
        header("Content-Description: File Transfer"); 
        $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); 
        //@ob_end_clean(); 
        header("Content-Disposition: ".$contentDisposition."; filename=\"".$new_name."\";"); 
        header("Content-Transfer-Encoding: binary"); 
        header("Accept-Ranges: bytes"); 
        $size = $row['size']; 
        $thefile = $url; 

        $urlparsed = parse_url($thefile); // urlllllllllllll 
        $isURI = array_key_exists('scheme', $urlparsed); 
        $localURI = (bool) strstr($thefile, get_bloginfo('wpurl')); 
        $patterns = array('|^'. get_bloginfo('wpurl') . '/' . '|'); 
        $path = preg_replace($patterns, '', $thefile); 
        // this is joining the ABSPATH constant, changing any slashes to local filesystem slashes, and then finally getting the real path. 
        $thefile = str_replace('/', DIRECTORY_SEPARATOR, path_join(ABSPATH, $path)); 

        /////////////////////////////////// 
        if($size == 0) 
        { 
         showMessage('Size 0'); 
        } 
        if (isset($size) && $size > 0) 
        { 
         //header("Content-Length: ".$size); 
         @readfile_chunked($thefile, $size, $uid, $pid); 
        } 
        /////////////////////////////////// 
        exit; 
       } 
       else 
        showMessage("buy credit"); 
      } 
      else 
       showMessage("login"); 
     } 
     else 
     { 
      wp_redirect(get_bloginfo('url')); 
      exit; 
     } 
    } 
    else 
    { 
     wp_redirect(get_bloginfo('url')); 
     exit; 
    } 
} 
+4

我看到了很多錯誤抑制的,代碼在牆上,這個概念由你「的錯誤顯示他們的情況,但沒有簡歷「。這是一個追求? – PeeHaa 2013-04-28 21:00:59

+0

尋找珍貴的東西可以讓人變得瘋狂 – 2013-04-28 21:02:18

+0

@PeeHaa埽編輯我的帖子 – revo 2013-04-28 21:04:30

回答

4

該腳本可以幫助你:

<?php 
/** 
* Copyright 2012 Armand Niculescu - media-division.com 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
* THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*/ 
// get the file request, throw error if nothing supplied 

// hide notices 
@ini_set('error_reporting', E_ALL & ~ E_NOTICE); 

//- turn off compression on the server 
@apache_setenv('no-gzip', 1); 
@ini_set('zlib.output_compression', 'Off'); 

if(!isset($_REQUEST['file']) || empty($_REQUEST['file'])) 
{ 
    header("HTTP/1.0 400 Bad Request"); 
    exit; 
} 

// sanitize the file request, keep just the name and extension 
// also, replaces the file location with a preset one ('./myfiles/' in this example) 
$file_path = $_REQUEST['file']; 
$path_parts = pathinfo($file_path); 
$file_name = $path_parts['basename']; 
$file_ext = $path_parts['extension']; 
$file_path = './myfiles/' . $file_name; 

// allow a file to be streamed instead of sent as an attachment 
$is_attachment = isset($_REQUEST['stream']) ? false : true; 

// make sure the file exists 
if (is_file($file_path)) 
{ 
    $file_size = filesize($file_path); 
    $file = @fopen($file_path,"rb"); 
    if ($file) 
    { 
     // set the headers, prevent caching 
     header("Pragma: public"); 
     header("Expires: -1"); 
     header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0"); 
     header("Content-Disposition: attachment; filename=\"$file_name\""); 

     // set appropriate headers for attachment or streamed file 
     if ($is_attachment) 
       header("Content-Disposition: attachment; filename=\"$file_name\""); 
     else 
       header('Content-Disposition: inline;'); 

     // set the mime type based on extension, add yours if needed. 
     $ctype_default = "application/octet-stream"; 
     $content_types = array(
       "exe" =--> "application/octet-stream", 
       "zip" => "application/zip", 
       "mp3" => "audio/mpeg", 
       "mpg" => "video/mpeg", 
       "avi" => "video/x-msvideo", 
     ); 
     $ctype = isset($content_types[$file_ext]) ? $content_types[$file_ext] : $ctype_default; 
     header("Content-Type: " . $ctype); 

     //check if http_range is sent by browser (or download manager) 
     if(isset($_SERVER['HTTP_RANGE'])) 
     { 
      list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2); 
      if ($size_unit == 'bytes') 
      { 
       //multiple ranges could be specified at the same time, but for simplicity only serve the first range 
       //http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt 
       list($range, $extra_ranges) = explode(',', $range_orig, 2); 
      } 
      else 
      { 
       $range = ''; 
       header('HTTP/1.1 416 Requested Range Not Satisfiable'); 
       exit; 
      } 
     } 
     else 
     { 
      $range = ''; 
     } 

     //figure out download piece from range (if set) 
     list($seek_start, $seek_end) = explode('-', $range, 2); 

     //set start and end based on range (if set), else set defaults 
     //also check for invalid ranges. 
     $seek_end = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1)); 
     $seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0); 

     //Only send partial content header if downloading a piece of the file (IE workaround) 
     if ($seek_start > 0 || $seek_end < ($file_size - 1)) 
     { 
      header('HTTP/1.1 206 Partial Content'); 
      header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size); 
      header('Content-Length: '.($seek_end - $seek_start + 1)); 
     } 
     else 
      header("Content-Length: $file_size"); 

     header('Accept-Ranges: bytes'); 

     set_time_limit(0); 
     fseek($file, $seek_start); 

     while(!feof($file)) 
     { 
      print(@fread($file, 1024*8)); 
      ob_flush(); 
      flush(); 
      if (connection_status()!=0) 
      { 
       @fclose($file); 
       exit; 
      }   
     } 

     // file save was a success 
     @fclose($file); 
     exit; 
    } 
    else 
    { 
     // file couldn't be opened 
     header("HTTP/1.0 500 Internal Server Error"); 
     exit; 
    } 
} 
else 
{ 
    // file does not exist 
    header("HTTP/1.0 404 Not Found"); 
    exit; 
} 

摘自:
http://www.richnetapps.com/php-download-script-with-resume-option/