0

我已经用 cURL 发送了一些帖子数据,现在正在尝试发送一个文件,但在此过程中迷路了。我正在使用带有文件输入的表单。然后希望 cURL 将其发送到我的服务器。这是我的上传页面。 <form action="curl_page.php" method="post" enctype="multipart/form-data"> Filename: <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form>

这是我的卷曲页面,有一些问题。

<?php
$tmpfile = $_FILES['file']['tmp_name'];
$filename = basename($_FILES['file']['name']);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://my_server/file_catch.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
    'uploaded_file' => '@'.$tmpfile.';filename='.$filename,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
?>

我服务器上的 file_catch.php 看起来像这样。

<?php
$folder = "audio/";

$path = $folder . basename( $_FILES['file']['name']);

if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
    echo "The file ".  basename( $_FILES['file']['name']). " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

谢谢你的帮助!

4

1 回答 1

2

您没有发送通过发布请求获得的文件。使用以下代码通过 CURL 请求发布文件。

$tmpfile = $_FILES['image']['tmp_name'];
$filename = basename($_FILES['image']['name']);

$data = array(
    'uploaded_file' => '@'.$tmpfile.';filename='.$filename,
);

$ch = curl_init();   
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// set your other cURL options here (url, etc.)

curl_exec($ch);

有关更多参考,请查看以下链接 Send file via cURL from form POST in PHP

于 2012-12-03T05:59:42.717 回答