WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Remote File Saving with PHP - Download and serve a remote file. The content of the file will be updated at fixed intervals.
Categories : PHP, Filesystem, Cache, Sockets, HTTP Click here to Update Your Picture
Sumit Agarwal
Date : Nov 24th 2007
Grade : 5 of 5 (graded 1 times)
Viewed : 2469
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Sumit Agarwal
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php

// File to download
$remoteFile = 'http://www.yahoo.com/';

// Local file for saving
$localFile = "YahooHome.htm";

// Time to cache in hours
$cacheTime = 24;

// Connection time out
$connTimeout = 10;

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){
         
// If connection failed, return the cached file
         
if(file_exists($localFile)){
               
readfile($localFile);
          }
     }else{
         
// Header Info
         
$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";

           
$response = '';
         
fputs($fp, $header);
         
// Get the file content
         
while($line = fread($fp, 4096)){
               
$response .= $line;
          }
         
fclose( $fp );

         
// Remove Header Info
         
$pos = strpos($response, "\r\n\r\n");
         
$response = substr($response, $pos + 4);
          echo
$response;

         
// Save the file content
         
if(!file_exists($localFile)){
               
// Create the file, if it doesn't exist already
               
fopen($localFile, 'w');
          }
          if(
is_writable($localFile)) {
               if(
$fp = fopen($localFile, 'w')){
                   
fwrite($fp, $response);
                   
fclose($fp);
               }
          }
     }
}

?>



Remote File Size
Categories : PHP, Filesystem, HTTP, Sockets
Simple pipe delimited file export program that downloads to a local machine
Categories : PHP, Filesystem, Databases, MySQL, HTTP
Class that allows the PHP developer to create and manage UNIX like password files suitable for use as Apache authentication password files.
Categories : HTTP, PHP, PHP Classes, Filesystem
PHP Domain Availability Checker
Categories : PHP, Complete Programs, Regexps, HTTP, Sockets
Easy upload class
Categories : PHP Classes, Filesystem, HTTP, PHP
Force file download
Categories : PHP, Filesystem, HTTP, Program Execution
How to let a user download a picture by clicking on it instead of needing to right click and Save-As.
Categories : HTTP, PHP, HTML and PHP, Filesystem
Using php3 to upload files, uploading files, file uploads.
Categories : PHP, Filesystem, HTTP
Random Image Display
Categories : PHP, Filesystem, Graphics, HTML and PHP
PHP based Contact email form with multiple recipients, text file based, supports departments.
Categories : PHP, Email, Beginner Guides, Filesystem
accept_connect -- Accepts a connection on a socket
Categories : PHP, PHP Functions, Sockets
The toll booth
Categories : PHP, Java Script, Filesystem
php jump urls...the best way
Categories : PHP, URLs, Filesystem
Simple way to replace a variable value in a .conf (.ini) file using a webbrowser - the first stage of a complete universal configuration editor
Categories : PHP, Regexps, Code Editors, Filesystem
Display list of files within current and subdirectories (recursively) showing each file as an anchored link and each directory as a category header.
Categories : Filesystem, Directories, Arrays, PHP