Upload file does not work - cURL & PHP
My code to upload the file on the REM开发者_开发知识库OTE 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?
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");
精彩评论