uploading file via post
-
Why the below code doesn't upload file to http://xyz.com/uvw/upload_data.php[^] automatically ? I get error = uploaded file is either missing, empty or was not uploaded via the POST method But am sure uploading file is not missing or empty as i have echo it and i think i am uploading via post method ?
function datapost($URLServer,$postdata)
{$agent = "Mozilla/5.0"; $cURL\_Session = curl\_init(); curl\_setopt($cURL\_Session, CURLOPT\_URL,$URLServer); curl\_setopt($cURL\_Session, CURLOPT\_USERAGENT, $agent); curl\_setopt($cURL\_Session, CURLOPT\_POST, 1); curl\_setopt($cURL\_Session, CURLOPT\_POSTFIELDS,$postdata); curl\_setopt($cURL\_Session, CURLOPT\_RETURNTRANSFER, 1); curl\_setopt($cURL\_Session, CURLOPT\_FOLLOWLOCATION, 1); $result = curl\_exec($cURL\_Session); return $result;
}
$postdata = array(); $postdata\['Id'\] = '12340'; $postdata\['profileName'\] = 'tsunami'; $postdata\['file'\] = file\_get\_contents('http://www.abc.co.uk/def/ghi.csv'); $postdata\['submit'\] = urlencode('submit'); $source= datapost("http://xyz.com/uvw/upload\_data.php",$postdata);
Any suggestion devs ??
-
Why the below code doesn't upload file to http://xyz.com/uvw/upload_data.php[^] automatically ? I get error = uploaded file is either missing, empty or was not uploaded via the POST method But am sure uploading file is not missing or empty as i have echo it and i think i am uploading via post method ?
function datapost($URLServer,$postdata)
{$agent = "Mozilla/5.0"; $cURL\_Session = curl\_init(); curl\_setopt($cURL\_Session, CURLOPT\_URL,$URLServer); curl\_setopt($cURL\_Session, CURLOPT\_USERAGENT, $agent); curl\_setopt($cURL\_Session, CURLOPT\_POST, 1); curl\_setopt($cURL\_Session, CURLOPT\_POSTFIELDS,$postdata); curl\_setopt($cURL\_Session, CURLOPT\_RETURNTRANSFER, 1); curl\_setopt($cURL\_Session, CURLOPT\_FOLLOWLOCATION, 1); $result = curl\_exec($cURL\_Session); return $result;
}
$postdata = array(); $postdata\['Id'\] = '12340'; $postdata\['profileName'\] = 'tsunami'; $postdata\['file'\] = file\_get\_contents('http://www.abc.co.uk/def/ghi.csv'); $postdata\['submit'\] = urlencode('submit'); $source= datapost("http://xyz.com/uvw/upload\_data.php",$postdata);
Any suggestion devs ??
Hi, You need to set CURLOPT_HTTPGET to false explicitly - the PHP documentation doesn't make this clear. curl_setopt($cURL_Session, CURLOPT_HTTPGET, FALSE); Niall
-
Hi, You need to set CURLOPT_HTTPGET to false explicitly - the PHP documentation doesn't make this clear. curl_setopt($cURL_Session, CURLOPT_HTTPGET, FALSE); Niall
-
Thanks, I added the line you said but still getting the same error .. :( Anything else i can add/edit to make it working ? Cheers
No - that works on my system, as long as the CSV file isn't too big. (OK with 150k, not with 30M) This is the version of your code I tried out.
<?php
function datapost($URLServer,$postdata)
{$agent = "Mozilla/5.0"; $cURL\_Session = curl\_init(); curl\_setopt($cURL\_Session, CURLOPT\_URL,$URLServer); curl\_setopt($cURL\_Session, CURLOPT\_USERAGENT, $agent); curl\_setopt($cURL\_Session, CURLOPT\_HTTPGET, FALSE); //curl\_setopt($cURL\_Session, CURLOPT\_POST, 1); curl\_setopt($cURL\_Session, CURLOPT\_POSTFIELDS,$postdata); curl\_setopt($cURL\_Session, CURLOPT\_RETURNTRANSFER, 1); curl\_setopt($cURL\_Session, CURLOPT\_FOLLOWLOCATION, 1); $result = curl\_exec($cURL\_Session); return $result;
}
$postdata = array(); $postdata\['Id'\] = '12340'; $postdata\['profileName'\] = 'tsunami'; $postdata\['file'\] = file\_get\_contents('http://localhost/tmp/test.csv'); $postdata\['submit'\] = urlencode('submit'); $source= datapost("http://localhost/tmp/posthere.php",$postdata); echo $source;
?>
My test posthere.php is
-
No - that works on my system, as long as the CSV file isn't too big. (OK with 150k, not with 30M) This is the version of your code I tried out.
<?php
function datapost($URLServer,$postdata)
{$agent = "Mozilla/5.0"; $cURL\_Session = curl\_init(); curl\_setopt($cURL\_Session, CURLOPT\_URL,$URLServer); curl\_setopt($cURL\_Session, CURLOPT\_USERAGENT, $agent); curl\_setopt($cURL\_Session, CURLOPT\_HTTPGET, FALSE); //curl\_setopt($cURL\_Session, CURLOPT\_POST, 1); curl\_setopt($cURL\_Session, CURLOPT\_POSTFIELDS,$postdata); curl\_setopt($cURL\_Session, CURLOPT\_RETURNTRANSFER, 1); curl\_setopt($cURL\_Session, CURLOPT\_FOLLOWLOCATION, 1); $result = curl\_exec($cURL\_Session); return $result;
}
$postdata = array(); $postdata\['Id'\] = '12340'; $postdata\['profileName'\] = 'tsunami'; $postdata\['file'\] = file\_get\_contents('http://localhost/tmp/test.csv'); $postdata\['submit'\] = urlencode('submit'); $source= datapost("http://localhost/tmp/posthere.php",$postdata); echo $source;
?>
My test posthere.php is
Niall Barr wrote:
(OK with 150k, not with 30M)
Initially i thought this could be the case as i was uploading around 12mb csv but then i tried with just 4kb but still getting the same error. I don't really understand what could be the problem - the uploading works fine if i try using normal html form submit but as we don't want to manually browse the file - we decided to go this route and it's not working.. What a pain. On the other hand why do you say it will not work for size in MB ? Is there any other auto upload way for such big files ?
-
Niall Barr wrote:
(OK with 150k, not with 30M)
Initially i thought this could be the case as i was uploading around 12mb csv but then i tried with just 4kb but still getting the same error. I don't really understand what could be the problem - the uploading works fine if i try using normal html form submit but as we don't want to manually browse the file - we decided to go this route and it's not working.. What a pain. On the other hand why do you say it will not work for size in MB ? Is there any other auto upload way for such big files ?
I think the only issue was the amount of memory available to PHP on my dev box. I tried with a file that was too big for it. Are you able to get the other post data transferred if you comment out the file_get_contents line? Niall
-
I think the only issue was the amount of memory available to PHP on my dev box. I tried with a file that was too big for it. Are you able to get the other post data transferred if you comment out the file_get_contents line? Niall
Niall Barr wrote:
Are you able to get the other post data transferred if you comment out the file_get_contents line?
I can't test that as it's some external company site where we have to upload our data so that they can do what they are suppose to do with the data. Well i use below code as initial request - so they receive the post data and they send us back id which we need to send along with the file -
function datapost($URLServer,$postdata)
{$agent = "Mozilla/5.0";
$cURL_Session = curl_init();
curl_setopt($cURL_Session, CURLOPT_URL,$URLServer);
curl_setopt($cURL_Session, CURLOPT_USERAGENT, $agent);
curl_setopt($cURL_Session, CURLOPT_POST, 1);
curl_setopt($cURL_Session, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($cURL_Session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL_Session, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($cURL_Session);
return $result;
}
$postdata = array(); $postdata\['profileName'\] = 'tsunami'; $postdata\['tName'\] = 'abc'; $postdata\['langCode'\] = 'en\_GB'; ... etc
$source= datapost("http://xyz.com/uvw/upload\_meta.php",$postdata);
</pre>
It using the same process - so am sure it's only file auto upload bit that's not working for us. As i said it works using normal html form submit.
-
Niall Barr wrote:
Are you able to get the other post data transferred if you comment out the file_get_contents line?
I can't test that as it's some external company site where we have to upload our data so that they can do what they are suppose to do with the data. Well i use below code as initial request - so they receive the post data and they send us back id which we need to send along with the file -
function datapost($URLServer,$postdata)
{$agent = "Mozilla/5.0";
$cURL_Session = curl_init();
curl_setopt($cURL_Session, CURLOPT_URL,$URLServer);
curl_setopt($cURL_Session, CURLOPT_USERAGENT, $agent);
curl_setopt($cURL_Session, CURLOPT_POST, 1);
curl_setopt($cURL_Session, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($cURL_Session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL_Session, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($cURL_Session);
return $result;
}
$postdata = array(); $postdata\['profileName'\] = 'tsunami'; $postdata\['tName'\] = 'abc'; $postdata\['langCode'\] = 'en\_GB'; ... etc
$source= datapost("http://xyz.com/uvw/upload\_meta.php",$postdata);
</pre>
It using the same process - so am sure it's only file auto upload bit that's not working for us. As i said it works using normal html form submit.
It occured to me that it's likely that the site you're posting to expects an attached file rather than the file content in a post field called file... If that's the case, then this code should work.
-
It occured to me that it's likely that the site you're posting to expects an attached file rather than the file content in a post field called file... If that's the case, then this code should work.