PHP Files Downloader (Needs A Files Sequence Or List)

Tags And Keywordsfree php scripts, php files downloader


Subject: PHP Files Downloader (Needs A Files Sequence Or List)
Today I would like to share with you a small script I coded yesterday because I found a good resources site to download from.

I noticed that all files where named sequentially: 'file_001.zip', 'file_002.zip', ... 'file_xxx.zip'

Instead of downloading files one by one by manually clicking and load pages, I decided to go using PHP.

Here are the instructions and the code:
  • First make sure you have a local server on your pc or a remote server with PHP running.
  • Suppose you need to download a lof of files which are named sequentially from website: 'file_001.zip', 'file_002.zip', ... 'file_xxx.zip'.
  • Create on your local (or remote) server a folder called "downloader" (or any name you like) and put this file in that folder:
    Code: (downloader.php) [Download] [Hide] [Select]
    <?php
    /**
    *
    * @package Icy Phoenix
    * @version $Id$
    * @copyright (c) 2008 Icy Phoenix
    * @license http://opensource.org/licenses/GPL-license.php GNU Public License
    *
    */

    @set_time_limit(0);
    //@ignore_user_abort(true);
    @ini_set('memory_limit', '64M');

    page_header('Files Downloader');

    $folder_src = 'http://www.targetsite.com/';
    $folder_tgt = 'files/';

    $filename_base_src = 'file_';
    $filename_base_tgt = 'file_';


    $files_src = array();
    $files_tgt = array();

    echo('<br /><br /><br /><h1>Files</h1>');
    flush();
    echo('<ul>');

    for ($i = 1; $i < 1000; $i++)
    {
    $filename_suffix = str_pad($i, 3, '0', STR_PAD_LEFT);
    $filename_src = $filename_base_src . $filename_suffix . '.zip';
    $filename_tgt = $filename_base_tgt . $filename_suffix . '.zip';

    $filename_full_path_src = $folder_src . $filename_src;
    $filename_full_path_tgt = $folder_tgt . $filename_tgt;

    //if (@remote_file_exists($folder_src, $filename_src) && @!file_exists($filename_full_path_tgt))
    if (@!file_exists($filename_full_path_tgt))
    {
    $files_src[] = $filename_full_path_src;
    $files_tgt[] = $filename_full_path_tgt;
    $copy_result = @copy($filename_full_path_src, $filename_full_path_tgt);
    echo('<li style="font-size: 9px;">Copying: <b>' . $filename_tgt . '</b> ยป ' . (!empty($copy_result) ? '<span style="color: #228822;">OK</span>' : '<span style="color: #dd2200;">ERROR</span>') . '</li>');
    flush();
    }

    }

    echo('</ul>');

    echo('<br /><br /><br /><b>Work Complete!!!</b>');
    page_footer();
    exit;

    function page_header($page_title)
    {
    $encoding_charset = 'UTF-8';
    echo('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n");
    echo('<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">' . "\n");
    echo('<head>' . "\n");
    echo(' <meta http-equiv="Content-Type" content="text/html; charset=' . $encoding_charset . '" />' . "\n");
    echo(' <meta name="author" content="Luca Libralato" />' . "\n");
    echo(' <title>' . $page_title . '</title>' . "\n");
    echo('</head>' . "\n");
    echo('<body>' . "\n");
    echo('<span><a name="top"></a></span>' . "\n");
    }

    function page_footer()
    {
    echo('<span><a name="bottom"></a></span>' . "\n");
    echo('</body>' . "\n");
    echo('</html>');
    }

    function remote_file_exists($dir, $file)
    {
    $ret = exec('ls ' . $dir . ' | grep ' . $file);
    return (!empty($ret));
    }

    ?>

  • I have highlighted the portion of codes you will need to adjust, in particular you need to edit the remote folder where the files are $folder_src = 'http://www.targetsite.com/'; and the base name of the file $filename_base_src = 'file_';
  • After having edited the file, you need to create a subfolder called files in the folder you just created (if you have created a folder called downloader, you will then have this path: downloader/files): make sure the files folder has CHMOD 0777 permissions.
  • If everything is fine you can then launch the file in your browser and just wait that it finishes to download all your files!


Enjoy!

Subject: Re: PHP Files Downloader (Needs A Files Sequence Or List)
Isn't that slow ? I can't remember exactly but cross-domain request are slow in PHP (and most because of the server) AFAIK.

Profile PM  
Subject: Re: PHP Files Downloader (Needs A Files Sequence Or List)
Do you think is faster clicking on 327 files manually? :LOL:

Subject: Re: PHP Files Downloader (Needs A Files Sequence Or List)
Surely not, but using a real langage should be faster, even just for copying. I may benchmark if I find time to update my os then install VS.

Profile PM  

Page 1 of 1


  
You cannot post new topics
You cannot reply to topics
You cannot edit your posts
You cannot delete your posts
You cannot vote in polls
You cannot attach files
You can download files
You cannot post calendar events

   

This is a "Lo-Fi" version of our main content. To view the full version with more information, formatting and images, please click here.

Powered by Icy Phoenix based on phpBB
Generation Time: 1.3064s (PHP: 1% SQL: 99%)
SQL queries: 31 - Debug Off - GZIP Enabled