13 if($url == "" || $file == "") {
14 echo "need a url and file\n";
18 // first check any directories in $file are in existence
19 $splfile = explode("/", $file);
20 if(count($splfile) > 1) {
22 for($i = 0; $i < count($splfile)-1; $i++) {
23 $tomake .= "/".$splfile[$i];
24 echo("making directory $tomake");
25 if(!is_dir($tomake)) mkdir($tomake);
30 // ok, we kick off a download
31 if(file_exists("$file")) {
32 // a download exists, does it still work
33 error_log("DOWNLOADER: file exists for current download, hope it works, attempting lock");
34 $localtmpfh = fopen("$file", "r");
35 $lockres = flock($localtmpfh, LOCK_EX|LOCK_NB);
37 error_log("flock did fail, all is right with the world a download is in progress");
40 error_log("lock succeeded, dieing in the arse");
42 if(file_exists("$file.size")) unlink("$file.size");
46 $remotefile = fopen($url, "r");
48 // check if ther mote file started
50 foreach($http_response_header as $key => $val) {
51 if(preg_match("/HTTP.*404.*/", $val)) {
52 echo "got a 404 touching 404 file\n";
58 if(file_exists("$file.404")) {
63 $localfile = fopen($file, "w");
64 $lockres = flock($localfile, LOCK_EX);
66 echo "something went plop\n";
71 // get the headers from the remote request and use them to hurt people
75 // detecting a remote directory on a web session is near impossible, this is the best i can come up with
76 // - check for a redirection header, then look for a trailing / on the url... best of luck to me
77 foreach($http_response_header as $key => $val) {
78 if(preg_match("/.*Location:.*/", $val)) {
79 echo "got location as $val\n";
80 $realloc = preg_replace("/.*[: ]/", "", $val);
81 if(preg_match("/.*\/$/", $realloc)) {
89 // get content length form upstream and print
90 if(preg_match("/^Content-Length:.*/", $val)) {
91 $clentemp = preg_split("/[: ]+/", $val);
92 $contentlen = $clentemp[1];
95 // get content type from upstream and print
96 if(preg_match("/^Content-Type:.*/", $val)) {
105 file_put_contents("$file.size", $contentlen);
107 while(!feof($remotefile)) {
108 $data = fread($remotefile, 2048);
109 fwrite($localfile, $data);
112 //rename("$file.tmp.data.deleteme", $file);
113 unlink("$file.size");