Re: Download and Write a Graphic
| From: | David Robley | Date: | Wed, 11 Oct 2000 06:20:19 +0000 |
| Subject: | Re: Download and Write a Graphic | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-19520@lists.php.net to get a copy of this message | ||
On Wed, 11 Oct 2000, Dan Wright wrote:
> Greetings!
>
> I am trying to download a graphic with either fopen() or file() and then
> write it to a local file on the server. What I'm trying to do is grab a
> weather graphic that's updated every 30 minutes.
>
> This will eliminate the need for the server to rely on that server being up
> to display the image and it will also allow the graphic to be displayed on
> https:// pages without displaying a "This page contains
> secure and unsecure
> items" message on SSL pages.
>
> Here's what I have now:
> $url = "http://www.graphics.com/graphic.gif";
> $fp = fopen($url, "r");
> if(!$fp) {
> echo "Error: Cannot connect";
> } else {
> $output = fgets($fp, 4096);
> fclose($fp);
Here you are returning the first (4k-1) of the file or up to the first
newline or to the end of the file, whichever comes first. I suspect that's
not exactly what you want. Given that you probably don't know how big the
image is, and can't be sure that there isn't an EOL character in the binary
data, and it's binary data and I'm not sure that fgets is binary safe, you
probably should do something like
} else {
$output = fread($fp,999999);
fclose($fp);
to get the file. Less typing and binary safe :-)
> }
> $filename =
> "/usr/home/inetwor6/www/vhosts/bocaratonchamber.com/graphics/weather/current
> .gif";
> $fd = fopen("$filename", "w+");
> fwrite($fd, $fp);
Here you are trying to write the file pointer from the 'get file' process
to the file you've opened locally; you probably want to write the contents
of $output, which theoretically contains your image.
>
> I've tried many combinations of the above and imagine one of you has done
> this and has a snippet solution.
>
> Thanks in advance,
> Dan
All untested, but may get you going.
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA