// 检查是否有文件上传
if ($_FILES['fileToUpload']['error'] === UPLOAD_ERR_OK) {
// 设定要上传的图片文件路径
$uploadFile = $_FILES['fileToUpload']['tmp_name'];
// cURL 请求的目标接口
$targetUrl = 'https://example.com/upload-image'; // 替换为实际要上传图片的接口
// 创建 cURL 句柄
$ch = curl_init();
// 设置 cURL 选项
$postData = ['file' => new CURLFile($uploadFile)];
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 发起请求并获取响应
$response = curl_exec($ch);
// 检查请求是否成功
if ($response === false) {
echo "cURL 请求失败: " . curl_error($ch);
} else {
echo "图片上传成功!";
}
// 关闭 cURL 句柄
curl_close($ch);
} else {
echo "文件上传出错,请重新尝试。";
}
fastAdmin接收并转发到其他接口
//默认普通上传文件
$file = $this->request->file('file');
$res = \fast\Http::post('接收的URL',['file'=>new \CURLFile($file->getInfo()['tmp_name'],'',$file->getInfo()['name'])]);