简体   繁体   English

PHP使用curl发布数据

[英]php post data using curl

When I post the data using ajax and send to post.php by curl then $_FILES variable is empty and get the data in $_POST variable. 当我使用ajax发布数据并通过curl发送到post.php$_FILES post.php变量为空,并在$_POST变量中获取数据。 When I print $_POST var getting the following data on post.php . 当我打印$_POST var时,在post.php上获取以下数据。

   [temp_upload_file] => @/tmp/php6OHgQc;filename=Penguins.jpg;type=image/jpeg

when I print "$_FILES" var getting empty data on post.php 当我打印“ $ _FILES” var在post.php上获取空数据时

Array
(
)  

Code: 码:

$url = "post.php";
$ch = curl_init($url);
// send a file
curl_setopt($ch, CURLOPT_POST, true);

$data = array('temp_upload_file' =>'@'.$_FILES['uploadfile']['tmp_name'].';filename='.$_FILES['uploadfile']['name'].';type='. $_FILES['uploadfile']['type']);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
echo curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

What I am doing wrong here?? 我在这里做错了什么?

Thank you in advance 先感谢您

For php5.5+ there is a new curl_file_create() function you need to use the @ format is now deprecated. 对于php5.5 +,现在不建议使用@格式的新curl_file_create()函数。

Try using following code: 尝试使用以下代码:

$url = "post.php";
$tmpfile = $_FILES['temp_upload_file']['tmp_name'];
$filename = basename($_FILES['temp_upload_file']['name']);

$ch = curl_init($url);
// send a file
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
    'uploaded_file' => curl_file_create($tmpfile, $_FILES['uploadfile']['type'], $filename)
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
echo curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM