Binary file copy
| From: | Jay Moore | Date: | Thu, 16 Oct 2008 17:58:04 +0000 |
| Subject: | Binary file copy | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-281992@lists.php.net to get a copy of this message | ||
Greetings list!
Say I want to copy a jpg from a remote server onto mine, using PHP. Right now, my script opens a socket to the remote server, and opens the image file. It copies its contents into a dummy variable, opens a new file on my server, and dumps the contents of the dummy variable into the new file.
For reasons I cannot figure out, it is not working the way I want. Rather than display the image, I get an nothing when opening it in an image viewer.
Code follows:
------
<?php
$ip = '10.10.10.3';
$port = '80';
$h = @fsockopen($ip, $port, $err, $str, 5);
if ($h)
{
// Get current image
$h = @fsockopen($ip, $port, $err, $str, 5);
$out = "GET /record/current.jpg HTTP/1.1\r\n";
$out .= "Host: $ip\r\n";
$out .= "Connection: Close\r\n\r\n";
fputs($h, $out, strlen($out));
$data = '';
while (!feof($h))
{
$data .= fgets($h, 128);
}
fclose($h);
// Store to file
$f = fopen('/path/test.jpg', "wb");
if ($f)
{
fwrite($f, $data, strlen($data));
fclose($f);
}
else
{
die('cannot open file for writing');
}
}
else
{
die('cannot contact server');
}
?>
---------
I have trimmed the code some, and omitted the part where I remove the HTTP headers and other information I do not need.
Why isn't this working for me?
Thanks in advance,
Jay