Revise and Repost: Question about Curl and File Uploads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HC

    Revise and Repost: Question about Curl and File Uploads

    OK, last time I posted, I must have been totally unclear :) Hopefully
    this clears things up:

    I wrote the following script that uses Curl to mimic submitting a form
    over SSL with <input type='file'>. It works well, but for security
    reasons, I would like to not have to write the necessary data to a
    temporary file. If anyone could point me in the right direction, that
    would be great.

    -HC :)


    // Write the batch file to a temporary file
    $filename = "batchtemp" . time() . ".tmp";
    // We will use curl to send this file for processing
    $ch = curl_init();
    curl_setopt($ch , CURLOPT_RETURNT RANSFER, 1);
    curl_setopt($ch , CURLOPT_POST, 1);
    curl_setopt($ch , CURLOPT_URL, "https://www.example.com/");
    curl_setopt($ch , CURLOPT_POSTFIE LDS, array("batchFil e"=>"@$filename ));
    // Write $batchFile to the temporary file.
    if (!$file = fopen($filename , "w")) { echo "Cannot open file
    ($filename)"; exit; }
    if (fwrite($file, $batchFile) === FALSE) { echo "Cannot write to file
    ($filename)"; exit; }
    fclose($file);
    // Send the data
    $txResult = curl_exec($ch);
    // We're done with the temp file, and curl.
    unlink($filenam e);
    curl_close($ch) ;
  • R. Rajesh Jeba Anbiah

    #2
    Re: Revise and Repost: Question about Curl and File Uploads

    HC wrote:[color=blue]
    > OK, last time I posted, I must have been totally unclear :) Hopefully
    > this clears things up:
    >
    > I wrote the following script that uses Curl to mimic submitting a form
    > over SSL with <input type='file'>. It works well, but for security
    > reasons, I would like to not have to write the necessary data to a
    > temporary file. If anyone could point me in the right direction, that
    > would be great.[/color]
    <snip>

    I guess, you need to chuck CURLOPT_POST and CURLOPT_POSTFIE LDS; and
    hence add the file contents and other form contents explicitly via the
    (custom) HTTP headers using:
    curl_setopt($ch , CURLOPT_CUSTOMR EQUEST,'POST');
    curl_setopt($ch , CURLOPT_HTTPHEA DER, $header_array);

    To see, how to form the header manually, look at the Example section
    (6) at <http://www.faqs.org/rfcs/rfc1867.html>

    In other words, you need to mimic the headers of browser as long at
    you're getting it done. You may also enable verbose logging for easy
    debugging:

    $fp_err = fopen('verbose_ file.txt', 'ab+');
    fwrite($fp_err, date('Y-m-d H:i:s')."\n\n") ; //add timestamp to the
    verbose log
    curl_setopt($ch , CURLOPT_VERBOSE , 1);
    curl_setopt($ch , CURLOPT_FAILONE RROR, true);
    curl_setopt($ch , CURLOPT_STDERR, $fp_err);

    --
    <?php echo 'Just another PHP saint'; ?>
    Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

    Comment

    Working...