php将数据和文件备份到另一个服务器

22 篇文章 0 订阅
  //数据备份到远程服务器
        $url='http://'.$ser.'/?act=bfapi';//$ser 远程服务器域名或ip地址
        $this->curl_call($url,'post',$datan);
 
      把文件备份到远程服务器
    $url1='http://'.$ser.'/?act=bfapi&mod=savefile';
            $str=substr($datan['picpath'],1);
            $str=Domain.$str;
            $d=[];
            $d['oldfilepath']=$str;
            $d['newfilepath']=$datan['uniqueid'];
            
            // echo '<pre>';
            // print_r($d);
            $this->curl_call($url1,'post',$d);
//CUrl请求调用接口
function curl_call($url,$method='get',$data='')
{
    $ch = curl_init();
    $header = array('Accept-Charset: utf-8');
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    if($error=curl_error($ch)){
        return $error;
    }
    curl_close($ch);
    return $result;
}
<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020/5/6
 * Time: 16:17
 */
if(!defined('URLCHECK')){echo 'request error';exit;}
class bfapiController extends common{
    //保存数据
    public function indexModel()
    {
        $modle=$this->Model('onlinesave');
        $info=$modle->GetInfo($_POST['id']);
        if(empty($info))
        {
            $model=$this->Model('onlinesave');
            $model->AddData($_POST);
        }else{
            $model=$this->Model('onlinesave');
            $model->saveInfo($_POST['id'],$_POST);
        }
    }


    //保存文件
    public function savefileModel()
    {
        $newfilepath=$_POST['newfilepath'];
        $file=$_POST['oldfilepath'];


        $arr=explode('/',$file);
        $filename=end($arr);


        $path='./file/'.$newfilepath.'/';
        if(!is_dir($path)){
            mkdir(iconv("GBK","UTF-8",$path),0777,true);
        }

        file_put_contents($path.$filename,file_get_contents($file));
    }
 
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值