sendfile() public static method

sendfile()
public static sendfile ( mixed $outfd, string $path, callable $cb, callable $startCb = null, integer $offset, integer $length = null, integer $pri = EIO_PRI_DEFAULT ) : true
$outfd mixed File descriptor
$path string Path
$cb callable Callback
$startCb callable Start callback
$offset integer Offset
$length integer Length
$pri integer Priority
return true Success
示例#1
0
 /**
  * Output whole contents of file
  * @param  string $path Path
  * @param  callable $cb Callback
  * @param  integer $pri Priority
  * @return boolean        Success
  */
 public function sendfile($path, $cb, $pri = EIO_PRI_DEFAULT)
 {
     if ($this->state === self::STATE_FINISHED) {
         return false;
     }
     try {
         $this->header('Content-Type: ' . MIME::get($path));
     } catch (RequestHeadersAlreadySent $e) {
     }
     if ($this->upstream->checkSendfileCap()) {
         FileSystem::sendfile($this->upstream, $path, $cb, function ($file, $length, $handler) {
             try {
                 $this->header('Content-Length: ' . $length);
             } catch (RequestHeadersAlreadySent $e) {
             }
             $this->ensureSentHeaders();
             $this->upstream->onWriteOnce(function ($conn) use($handler, $file) {
                 $handler($file);
             });
             return true;
         }, 0, null, $pri);
         return true;
     }
     $first = true;
     FileSystem::readfileChunked($path, $cb, function ($file, $chunk) use(&$first) {
         // readed chunk
         if ($this->upstream->isFreed()) {
             return false;
         }
         if ($first) {
             try {
                 $this->header('Content-Length: ' . $file->stat['size']);
             } catch (RequestHeadersAlreadySent $e) {
             }
             $first = false;
         }
         $this->out($chunk);
         return true;
     });
     return true;
 }