php http curl 上传附件

该PHP脚本包含了数据库查询、文件路径处理、HTTPPOST请求以及文件上传功能。它从数据库中获取数据,对文件进行编码转换,然后通过CURLFile发送到特定URL。如果上传成功,脚本会更新数据库状态并记录日志。
摘要由CSDN通过智能技术生成
<?php
include_once("inc/conn.php");
include_once("inc/utility_all.php");
include_once("inc/utility_org.php");
include_once("inc/utility_file.php");  
function http_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)) {
//        curl_setopt($curl, CURLOPT_HTTPHEADER, [
//            'Content-Type: application/post',
//        ]);
//        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_USERAGENT, 'PostmanRuntime/7.29.0');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $output = curl_exec($curl);
    curl_close($curl);
	return $output;
}
function convertEncoding($string){ 
    $encode = stristr(PHP_OS, 'WIN') ? 'GBK' : 'UTF-8';
    $string = iconv('UTF-8', $encode, $string);
    return $string;
}
$sql="select b.data_m882,b.data_m882_key,b.data_m883_key,b.data_m883,a.begin_time,a.run_id,a.data_m6316 from bpm_data_36 as a,bpm_data_36_child as b where a.run_id=b.run_id and (a.data_m6316 is null or a.data_m6316='') and a.id>0 and a.id<=8500 ";
$rows =TD::DB()->prepareQuery($sql);
if(is_array($rows) && !empty($rows))
{
	foreach($rows as $v){
	$zhengwen=explode('*',$v['data_m882']);
	$zhengwen_key=explode(',',$v['data_m882_key']);
	$zhengwen_shu =  sizeof($zhengwen_key);
	$path =  str_replace('-','',$v['begin_time']);
	$path = substr($path , 0 , 8);
	$run_id = $v['run_id'];
	$time = date("Y-m-d");
	
	for($i=1; $i<$zhengwen_shu ; $i++){
		$yueri = explode('_',$zhengwen_key[$i-1]);
		$zhengwen_yueri =  substr($yueri[0],-4);
	     $zhengwen_suiji = $yueri[1];
	     $file_fullpath = '/opt/tdoa/attach/approve_center/'.$zhengwen_yueri.'/'. $zhengwen_suiji.'.'.$zhengwen[$i-1];
		
		$file_url = new \CURLFile(convertEncoding($file_fullpath));
		$postData = [
			"file"=>$file_url,
			"path"=>'/'.$path.'/',
			"filename"=>$zhengwen[$i-1],
			"type"=>'fw'
		];
		
		$url = 'http://192.200.0.63:61160/receiveFiles';
		$res = http_request($url,$postData);
		$msg = json_decode($res,true);		
		if($msg['msg'] = 'success' and $msg['code'] = '200'){
		$sql2="UPDATE bpm_data_49 SET data_m6315='1' WHERE run_id=$run_id";
		TD::DB()->prepareQuery($sql2);
		file_put_contents("/opt/tdoa/webroot/ftp/sites.txt",$time.' | '.$run_id." 正文上传成功\r\n", FILE_APPEND);
			}else{
		file_put_contents("/opt/tdoa/webroot/ftp/sites.txt",$time.' | '.$run_id." 正文上传失败\r\n", FILE_APPEND);		
				}

			
		}


	$fujian=explode('*',$v['data_m883']);
	$fujian_key=explode(',',$v['data_m883_key']);
	$fujian_shu =  sizeof($fujian_key);
		for($i=1; $i<$fujian_shu ; $i++){
		 $yueri = explode('_',$fujian_key[$i-1]);
		 $fujian_yueri =  substr($yueri[0],-4);
	       $fujian_suiji = $yueri[1];
	       $file_fullpath = '/opt/tdoa/attach/approve_center/'.$fujian_yueri.'/'. $fujian_suiji.'.'.$fujian[$i-1];
		
		$file_url = new \CURLFile(convertEncoding($file_fullpath));
		$postData = [
			"file"=>$file_url,
			"path"=>'/'.$path.'/',
			"filename"=>$fujian[$i-1],
			"type"=>'fw'
		];
		
		$url = 'http://192.200.0.63:61160/receiveFiles';
		$res = http_request($url,$postData);
		$msg = json_decode($res,true);		
		if($msg['msg'] = 'success' and $msg['code'] = '200'){
		$sql2="UPDATE bpm_data_49 SET data_m6315='2' WHERE run_id=$run_id";
		TD::DB()->prepareQuery($sql2);
		file_put_contents("/opt/tdoa/webroot/ftp/sites2.txt",$time.' | '.$run_id." 附件上传成功\r\n", FILE_APPEND);
			}else{
		file_put_contents("/opt/tdoa/webroot/ftp/sites2.txt",$time.' | '.$run_id." 附件上传失败\r\n", FILE_APPEND);		
				}	
		}
	
	}
}
echo ok;
//var_dump($res);die;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hai7425

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值