easyswoole视频上传大小限制取消

  • 在nginx.conf的http模块加入:
    client_max_body_size 200m;
  • 配置nginx文件预览功能:
server {
    listen       10000;
    server_name  127.0.0.1;
    #charset koi8-r;
    access_log  /php/host.access.log  main;
    location / {
        root   /php/easyswoole/webroot/video;	
		autoindex on;
		autoindex_localtime on; #之类的参数写这
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
  • 在easyswoole根目录dev.php下面配置
    ‘package_max_length’ => 210241024000
  • App/HttpController/Api/Upload.php
  • 上传方法
 public function file(){
        $request=$this->request();
        $obj=new Video($request);
        $path=$obj->upload();
        $paths=explode('/',$path);
        if(!empty($path)){
            return $this->writeJson(200,array('data'=>array(
                'url'=>'http://39.106.10.163:10000/'.$paths[5].'/'.$paths[6]
            )),'上传成功');
        }
        else{
            return $this->writeJson(400,array('data'=>array(
                'url'=>''
            )),'上传失败');
        }
/*       $request=$this->request();
       $videos=$request->getUploadedFile("video");
       $flag=$videos->moveTo('/php/video/1.mp4');
//       if($flag){
//           return $this->writeJson(0,array(data=>[
//               'url'=>''
//           ]),'获取成功');
//       }
        $configs=array(
            'host'=>'39.105.231.119',
            'username'=>'root',
            'password'=>'asdf123456A*',
            'port'=>'22'
        );
        $sftp=new Sftp($configs);
        $re=$sftp->ssh2_dir_exits("/usr/local");
		if($re){//如果目录存在直接上传
            $sftp->upftp('/php/video/1.mp4','/usr/local/1.mp4');
        }else{
            $sftp->ssh2_sftp_mchkdir('/usr/local');
            $sftp->upftp('/php/video/1.mp4','/usr/local/1.mp4');
        }*/

    }
  • App/Lib/Upload/Video.php
<?php
/**
 * Created by PhpStorm.
 * User: niuyueyang
 * Date: 2019/7/15
 * Time: 8:56
 */
namespace App\Lib\Upload;
use App\Lib\Upload\Base;
class Video extends Base{
    public $fileType='video';
    public $fileTypes=[
        'mp4',
        'x-flv'
    ];
}
  • App/Lib/Upload/Base.php
<?php
/**
 * Created by PhpStorm.
 * User: niuyueyang
 * Date: 2019/7/15
 * Time: 8:57
 */
namespace App\Lib\Upload;
use App\Lib\Utils;
class Base {
    public $type='';
    public $request='';
    public $size='';
    public $clientType='';
    public function __construct($request)
    {
        $this->request=$request;
        //获取所有上传的文件信息
        $files=$this->request->getSwooleRequest()->files;
        $types=array_keys($files);
        $this->type=$types[0];//video
    }
    public function index(){
        echo '111';
    }
    public function upload(){
        if($this->type!=$this->fileType){
            return false;
        }
        $videos=$this->request->getUploadedFile($this->type);
        $this->size=$videos->getSize();//获取上传文件大小
        $this->checkSize();
        $fileName=$videos->getClientFileName();//获取文件名
        $this->clientType=$videos->getClientMediaType();//获取文件类型
        $this->checkMediaType();
        $filePath=$this->getFile($fileName);
        $flag=$videos->moveTo($filePath);
        if(!empty($flag)){
            return $filePath;
        }
        else{
            return '';
        }
    }
    public function checkSize(){
        if(empty($this->size)){
            return false;
        }
        return true;
    }
    public function checkMediaType(){
        $clientType=explode('/',$this->clientType);
        if(empty($clientType[1])){
            return '上传文件不能为空';
        }
        else if(!in_array($clientType[1],$this->fileTypes)){
            return'上传文件格式不合法';
        }
        else{
            return true;
        }
    }
    public function getFile($filename){
        $pathinfo=pathinfo($filename);
        $extension=$pathinfo['extension'];
        $dir=EASYSWOOLE_ROOT.'/webroot/'.$this->type.'/'.date('Y').'/'.date('m');
        if(!is_dir($dir)){
            mkdir($dir,0777,true);
        }
        return $dir.Utils::getFileKey($filename).'.'.$extension;
    }
}
  • App/Lib/Utils.php
<?php
namespace App\Lib;
/**
 * Created by PhpStorm.
 * User: niuyueyang
 * Date: 2019/7/15
 * Time: 18:56
 */
class Utils {
    /**
     * 生成的唯一性key
     * @param string $str
     * @return string
     */
    public static function getFileKey($str) {
        return substr(md5(self::makeRandomString() . $str . time() . rand(0, 9999)), 8, 16);
    }

    /**
     * 生成随机字符串
     * @param string $length 长度
     * @return string 生成的随机字符串
     */
    public static function makeRandomString($length = 1) {

        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol) - 1;

        for($i=0; $i<$length; $i++) {
            $str .= $strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }
        return $str;
    }
}
安装sftp依赖

https://blog.csdn.net/u011477914/article/details/90668282

上传至其他服务器

https://blog.csdn.net/xgs736214763/article/details/78354659

  • App/Lib/Sftp/Sftp.php
<?php
namespace App\Lib\Sftp;

class Sftp{
    private $config='';
    private $conn='';
    private $ressftp='';
    public function __construct($config)
    {
        $this->config=$config;
        $this->connect();
    }
    public function connect(){
        $this->conn=ssh2_connect($this->config['host'],$this->config['port']);
        if(ssh2_auth_password($this->conn,$this->config['username'],$this->config['password'])){
            $this->ressftp=ssh2_sftp($this->conn);
        }
        else{
            echo '用户名或者密码错误';
        }
    }
    //下载文件
    public function downftp($remote,$local){
        return copy("ssh2.sftp://{$this->ressftp}".$remote,$local);
    }
    //文件上传
    public function upftp($local,$remote,$file_mode=0777){
        return copy($local,"ssh2.sftp://{$this->ressftp}".$remote);
    }
    //创建目录
    public function ssh2_sftp_mchkdir($path){
        $this->ssh2_sftp_mchkdir($this->ressftp,$path,0777);
    }
    //判断目录是否存在
    public function ssh2_dir_exits($dir){
        return file_exists("ssh2.sftp://{$this->ressftp}".$dir);
    }
}


  • easyswoole里面添加一个方法
public function file(){
       $request=$this->request();
       $videos=$request->getUploadedFile("video");
       $flag=$videos->moveTo('/php/video/1.mp4');
//       if($flag){
//           return $this->writeJson(0,array(data=>[
//               'url'=>''
//           ]),'获取成功');
//       }
        $configs=array(
            'host'=>'39.105.231.119',
            'username'=>'root',
            'password'=>'asdf123456A*',
            'port'=>'22'
        );
        $sftp=new Sftp($configs);
        $re=$sftp->ssh2_dir_exits("/usr/local");
		if($re){//如果目录存在直接上传
            $sftp->upftp('/php/video/1.mp4','/usr/local/1.mp4');
        }else{
            $sftp->ssh2_sftp_mchkdir('/usr/local');
            $sftp->upftp('/php/video/1.mp4','/usr/local/1.mp4');
        }

    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值