swoole_client->sendfile
发送文件到服务器,本函数是基于sendfile操作系统调用的。在swoole-1.7.5以上版本可用。
bool swoole_client->sendfile(string $filename)
- $filename指定要发送文件的路径
- 如果传入的文件不存在,将返回false
例子:
<?php $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞 $client->set(array( 'socket_buffer_size' => 1024 * 1024 * 2, )); $client->_count = 0; $client->on("connect", function(swoole_client $cli) { $cli->sendfile(__DIR__.'/test.txt'); }); $client->on("receive", function(swoole_client $cli, $data){ echo "Receive: $data"; $cli->close(); }); $client->on("error", function(swoole_client $cli){ echo "error\n"; }); $client->on("close", function(swoole_client $cli){ echo "Connection close\n"; }); $client->connect('127.0.0.1', 9501); $client->timer = swoole_timer_after(1000, function () use ($client) { echo "socket timeout\n"; $client->close(); }); echo "connect to 127.0.0.1:9501\n";
如果是同步client,sendfile会一直阻塞直到整个文件发送完毕或者发生致命错误
如果是异步client,sendfile会异步发送,当发生致命错误时会回调onError