Web::send PHP 메소드

send() 공개 메소드

Transmit file to HTTP client; Return file size if successful, FALSE otherwise
public send ( $file, $mime = NULL, $kbps, $force = TRUE, $name = NULL, $flush = TRUE ) : integer | FALSE
$file string
$mime string
$kbps int
$force bool
$name string
$flush bool
리턴 integer | FALSE
    function send($file, $mime = NULL, $kbps = 0, $force = TRUE, $name = NULL, $flush = TRUE)
    {
        if (!is_file($file)) {
            return FALSE;
        }
        $size = filesize($file);
        if (PHP_SAPI != 'cli') {
            header('Content-Type: ' . ($mime ?: $this->mime($file)));
            if ($force) {
                header('Content-Disposition: attachment; ' . 'filename="' . ($name !== NULL ? $name : basename($file)) . '"');
            }
            header('Accept-Ranges: bytes');
            header('Content-Length: ' . $size);
            header('X-Powered-By: ' . Base::instance()->get('PACKAGE'));
        }
        $ctr = 0;
        $handle = fopen($file, 'rb');
        $start = microtime(TRUE);
        while (!feof($handle) && ($info = stream_get_meta_data($handle)) && !$info['timed_out'] && !connection_aborted()) {
            if ($kbps) {
                // Throttle output
                $ctr++;
                if ($ctr / $kbps > ($elapsed = microtime(TRUE) - $start)) {
                    usleep(1000000.0 * ($ctr / $kbps - $elapsed));
                }
            }
            // Send 1KiB and reset timer
            echo fread($handle, 1024);
            if ($flush) {
                ob_flush();
                flush();
            }
        }
        fclose($handle);
        return $size;
    }