douguizhuang8276 2014-06-23 12:12
浏览 139
已采纳

PHP cURL将文件设置为POST表单输入类型文件

I have a page on a different server with a <form> and I use cURL to set the data automatically. However in this form there is a file input <input name='file' type='file'> and now I am aware of the fact that the file input behaves differently from the others.

//This is my code till now.
$ch = curl_init();
//page is password protected I am not sure if this works but I hope it does.
curl_setopt($ch, CURLOPT_URL, "http://user:pass@mydomain.com/system_backup.php");
curl_setopt($ch, CURLOPT_POST, 1);
//set a file as post variable this doesn't work
curl_setopt($ch, CURLOPT_POSTFIELDS, "file=".$file."&var1=".$var1."&var2=".$var2);
$result = curl_exec($ch);
curl_close($ch);

So my question is how do I set a file as a postfield with cURL.

  • 写回答

3条回答 默认 最新

  • douyue2313 2014-06-23 14:39
    关注

    found the solution myself

    $postParams['file'] = '@'.realpath("URLTOFILE.txt"); //for type = file
    $postParams['var1'] = urlencode("var1"); //type = text, submit pretty much everything.
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://user:pass@mydomain.com/system_backup.php");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动