473,231 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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="multipart/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 = "application/x-zip-compressed"; // the file mime type

srand((double)microtime()*1000000);
$boundary = "---------------------------".substr(md5(rand(0,32000)),0,10);

$data = "--$boundary";

for($i=0;$i<count($file_array);$i++){
$content_file = join("", file($file_array[$i]));

$data.="
Content-Disposition: form-data; name=\"$file_array[$i]\";
filename=\"$file_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=$boundary\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!
Jul 17 '05 #1
3 4834
Ot?vio wrote:
// ==== 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=$boundary\r\n";
$msg .= "Content-Length: ". strlen($data) ."\r\n\r\n";

echo $msg . "\n";


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

Jul 17 '05 #2
Janwillem Borleffs wrote:
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";


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:

http://www.jwscripts.com/playground/postdata.phps
JW

Jul 17 '05 #3
Hello,

on 02/11/2005 08:47 PM Ot?vio said the following:
Hi!

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


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:

http://www.phpclasses.org/httpclient
--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: coder_1024 | last post by:
I'm trying to send a packet of binary data to a UDP server. If I send a text string, it works fine. If I attempt to send binary data, it sends a UDP packet with 0 bytes of data (just the...
2
by: Loopy | last post by:
I'm trying to write a script that will connect to an external site so that I can get access referer information in a database on another one of my sites. I got errors, so I tried to write a script...
5
by: hntgzr | last post by:
I am trying to include a function in a .php file on a different server from the main .php files. I am using: include_path=http://www.anotherserver.com/foldername;...
9
by: fipaj1992 | last post by:
Hi! I have very big problem with fsockopen() function. It is very important... That's code: <?php $test = fsockopen ('chrome.pl', '8080'); ?>
4
by: yawnmoth | last post by:
Is it possible to send http requests with curl but not have curl wait for the response? The reason I ask is because I'd like to code a web app that can sorta start time consuming processes...
9
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim...
3
by: Aetherweb | last post by:
Hi, I need to use fsockopen to open a connection, send a GET request and read in the response. At the moment I'm trying the following code, but I think it's ignoring the actual URL included in...
3
by: Christoph Burschka | last post by:
Is there some way to get the dimensions of an image, given the binary data of the image, without having to write it to a temporary file? It seems that getimagesize() will only take a filename,...
2
by: ksheerasagar17 | last post by:
Hello All, Scenario: Sending an image through webservice as byte array to an Java webservice. The Problem1: The webservice method image property expects (data type) SByte rather than Byte...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.