Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用POST上传带有curl的文件,但模拟HTML提交_Php_C_Post_Curl_Libcurl - Fatal编程技术网

Php 使用POST上传带有curl的文件,但模拟HTML提交

Php 使用POST上传带有curl的文件,但模拟HTML提交,php,c,post,curl,libcurl,Php,C,Post,Curl,Libcurl,我有以下C代码: int main(int argc, char* argv[]) { CURL *curl; CURLcode res; struct stat file_info; double speed_upload, total_time; FILE *fd; fd = fopen(argv[1], "rb"); /* open file to upload */ if(!fd) { return 1; /* can't continue */

我有以下C代码:

int main(int argc, char* argv[])
{
  CURL *curl;
  CURLcode res;
  struct stat file_info;
  double speed_upload, total_time;
  FILE *fd;

  fd = fopen(argv[1], "rb"); /* open file to upload */
  if(!fd) {

    return 1; /* can't continue */
  }

  /* to get the file size */
  if(fstat(fileno(fd), &file_info) != 0) {

    return 1; /* can't continue */
  }

  curl = curl_easy_init();
  if(curl) {
    /* upload to this place */
    curl_easy_setopt(curl, CURLOPT_URL,
                     "http://www.website.com/index.php");

    /* tell it to "upload" to the URL */
    curl_easy_setopt(curl, CURLOPT_POST, 1L);

    /* set where to read from (on Windows you need to use READFUNCTION too) */
    curl_easy_setopt(curl, CURLOPT_READDATA, fd);

    /* and give the size of the upload (optional) */
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
                     (curl_off_t)file_info.st_size);

    /* enable verbose for easier tracing */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK) {
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    }
    else {
      /* now extract transfer info */
      curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
      curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);

      fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
              speed_upload, total_time);

    }
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
以及以下PHP代码:

<?php
header('Access-Control-Allow-Origin: *');

if ($_SERVER['REQUEST_METHOD'] == 'POST' )
{
    if( isset( $_FILES["file_entry"] ) )
    {
    print "file ok";
    }
    else
    {
    print "no file sent";
    }
}
else
{
    print "No post method" . $_SERVER['REQUEST_METHOD'];
}
?>

和html代码:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>
</head>
<body>
<form action="index.php" enctype="multipart/form-data" method="post">
    <input type="file" name="file_entry"/>
    <input type="submit" value="enviar"/>
</form>
</body>
</html>

如果我尝试通过html发送文件,它可以正常工作,但在使用C和cURL时就不行了。 因为我不知道如何发送“文件输入”。如何像html那样用C cURL模拟提交“文件\条目”?我尝试使用CURLOPT_POSTFIELDS,但不起作用

有人能帮我吗


谢谢

这是因为您的服务器端需要“多部分/表单数据”帖子,而您发送的是常规的“url编码”帖子

相反,您应该查看示例或()文档


你可以更改服务器端。

没有C++,只是C。我尝试将方法改为“CurLopt*Poad”,但也不工作。有一个简单的方法使用我的C代码吗?只实施一些更改?因为示例“multi-post.c”让我很难理解。我已经回答了这个问题。如何修复进行表单后期上传的代码。如果您现在突然想做其他事情,那么请确定您需要一些其他代码,但这不再是这个问题,因此,我很抱歉,但我不会继续在这里作为评论沿着这条路线走下去!