Sending a binary file (and other stuffs) with fsockopen

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ot?vio

    Sending a binary file (and other stuffs) with fsockopen

    Hi!

    I'm havin a problem about sending a binary file with fsockopen. My
    problem is solved when i do:

    -------------------->8------CODE-----------------------------
    <form action="200.120 ...../file.php" method="POST"
    enctype="multip art/form-data">
    <form name="file" />
    <form name="ip_01" />
    <form name="ip_02" />
    < (...) submit button (...)>
    </form>
    -------------------->8------CODE-----------------------------

    where file must be a binary file (in this case is a zip file) and
    ip_0X is a number.. Some ip from my internal network...

    But i was trying a script to do this automaticaly, like:


    -------------------->8------CODE-----------------------------
    <?

    $host='200.120. ....';
    $port=80;
    $path='/file.php';

    // Ip Values :)
    $ip_01 = "192.168... ."
    $ip_02 = someRandonIp();

    // File to POST
    $file = "file.zip"

    $file_array[0] = $file; // the file
    $content_type = "applicatio n/x-zip-compressed"; // the file mime type

    srand((double)m icrotime()*1000 000);
    $boundary = "---------------------------".substr(md5(ra nd(0,32000)),0, 10);

    $data = "--$boundary";

    for($i=0;$i<cou nt($file_array) ;$i++){
    $content_file = join("", file($file_arra y[$i]));

    $data.="
    Content-Disposition: form-data; name=\"$file_ar ray[$i]\";
    filename=\"$fil e_array[$i]\"
    Content-Type: $content_type
    $content_file
    --$boundary";
    }

    $data.="--\r\n\r\n";

    // ==== I think my error is here!!! ==== //
    $path = $path . "?ip_01=$ip_01& ip_02=$ip_02";

    $msg = "GET $path HTTP/1.0\r\n";
    $msg .= "Content-Type: multipart/form-data; boundary=$bound ary\r\n";
    $msg .= "Content-Length: ". strlen($data) ."\r\n\r\n";

    echo $msg . "\n";

    // open the connection
    $f = fsockopen($host , $port);
    fputs($f, $msg.$data);
    $result="";
    while (!feof($f)) $result .= fread($f,32000) ;
    fclose($f);

    echo $result . "\n";
    ?>
    -------------------->8------CODE-----------------------------


    file.php is not receiving ip_01 and ip_2 .. So i thin the problem is
    in:

    -------------------->8------CODE-----------------------------
    $path = $path . "?ip_01=$ip_01& ip_02=$ip_02";
    -------------------->8------CODE-----------------------------

    Any Ideia?
    Thanks!
  • Janwillem Borleffs

    #2
    Re: Sending a binary file (and other stuffs) with fsockopen

    Ot?vio wrote:[color=blue]
    > // ==== I think my error is here!!! ==== //
    > $path = $path . "?ip_01=$ip_01& ip_02=$ip_02";
    >
    > $msg = "GET $path HTTP/1.0\r\n";
    > $msg .= "Content-Type: multipart/form-data; boundary=$bound ary\r\n";
    > $msg .= "Content-Length: ". strlen($data) ."\r\n\r\n";
    >
    > echo $msg . "\n";
    >[/color]

    Yes, it probably is, because it looks like the only thing you want to do is
    to call the script with some parameters and get the response.

    For this, you don't need to send the Content-Type/Length headers; the
    following will suffice:

    $msg = "GET $path HTTP/1.0\r\n";
    $msg .= "Host: $host\r\n\r\n";


    JW



    Comment

    • Janwillem Borleffs

      #3
      Re: Sending a binary file (and other stuffs) with fsockopen

      Janwillem Borleffs wrote:[color=blue]
      > Yes, it probably is, because it looks like the only thing you want to
      > do is to call the script with some parameters and get the response.
      >
      > For this, you don't need to send the Content-Type/Length headers; the
      > following will suffice:
      >
      > $msg = "GET $path HTTP/1.0\r\n";
      > $msg .= "Host: $host\r\n\r\n";
      >[/color]

      Hmmm.. had another look at your message and realized that this reply isn't
      correct. You must think of the following rules if you want to send both
      parameters and binary data to a script pver HTTP:

      1. You can only use POST to send binary data (as in file uploads)
      2. Additional parameters should be included in seperate boundaries when they
      are part of a multipart message

      I have posted an example script, which might be helpful:




      JW



      Comment

      • Manuel Lemos

        #4
        Re: Sending a binary file (and other stuffs) with fsockopen

        Hello,

        on 02/11/2005 08:47 PM Ot?vio said the following:[color=blue]
        > Hi!
        >
        > I'm havin a problem about sending a binary file with fsockopen. My
        > problem is solved when i do:[/color]

        You should submit it with the POST method and the other variables should
        be also submitted in the request body.

        Anyway, you may want to try this HTTP client class that can submit a
        POST request with uploaded files and other form values correctly:




        --

        Regards,
        Manuel Lemos

        PHP Classes - Free ready to use OOP components written in PHP
        Free PHP Classes and Objects 2026 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials


        PHP Reviews - Reviews of PHP books and other products


        Metastorage - Data object relational mapping layer generator

        Comment

        Working...