I'm trying to download an image and save it to my server. The image is to be stored in cache to be further manipulated, but I'm having a problem getting the file to download to the directory. The file shows up in the directory, but it's a 0 byte file and contains no data.
I'm using a patchwork of scripts. This is mainly part of one file that works OK for text, but it's giving me problems for images. I'm a php idiot really and need some help, since I really have no idea what I'm doing. If anyone has any insight, it would be appreciated.
<?php //WIND @ FBIS1 WOHOO
$stationNumber = "TR";
$stationName = "TR";
$maxReadings = 1;
$backend = "https://www.fnmoc.navy.mil/products/WW3/ww3.b.natl.sig_wav_ht.000.gif";
$cache_file = "tmp/buoydata.".$stationName.".gif";
$timeout = 10;
// # Cache file time-based naming stuff
$cache_time = 21600; #Images are only updated every 4 hours so cache the current reading until the next is available
$time = split(" ", microtime());
srand((double)microtime()*1000000);
$cache_time_rnd = 300 - rand(0, 600);
if ( (!(file_exists($cache_file))) || ((filectime($cache_file) + $cache_time - $time[1]) + $cache_time_rnd < 0) || (!(filesize($cache_file))) ) {
$url = parse_url($backend);
$fp = fsockopen($url['host'], "443", &$errno, &$errstr, $timeout);
if(!$fp) {
echo "NO DATA";
return;
} else {
$fpread = @fopen($backend, 'r');
if(!$fpread) {
echo "Buoy Down";
return;
} else {
$fpwrite = @fopen($cache_file, 'w');
if(!$fpwrite) {
echo "Cache Error";
return;
}
fclose($fpread);
}
fclose($fpwrite);
}
fclose($fp);
}
if (file_exists($cache_file)) {
include($cache_file);
}
?>