使用cURL发送文件 [英] Sending File with cURL
问题描述
我使用cURL发送了一些帖子数据,现在正在尝试发送一个文件,在途中丢失。我使用一个带有文件输入的窗体。然后想要cURL将其发送到我的服务器。这是我的上传页面。
< form action =curl_page.phpmethod =postenctype =multipart / form-data>
文件名:< input type =filename =fileid =file/> < br />
< input type =submitname =submitvalue =Submit/>
< / form>
这是我的curl页面有一些问题。
<?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文件。 basename($ _FILES ['file'] ['name'])。 已上传;
} else {
echo上传文件时出错,请重试!
}
?>感谢任何帮助! 解决方案您不是发送通过发布请求获得的文件。使用以下代码通过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);
//在这里设置你的其他cURL选项(url等)
curl_exec($ ch);
有关更多参考,请查看以下链接
通过cURL通过PHP在表单POST中发送文件
I've sent some post data with cURL and am now trying to send a file, getting lost along the way. I'm using a form with a file input. Then would like cURL to send it off to my server. Here is my upload page.
<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>
Here is my curl page with some issues.
<?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);
?>
The file_catch.php on my server looks like this.
<?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!";
}
?>
Thanks for any help!
解决方案 You are not sending the file that you got through the post request. Use the below code for posting the file through CURL request.
$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);
For more reference check the below link
Send file via cURL from form POST in PHP
这篇关于使用cURL发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!