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
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
Mobile Dev World

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 : Grab images from one or more URLs and save them to a specified local directory.
Categories : PHP, Filesystem, Strings, Arrays Click here to Update Your Picture
Aris Karidis
Date : Jan 19th 2006
Grade : 4 of 5 (graded 10 times)
Viewed : 10959
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Aris Karidis
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php

# This code was requested by Ori Lahav #

#########################################################################
# imageGrabber.php v1.0                                                 #
# -----------                                                           #
# Copyright (C) 2005 Aristidis Karidis, aris.karidis@bcs.org            #
# ----------------------------------------------------------            #
# This function grabs the images from one or more URLs and saves them   #
# to a specified local directory.                                       #
#                                                                       #
#########################################################################
#                                                                       #
# This program is free software; you can redistribute it and/or         #
# modify it under the terms of the GNU General Public License           #
# as published by the Free Software Foundation; either version 2        #
# of the License, or (at your option) any later version.                #
# This program is distributed in the hope that it will be useful,       #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
# GNU General Public License for more details.                          #
# ------------------------------------                                  #
# http://www.gnu.org/copyleft/gpl.html                                  #
#########################################################################
   
/**
* This function mines the image URLs form one or more webpages, returning an array of links.
*
* @param string $url
* @param int $unique
* @return array
*/
function imageGrabber($url, $unique = 1)
{
   
$startTag = '<img';
   
$srcTag = 'src=';
   
$endTag = '>';
   
$counter = 0;
   
    if(!
is_array($url))
    {
       
$url = array($url);
    }
   
    if (
$unique !== 0 && $unique !== 1)
    {
       
printf('Invalid parameter for $unique. The parameter must be either 1 or 0.');
        exit();
    }
   
    foreach (
$url as $value)
    {
       
$contents = file_get_contents($value);
       
       
$domain = $value;
       
$domain = substr($domain, 7);
       
$pos = stripos($domain, '/');
       
        if (
$pos)
        {
           
$domain = substr($domain, 0, stripos($domain, '/'));
        }
       
        while (
$contents)
        {
           
set_time_limit(0);                                    # In case we have several large pages
           
           
$quotes = array('"', "'", "\n");
           
$contents = str_replace($quotes, '', $contents);    # Strip " and ' as well as \n from input string
           
$contents = stristr($contents, $startTag);            # Drop everything before the start tag '<img'
           
$contents = stristr($contents, $srcTag);            # Drop everything before the 'src'
           
           
$endTagPosition = stripos($contents, $endTag);        # Position of the end tag '>'
           
$src = substr($contents, 4, $endTagPosition - 4);    # Get everything from src to end tag --> 'src="path" something>'
           
           
$spacePosition = stripos($src, ' ');                # Position of space (if it exists)               
           
           
if ($spacePosition !== false)
            {
               
$src = substr($src, 0, $spacePosition);            # Drop everything after space, keeping 'src="path"'
           
}
           
           
$questionMarkPosition = stripos($src, '?');
           
            if (
$questionMarkPosition !== false)
            {
               
$src = substr($src, 0, $questionMarkPosition);    # Remove any part after a '?'
           
}
           
           
$contents = stristr($contents, $endTag);            # Drop everything before the end tag '>'
           
           
if ($src)
            {
                if (
stripos($src, '/') === 0)
                {
                   
$src = 'http://'.$domain.$src;                # Relative link, so add domain before '/'
               
}
                else
                {
                    if (
stripos($src, 'http://') !== 0 && stripos($src, 'https://') !== 0 && stripos($src, 'ftp://') !== 0)
                    {
                       
$src = 'http://'.$domain.'/'.$src;        # Relative link, so add domain and '/'
                   
}
                }
               
               
$paths[] = $src;
            }
        }
       
        if (
$unique === 1)
        {
           
$results[] = array_unique($paths);        # Create final array with unique $paths
       
}
        else
        {
           
$results[] = $paths;                    # Create final array with all $paths
       
}
       
       
$paths = array();                            # Reset links
       
$counter++;                                    # Increment counter
   
}
   
    return
$results;
}
   
/**
* This function will downlaod and save all images on the specified directory.
*
* @param array $results
* @param sting $localPath
*/
function saveImages($results, $localPath = 'C:\\Temp\\test\\')
{       
    foreach (
$results as $v)
    {           
        foreach (
$v as $value)
        {
           
set_time_limit(0);
       
           
$path = $value;
           
            if (!
file_exists($localPath))
            {
               
mkdir($localPath);                    # Create the dir if it doesn't exist
           
}
           
           
$localFile = $localPath.basename($path);
           
            if (!
copy($path, $localFile))
            {
                   echo
"<font color=red>Failed to copy $path</font><br>";
            }
            else
            {
                echo
"<font color=blue>Successfully copied $path</font><br>";
            }
        }
    }
}
?>

<html>
<head>
</head>
<body>
    <?php
        $array
= array('http://www.weberdev.com', 'http://www.google.com', 'http://www.php.net',
                       
'http://www.zend.com', 'http://www.phparch.com', 'http://www.bbc.co.uk');
       
$links = imageGrabber($array);
       
saveImages($links);
   
?>
</body>
</html>



How to ifconfig down/up a list of IP's
Categories : Arrays, Strings, Filesystem, PHP
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
Compare two texts and display a block of text with the differences between them.
Categories : PHP, PHP Classes, Filesystem, Strings, Arrays
Link Submition - Allow your visitors to submit links to the site.
Categories : PHP, Arrays, Filesystem, Beginner Guides
WWW interface to Unix Manual(phpMan)
Categories : Program Execution, Strings, Arrays, PHP
How to find the name of the current file?
Categories : PHP, Filesystem, Strings
Get TemplateMonster data
Categories : Arrays, Ecommerce, PHP, Strings
Can the word DO be used in arrays?
Categories : Arrays, PHP, Strings
Parse string to find sub-string between two arbitrary strings
Categories : PHP, Strings, HTML and PHP, Arrays
Read a file with strings and create a new file with the first half of each string
Categories : PHP, Strings, Filesystem
How to Get a character array from a string
Categories : PHP, Strings, Arrays
Takes an array and returns a string, suitable for inputing in an SQL statement
Categories : Arrays, Strings, PHP
Working with files - return an array of files within a directory
Categories : PHP, Strings, Variables, Filesystem
Single-file PHP news system with automatic folder structure creation
Categories : PHP, Filesystem, Arrays
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
 Daniel Guldstrand wrote : 1827
I really like this code, thanks.

Is it possible to grab images even from 2 level depth? *hard to explain*
Otherwise.. it´s only getting the d*mn thumbs.  =)

Again... thanks!
 
 Vivek Dubey wrote :1893
Thanks for this precious code