PHPDaemon\FS\FileSystem::sendfile PHP Method

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
    public static function sendfile($outfd, $path, $cb, $startCb = null, $offset = 0, $length = null, $pri = EIO_PRI_DEFAULT)
    {
        $cb = CallbackWrapper::forceWrap($cb);
        if (!self::$supported) {
            $cb($path, false);
            return false;
        }
        $noncache = true;
        FileSystem::open($path, 'r!', function ($file) use($cb, $noncache, $startCb, $path, $pri, $outfd, $offset, $length) {
            if (!$file) {
                $cb($path, false);
                return;
            }
            $file->sendfile($outfd, function ($file, $success) use($cb, $noncache) {
                $cb($file->path, $success);
                if ($noncache) {
                    $file->close();
                }
            }, $startCb, $offset, $length, $pri);
        }, $pri);
        return true;
    }

Usage Example

Ejemplo n.º 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;
 }