cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

Failed mixed post string and file upload

From: Tilman Otto <t_otto_at_gmx.de>
Date: Thu, 30 Oct 2003 02:19:03 +0100

Hi,

I tried to convert this simple HTML-form in cURL-code:

<form action="http://www.externaldomain.com/importexport.php"
method="post" enctype="multipart/form-data">
        <input type="hidden" name="action" value="import">
        <input type="file" name="import_file">
        <input type="hidden" name="user" value="anyuser">
        <input type="hidden" name="passwd" value="anypasswd">
        <input type="hidden" name="delall" value="1">
        <input type="submit" value="import data">
</form>

The problem is I can't submit the name of the variable where the upload
file is stored (import_file).

This is my workaround:

<?php

$formvars = array();
$formvars["user"] = "anyuser \n";
$formvars["passwd"] = "anypasswd \n";
$formvars["delall"] = "1 \n";
$formvars["action"] = "import";

$fp = fopen("tool.xls","r");

 $ch = curl_init("http://www.externaldomain.com/importexport.php");

 //segment 1 begin
 curl_setopt($ch, CURLOPT_POST,1);
 curl_setopt($ch, CURLOPT_VERBOSE,1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS,$formvars);
 //segment 1 end

 //segment 2 begin
 curl_setopt($ch, CURLOPT_INFILE,$fp);
 curl_setopt($ch, CURLOPT_FILE,"import_file"); // With or without this
option, the script doesn't work
 curl_setopt($ch, CURLOPT_INFILESIZE, filesize("tool.xls"));
 curl_setopt($ch, CURLOPT_VERBOSE, 1);
 //segment 2 end

 $res = curl_exec($ch);
 $result_array = curl_getinfo($ch);
 $result_array{"CONTENT"} = $res;

 echo $result_array{"CONTENT"};

 curl_close($ch);

?>

I also tried this possibility in vain:
$formvars["import_file"] = "@tool.xls"; instead of segment 2
(CURLOPT_INFILE etc.).

The post data as a string wasn't successful, too:
$formvars = "user=anyuser&passwd=anypasswd&delall=1&action=import";

The receiving php-script displays an error: No file imported.
I'd been looking in several forums and haven't found anything.
The submit variable with the value "import data" is not necessary for
the cURL-script.

Can anybody help me?

Thanks,

  Tilman

-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/
Received on 2003-10-30