Jay Moore wrote:
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
in the response you'll be getting the raw http response (including
headers); so you're saving them as well thus not a valid image file.
can't see why:
$image = file_get_contents('http://10.10.10.3/record/current.jpg');
wouldn't work for you..
regardless though if you are using sockets, be sure to trim of that raw
http response - oh and look out for chunked or encoded file transfer as
well as you'll need to decode etc etc.. (large can of worms - use an
http transport class)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php