Need script to download files from web unto server

Discussion in 'Programming' started by visio, Nov 18, 2007.

  1. #1
    I need a script which allows me to enter a url(of downloadable file/s) and the script will take it download and unzip it unto my server without using CPU resources.
    This is an easy task for anyone with beginner php knowledge and on...
     
    visio, Nov 18, 2007 IP
  2. st1905

    st1905 Well-Known Member

    Messages:
    573
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    135
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    If you want to download files from web remotely to your server, its extremely easy to do. Also if you have cpanel or another control panel then you can extract them easily again.

    <?php
      set_time_limit(0);
      ignore_user_abort();
      print "<p>Getting</p>";
      exec("wget here comes the url");
      print "<p>Done.</p>";
    ?> 
    Code (markup):
    Dont forget to chmod 777 the directory where you put this php file.
     
    st1905, Nov 18, 2007 IP
  3. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    If it's such an easy task then you should be able to do it ;) It's not the simplest thing for a beginner to figure out, but since I happen to have written a script that efficiently downloads a file (wget is easy as well but it's only on linux and sadly isn't always installed or accessible). If you want to unzip things, check php.net look for zip_open, zip_read, and zip_close, it's fairly straightforward.

    
    function downloadFile($remote, $local){
    	$file = @fopen($remote, 'r');
    	if (!$file)
    		return false;
    	$f = @fopen($local, 'w+');
    	if (!$f)
    		return false;
    	while (!feof($file))
    		fwrite($f, fread($file, 1024));
    
    	return true;
    }
    
    PHP:
    All you do is downloadFile('http://www.blah.com/file.txt', 'file.txt'); and it downloads the remote file and saves it as file.txt. It's a simple and easy function, enjoy
     
    projectshifter, Nov 18, 2007 IP
  4. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    NO NO NO NO NO :( Do NOT use wget. And if you WERE to use wget, you'd want to add -O file.name so it saves it as what you want. A lot of webhosts will disable exec or system, and I highly doubt he has a dedicated server on hand, although I could be wrong. You also have to make sure that wget is accessible from php, and that it is installed. It's an easy solution, but it's not an efficient one.
     
    projectshifter, Nov 18, 2007 IP
  5. st1905

    st1905 Well-Known Member

    Messages:
    573
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    135
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    Thanks for the information, yes your solution looks better.
     
    st1905, Nov 18, 2007 IP
  6. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #6
    Np man, when you've been doing this as long as I have, you pick up a few tricks now and again ;)
     
    projectshifter, Nov 18, 2007 IP