473,668 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!
Jul 17 '05 #1
3 4868
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=$bound ary\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
14519
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 headers). I can see this because I'm running Ethereal and watching the packets. I'm defining the packets as shown below: $text_msg = "Hello, world\r\n"; $binary_msg = chr(0x01).chr(0x02).chr(0x03).chr(0x00).chr(0xA0); $binary_msg_size = 5;
2
4051
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 that will test the three main forms of connecting externally: fsockopen, fopen and file. I wrote a simple script that connects to my own domain (http://www.kigurumi.co.uk) and to another external domain (http://www.yahoo.co.uk), I do nothing...
5
2294
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; include(http://www.anotherserver.com/foldername/phpfiletocall.inc); The .inc file is php formatted. Variables are passed to it (not via GET or POST though) and returned using return(variablenames, etc);
9
18692
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
12138
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 without the user having to wait. I'm doing this (with fsockopen) by sending an http request to a page that does the time consuming stuff and then immediatly closing, without having read any of the response. This way, I don't have to wait for the...
9
4917
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 strReturnData As String = _ System.Text.Encoding.ASCII.GetString(receiveBytes)
3
8503
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 the $request variable. $host = "www.myhost.com"; $request = "/myurl.php?param1=1&param2=2&param3=3";
3
12972
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, but since I have to download the image from a remote URL with fsockopen(), I have it stored as a binary string. I've had a cursory glance at the data, but predictably the size is not stored in decimal format anywhere...
2
9783
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 array. Thus i'm converting a byte array to sbyte array and sending through web service. The converted SByte contains negative numbers wihch are resulting in an error "java.lang.ArrayIndexOutOfBoundsException: -106" Byte => SByte...error...
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8575
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2784
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.