Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using http header to POST a file 1

Status
Not open for further replies.

kaancho12

Technical User
Feb 22, 2005
191
hi,
could anybody tell me the way to "POST" a file using "HTTP header" after authentication??? Here's what I have as of now, but its not working. Is this the way to send out HTTP header with POST ???:
$fp = fsockopen("192.168.0.141", 80, $errno, $errstr);
if (!$fp){
echo "ERROR:" . $errno . "-" . $errstr . "<br>";
}else{
$out = "POST /cgi-bin/ss/program.cgi?client=1&dbname=products&filename=\"/var/ HTTP /1.1\r\n";
$out .= "HOST: 192.168.0.141\r\n";
$out .= "Authorization: Basic ".base64_encode("username:passwd")."\r\n\r\n";
$string = "";
while(!feof($fp)){
$string .= fgets($fp, 128);
echo "string: " . $string;
}
fclose($fp);
 
hi,
thanks for the link.
i tried using the
curl_setopt ($curl_handle, CURLOPT_INFILE,"/var/option but its giving me this error:
curl_setopt(): supplied argument is not a valid File-Handle resource in <b>/var/ on line <b>53</b><br />
which is the "CURLOPT_INFILE" line ---is this the right format for file input???
thanks
ko12
 
I've never used that cURL option, but the error message tells me that the third parameter of your curl_setopt() invocation should be a file handle, not a string filename.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
thanks, for the tip.got that error fixed another error i get is, when i output the data is:
/////////////////
<html>
<body>
<h1>
<center>
Error
</center>
</h1>
<hr>
<p>
Internal error: Upload file name is missing in CGI data.
<!-- ClientApp: bo_error_message -->
<br>
<hr>
</body>
</html>
</table>
</table>
</table>
</table>
</table>
////////////

here's the code:
<?
/*
This method uses CURL to contact the server.

*/


// Either 'http' or 'https'. 'https' is only an option if OpenSSH
// is available on your system. Check phpinfo() to see whether
// HTTPS is available.
$HTTP_method = 'http';

// IP-resolvable FQDN of the server
$hostname = 'localhost';

// Path on that server to the CGI
$cgi = '/cgi-bin/ss/dbupload.cgi';

// Array of data. The foreach loop below is going to construct a field/data
// string like the one you see in the URL of a GET-method CGI.
$my_data = array (
'App' => '1',
'dbname' => 'products'
);

// This section constructs the field/value pairs of the form
// field1=value1&field2=value2&field3=value3
$data_string = " ";
$add_ampersand = FALSE;
foreach ($my_data as $key => $value)
{
if ($add_ampersand)
{
$data_string .= '&';
}
$data_string .= $key . '=' . $value;
$add_ampersand = TRUE;
}
//Set a file handle
$fp = fopen("/var/ "r");

// Get a CURL handle
$curl_handle = curl_init ();

// Tell CURL the URL of the CGI
curl_setopt ($curl_handle, CURLOPT_URL, $HTTP_method . '://' . $hostname . $cgi);

curl_setopt ($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_POST, 1);
curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $data_string);
curl_setopt ($curl_handle, CURLOPT_USERPWD,"username:passwd");
curl_setopt ($curl_handle, CURLOPT_INFILE,$fp);

echo "curl handle: " . $curl_handle . "\n";
// Perform the POST and get the data returned by the server.
$result = curl_exec($curl_handle) or die ("There has been an error");

// Close the CURL handle
curl_close ($curl_handle);

// Process the return
print $result;

?>

any input is welcome :)) thanks.
 
Sorry, that's probably not going to do it.

According to the comments in the curl_setopt() online manual page, if you have a file on to upload named "foo.bar", then something like:

$post_fields = array ('file_to_upload" => '@foo.bar');
curl_setopt ($ch, CURL_POSTFIELDS, $post_fields);

is all you have to do. This works with PHP 5.0.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top