13 if($url == "" || $file == "") {
14 echo "need a url and file\n";
19 // ok, we kick off a download
20 if(file_exists("$file")) {
21 // a download exists, does it still work
22 error_log("DOWNLOADER: file exists for current download, hope it works, attempting lock");
23 $localtmpfh = fopen("$file", "r");
24 $lockres = flock($localtmpfh, LOCK_EX|LOCK_NB);
26 error_log("flock did fail, all is right with the world a download is in progress");
29 error_log("lock succeeded, dieing in the arse");
35 $remotefile = fopen($url, "r");
36 $localfile = fopen($file, "w");
37 $lockres = flock($localfile, LOCK_EX);
39 erorr_log("something went plop");
42 // get the headers from the remote request and use them to hurt people
45 foreach($http_response_header as $key => $val) {
46 if(preg_match("/HTTP.*30[1-9].*/", $val)) {
50 // get content length form upstream and print
51 if(preg_match("/^Content-Length:.*/", $val)) {
52 $clentemp = preg_split("/[: ]+/", $val);
53 $contentlen = $clentemp[1];
56 // get content type from upstream and print
57 if(preg_match("/^Content-Type:.*/", $val)) {
66 file_put_contents("$file.size", $contentlen);
68 while(!feof($remotefile)) {
69 $data = fread($remotefile, 2048);
70 fwrite($localfile, $data);
73 //rename("$file.tmp.data.deleteme", $file);