name_masked
name_masked

Reputation: 9794

Upload file does not work - cURL & PHP

My code to upload the file on the REMOTE server doesn't seem to be working. When uploading the file from the browser at the link, all the contents of csv file is committed to the database. Here is the code:

function postToDB()
{
  $fp = fopen('./errorLog.txt', 'w+');
  $csvFile = fopen('./myFile.csv', 'r');
  $url="http://abc.com/UploadCSVLogin/";

  $ch=curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 100);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_USERPWD, ":password");
  curl_setopt($ch, CURLOPT_PUT, true);

  curl_setopt($ch, CURLOPT_INFILE, $csvFile);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_STDERR, $fp);

  if (!curl_exec($ch))
     echo "Error: ".curl_error($ch);
  else
     echo "Success";
  curl_close($ch);
}

Strangely, the code echoes "Success". But I don't find the database updates taking place with the code above even though "Success" is echoed.

Can anyone help me find the issue here?

Upvotes: 0

Views: 1049

Answers (1)

Geoffrey
Geoffrey

Reputation: 11353

This is because your submitting the raw CSV data, and its not encoded into field=value pairs as form post data is. The easiest way to deal with this is on the server to just use:

$csvfile = file_get_contents("php//input");

Upvotes: 1

Related Questions