Harts
Harts

Reputation: 4093

PHP socket programming

I'm really new in client-server and socket programming.

Is it possible to write client-server file transfer and upload large file (4GB+) using php socket programming? Or should I use php ftp?

Upvotes: 6

Views: 4652

Answers (3)

Trent
Trent

Reputation: 87

You could with sockets. However, you would save so much time by just using PHP's built in FTP functions. However, if you want to learn more about sockets here are some links.

http://php.net/manual/en/book.sockets.php

http://beej.us/guide/bgnet/output/print/bgnet_USLetter.pdf

http://www.devshed.com/c/a/PHP/Socket-Programming-With-PHP/

Upvotes: 2

jakx
jakx

Reputation: 758

Of course it's possible. If you can send it over http you can use sockets. You just have to specify the right headers describing the data you want to send across and then send the bytes across. Here's some example code:

$fp = fsockopen("localcook", 80, $errno, $errstr, 30);
$outbody = getPureJSON();
//$out = getHeaders2($outbody);
$out = getKOHeaders($outbody);
echo "Message Sent:<br/>";
echo $out;
fwrite($fp, $out);
echo "<br/>Message Received:<br/>";
while (!feof($fp)) {
    echo fgets($fp, 128);
}

fclose($fp);

Hope this helps!

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270599

Though it should be possible to write both the client & server parts with sockets in PHP, I would almost certainly opt for PHP's FTP extension, as most of the work handling files and building/tearing down connections is already done for you. http://www.php.net/manual/en/book.ftp.php

Upvotes: 5

Related Questions