当前位置: 代码迷 >> 综合 >> 简单的 swoole 文件上传
  详细解决方案

简单的 swoole 文件上传

热度:19   发布时间:2024-01-26 03:27:56.0

 swoole_http_Server  简单的文件上传  代码如下:

 

<?php
$http = new swoole_http_Server("0.0.0.0", 9501);$http->on('request', function ($request, $response) use($http) {if($request->server['request_method'] != 'GET'){echo'提交方式不对'.PHP_EOL;
//        $response->status(404);//发送Http状态码。 $http_status_code必须为合法的HttpCode,如200, 502, 301, 404等,否则会设置为200状态码
//        $response->close();// 关闭WebSocket连接。仅用于Coroutine\Http\Server中。
//        $response->redirect('http://fanyi.youdao.com/',302);//发送Http跳转。调用此方法会自动end发送并结束响应。return ;}
//    var_dump($request->files);//文件属性/*array(5) {["name"]=>  "birdName.txt"// 浏览器上传时传入的文件名称["type"]=>"text/plain"//MIME类型["tmp_name"]=>"/tmp/swoole.upfile.uhmzYL"//上传的临时文件,文件名以/tmp/swoole.upfile开头["error"]=>0["size"]=> 4156//文件尺寸}*/$file = $request->files['files'];$file_name = $file['name'];$file_tmp_path = $file['tmp_name'];$uplod_path = __DIR__.'/uplode/';if(!file_exists($uplod_path)){mkdir($uplod_path);}$res = move_uploaded_file($file_tmp_path,$uplod_path . $file_name);//函数将上传的文件移动到新位置。if($res){$response->end("<h1>Hello Swoole. Upload Success </h1>");}$response->end("<h1>Hello Swoole. Upload Failed </h1>");
});
$http->start();