Swoole 上传文件

目前呼叫中心项目中有转码、CDR、拨号计划、SIP 分机等模块交给 PHP 大家族中风格清奇的 Swoole 来实现,为了图方便,具体用的是 EasySwoole 框架。这次在开发 IVR 模块中要用到 WEB 服务器上传录制好的音频文件到跑在 EasySwoole 的语音服务器上,于是用到了之前博客中的 Curl 上传文件,不过这次接收上传对象由 EasySwoole 来执行。

和常用的接收文件 $_FILES 不一致的是, Swoole 都封装到了 request 对象中,在 EasySwoole 中对应的就是通过 $this->request()->getSwooleRequest() 获取请求对象,具体的可以看看下面的代码实例

<?php

namespace App\HttpController;

use EasySwoole\Config;
use EasySwoole\Core\Http\AbstractInterface\Controller;

/**
 * Class Index
 * @package App\HttpController
 */
class Video extends Controller
{

    function index()
    {
        /**
         * "files": {
                "video": {
                "name": "defmusic.wav",
                "type": "audio/wave",
                "tmp_name": "/tmp/swoole.upfile.zpZjtJ",
                "error": 0,
                "size": 1939376
                }
            }
         */
        $swooleRequest = $this->request()->getSwooleRequest();
        try {
            if ($swooleRequest->files) {
                $filename = basename($swooleRequest->files['video']['name']);
                $tmpname = $swooleRequest->files['video']['tmp_name'];
                if (move_uploaded_file($tmpname, Config::getInstance()->getConf('video_path') . $filename)) {
                    $this->writeJson(['status' => 1, 'msg' => 'success']);
                } else {
                    $this->writeJson(['status' => 0, 'msg' => 'failure']);
                }
            } else {
                $this->writeJson(['status' => 0, 'msg' => 'no file']);
            }
        } catch (\Exception $e) {
            $this->writeJson(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值