php使用curl模拟post传输文件

fasong.php
<?
$url = 'http://127.0.0.1/fujian/jieshou.php'; // 目标URL
$filePath2 = iconv("utf-8","gb2312",'D:/MYOA/webroot/fujian/1/副本.txt'); // 文件路
$filePath1 = iconv("utf-8","gb2312",'D:/MYOA/webroot/fujian/1/1.txt'); // 文件路径径
 
$cfile1 = curl_file_create($filePath1);
$cfile2 = curl_file_create($filePath2);
$data = array('file1' => $cfile1,'file2' => $cfile2,'aa'=>'bbbb');
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
 
if(curl_errno($ch)){
    echo 'Error:' . curl_error($ch);
}
else{
    echo 'Response:' . $response;
}
 
curl_close($ch);

?>
jieshou.php
<?

//$postData = file_get_contents('php://input');
$aa=$_REQUEST["aa"];
$jintiantxt=date("Y-m-d").".txt";
$myfile = fopen($jintiantxt, "a") or die("Unable to open file!");//w
$txt =date("Y-m-d H:i:s")."\r\n".$aa."\r\n";
fwrite($myfile, $txt);

fclose($myfile);

/*
if (isset($_FILES['file']) && $_FILES['file']['error'] == UPLOAD_ERR_OK) {
    $tmpName = $_FILES['file']['tmp_name'];
    $name = basename($_FILES['file']['name']);
    $destination = "./2/{$name}";
 
    if (move_uploaded_file($tmpName, $destination)) {
        echo "File uploaded successfully to: {$destination}";
    } else {
        echo "Failed to move uploaded file.";
    }
} else {
    echo "No file uploaded or error occurred.";
}
*/
$save_dir="D:/MYOA/webroot/fujian/2/";
foreach ($_FILES as $key => $file) {
    if ($file["error"] > 0) {
        echo "错误: " . $file["error"] . "<br />";
    } else {
        echo "文件名: " . $file["name"] . "<br />";
        echo "类型: " . $file["type"] . "<br />";
        echo "大小: " . ($file["size"] / 1024) . " Kb<br />";
        echo "临时文件位置: " . $file["tmp_name"];
   	    // windows下路径有问题时进行处理(这个似乎是错的)
	    // $_FILES[$key]["tmp_name"] = str_replace("\\", "//", $file["tmp_name"]);
        // 保存文件
        if (file_exists($save_dir . $file["name"])) {
            echo $file["name"] . " 已存在. ";
        } else {
            move_uploaded_file($file["tmp_name"], $save_dir . $file["name"]);
            echo "存储到: " . $save_dir . $file["name"];
        }
    }
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hai7425

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

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

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

打赏作者

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

抵扣说明:

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

余额充值