php使用curl模拟上传文件(多个),接收文件
模拟上传代码// 初始化$ch = curl_init($url);// 创建 CURLFile 对象$cfile = curl_file_create($file);// 传入数据$data = array('test_file' => $cfile);curl_setopt($ch, CURLOPT_POST,1);curl_setopt($ch, CURLOPT_POS...
·
模拟上传代码
// 参数
$url = 'http://192.168.23.119/savefile.php';
$file1 = './phpinfo1.php';
$file2 = './phpinfo2.php';
// 初始化
$ch = curl_init($url);
// 创建 CURLFile 对象,两种方法效果相同
// 依次3各参数:(filename上传文件的路径)(mimetype文件的Mimetype)(postname文件名)。
$cfile1 = curl_file_create($file1);
$cfile2 = new CURLFile($file2);
// 传入数据(数组中可放入一个或多个文件)
$data = array(
'test_file1' => $cfile1,
'test_file2' => $cfile2,
);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// 执行
$output = curl_exec($ch);
HTML上传格式
<html>
<head>
<meta charset="utf-8">
<title>上传文件</title>
</head>
<body>
<form action="上传路径" method="post" enctype="multipart/form-data">
<label for="file">文件名:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
接收保存文件
$save_dir = 'c:/c/';
// 查看接收到的文件
var_dump($_FILES);
// 接收多个文件
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"];
}
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)