PHPFusion\httpdownload::download PHP Метод

download() публичный Метод

Start download
public download ( ) : boolean
Результат boolean
    function download()
    {
        if (!$this->initialize()) {
            return FALSE;
        }
        $seek = $this->seek_start;
        $speed = $this->speed;
        $bufsize = $this->bufsize;
        $packet = 1;
        //do some clean up
        if (ob_get_length() !== FALSE) {
            @ob_end_clean();
        }
        $old_status = ignore_user_abort(TRUE);
        if (!ini_get('safe_mode') && function_exists("set_time_limit")) {
            @set_time_limit(0);
        }
        $this->bandwidth = 0;
        $size = $this->data_len;
        if ($this->data_type == 0) {
            $size = filesize($this->data);
            if ($seek > $size - 1) {
                $seek = 0;
            }
            if ($this->filename == NULL) {
                $this->filename = basename($this->data);
            }
            $res = fopen($this->data, 'rb');
            if ($seek) {
                fseek($res, $seek);
            }
            if ($this->seek_end < $seek) {
                $this->seek_end = $size - 1;
            }
            $this->header($size, $seek, $this->seek_end);
            //always use the last seek
            $size = $this->seek_end - $seek + 1;
            while (!(connection_aborted() || connection_status() == 1) && $size > 0) {
                if ($size < $bufsize) {
                    echo fread($res, $size);
                    $this->bandwidth += $size;
                } else {
                    echo fread($res, $bufsize);
                    $this->bandwidth += $bufsize;
                }
                $size -= $bufsize;
                flush();
                if ($speed > 0 && $this->bandwidth > $speed * $packet * 1024) {
                    sleep(1);
                    $packet++;
                }
            }
            fclose($res);
        } elseif ($this->data_type == 1) {
            if ($seek > $size - 1) {
                $seek = 0;
            }
            if ($this->seek_end < $seek) {
                $this->seek_end = $this->data_len - 1;
            }
            $this->data = substr($this->data, $seek, $this->seek_end - $seek + 1);
            if ($this->filename == NULL) {
                $this->filename = time();
            }
            $size = strlen($this->data);
            $this->header($this->data_len, $seek, $this->seek_end);
            while (!connection_aborted() && $size > 0) {
                if ($size < $bufsize) {
                    $this->bandwidth += $size;
                } else {
                    $this->bandwidth += $bufsize;
                }
                echo substr($this->data, 0, $bufsize);
                $this->data = substr($this->data, $bufsize);
                $size -= $bufsize;
                flush();
                if ($speed > 0 && $this->bandwidth > $speed * $packet * 1024) {
                    sleep(1);
                    $packet++;
                }
            }
        } else {
            if ($this->data_type == 2) {
                //just send a redirect header
                header('location: ' . $this->data);
            }
        }
        if ($this->use_autoexit) {
            exit;
        }
        //restore old status
        ignore_user_abort($old_status);
        if (!ini_get('safe_mode') && function_exists("set_time_limit")) {
            @set_time_limit(ini_get("max_execution_time"));
        }
        return TRUE;
    }

Usage Example

Пример #1
0
 /**
  * Attachment download request
  */
 public static function check_download_request()
 {
     $locale = fusion_get_locale("", FORUM_LOCALE);
     $response = FALSE;
     if (isset($_GET['getfile']) && isnum($_GET['getfile'])) {
         $result = dbquery("SELECT attach_id, attach_name FROM " . DB_FORUM_ATTACHMENTS . " WHERE attach_id='" . $_GET['getfile'] . "'");
         if (dbrows($result)) {
             $data = dbarray($result);
             if (file_exists(FORUM . "attachments/" . $data['attach_name'])) {
                 dbquery("UPDATE " . DB_FORUM_ATTACHMENTS . " SET attach_count=attach_count+1 WHERE attach_id='" . $data['attach_id'] . "'");
                 //ob_end_clean();
                 require_once INCLUDES . "class.httpdownload.php";
                 $object = new httpdownload();
                 $object->set_byfile(FORUM . "attachments/" . $data['attach_name']);
                 $object->use_resume = TRUE;
                 $object->download();
                 $response = TRUE;
             } else {
                 addNotice("warning", $locale['forum_0398']);
             }
         }
     }
     return $response;
 }