0

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?

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

3 Comments

You can, but you need to read up on RFC 2616 w3.org/Protocols/rfc2616/rfc2616.html
Also since your posting a file, you need to look at RFC 1341 - w3.org/Protocols/rfc1341/7_2_Multipart.html
Or, you could give this class a go, not sure though if it works as I just googled it for you - phpclasses.org/package/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.