Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    160
    Thanks
    10
    Thanked 1 Time in 1 Post

    File not downloading

    Hi

    I found this script online but it only seems to display the code within the browser. I have the script installed in /scripts/analytics

    Can anyone see why its not saving the file

    Code:
    <?php
    // Remote file to download
    $remoteFile = 'http://www.google-analytics.com/urchin.js';
    // Local File name. Must be made writable
    $localFile = "local-urchin.js";
    // Time to cache in hours
    $cacheTime = 24;
    // Connection time out
    $connTimeout = 10;
    // Use Gzip compression
    $useGzip = false;
    
    if($useGzip){
    	ob_start('ob_gzhandler');
    }
    
    if(file_exists($localFile) && (time() - ($cacheTime * 3600) < filemtime($localFile))){
    	readfile($localFile);
    }else{
    	$url = parse_url($remoteFile);
    	$host = $url['host'];
    	$path = isset($url['path']) ? $url['path'] : '/';
    
    	if (isset($url['query'])) {
    		$path .= '?' . $url['query'];
    	} 
    	
    	$port = isset($url['port']) ? $url['port'] : '80';
    
    	$fp = @fsockopen($host, '80', $errno, $errstr, $connTimeout ); 
    
    	if(!$fp){
    		// On connection failure return the cached file (if it exist)
    		if(file_exists($localFile)){
    			readfile($localFile);
    		}
    	}else{
    		// Send the header information
    		$header = "GET $path HTTP/1.0\r\n";
    		$header .= "Host: $host\r\n";
    		$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\r\n";
    		$header .= "Accept: */*\r\n";
    		$header .= "Accept-Language: en-us,en;q=0.5\r\n";
    		$header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
    		$header .= "Keep-Alive: 300\r\n";
    		$header .= "Connection: keep-alive\r\n";
    		$header .= "Referer: http://$host\r\n\r\n";
    
    		fputs($fp, $header);
    
    		$response = ''; 
    		// Get the response from the remote server
    		while($line = fread($fp, 4096)){ 
    			$response .= $line;
    		} 
    
    		// Close the connection
    		fclose( $fp );
    
    		// Remove the headers
    		$pos = strpos($response, "\r\n\r\n");
    		$response = substr($response, $pos + 4);
    
    		// Return the processed response
    		echo $response;
    
    		// Save the response to the local file
    		if(!file_exists($localFile)){
    			// Try to create the file, if doesn't exist
    			fopen($localFile, 'w');
    		}
    		if(is_writable($localFile)) {
    			if($fp = fopen($localFile, 'w')){
    				fwrite($fp, $response);
    				fclose($fp);
    			}
    		}
    	}
    }
    ?>
    Thanks
    Roy

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by ROYW1000 View Post
    I found this script online but it only seems to display the code within the browser.
    Have you got this installed on your local PC in a wamp setup? If so you need to call it via the http://localhost/ address not file:// as it will not go through the webserver and php.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •