Re: post thru fsockopen

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tuesday 17 April 2007 10:46:58 Joakim Ling wrote:
> Hi there
>
> Im tring to send a file  with a php script, Im using php 5.2 on a Win
> 2003 server. At the other end I just save the file and process the data
> and print the result, then fetch the output and write a report. But this
> doesn't work, any one got an idea? The file never get to the other end?
> When I do a var_export($_POST) is empty.
>
>
> This is the code I use:
> function postFile($file,$host,$path)
> {
> $remote_page = "http://".$host.$path;
> $boundary =
> "---------------------------".substr(md5(rand(0,32000)),0,10);
>
> // read the file
> $fp = @fopen($file,"r");
> if (!$fp) die ("Cannot read $file !!");
> $content_file = fread($fp,filesize($file));
> fclose($fp);
>
> // define HTTP POST DATA
> $data = "--$boundary\n".
> "Content-Disposition: form-data; name=\"file\"; filename=\"$file\"\n".
> "Content-Type: application/octet-stream\n\n$content_file".
> "--$boundary--\r\n\r\n";
>
> $msg = "POST $remote_page HTTP/1.0\n".
> "Content-Type: multipart/form-data; boundary=$boundary\n".
> "Content-Length: ".strlen($data)."\r\n\r\n";
>
> // Open socket connection ...
> $f = fsockopen($host,80, $errno, $errstr, 30);
> if ($f)
> {
>
> // Send the data
> fputs($f,$msg.$data);
>
> // retrieve the response
> $result="";
>
> while (!feof($f)) {
> $result.=fread($f,1024);
> }
>
> fclose($f);
> // write the response (if needed)
> return $result;
> } else {
> die ("Cannot connect !!!");
> }
> }

If you can use file_get_contents() with the allow_url_fopen = yes directive in 
php.ini it would be easier.
>From http://us2.php.net/manual/en/wrappers.http.php :
<?php

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

?>

I have done something similar except I used the $_REQUEST variable in the 
remote script rather than doing a POST. I had just a small amount of data to 
pass so $_REQUEST worked for me.

On Windows with PHP4 I had to do some tricks to get this type of thing to 
work. On Linux with PHP5 it worked flawlessly.

Thanks,
James

-- 
James Crow

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux