1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Lifting Images On-The-Fly

Discussion in 'PHP' started by T0PS3O, May 25, 2005.

  1. #1
    I have a website with a host through which I don't have FTP access to my own folders. Image uploads happen via a form.

    I can access the images directory and get a full listing.

    Now I want to lift all of them and dump them in a folder on my new host via PHP. Probably the built-in FTP function.

    Re-inventing the wheel is not my prefered business so I'd rather not code it.

    Any of you know of a script that does this out of the box?

    Thanks.
     
    T0PS3O, May 25, 2005 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    It shouldn't be terribly difficult... if you want to list the directory URL (or something that looks like it/uses the same HTML formatting) I could probably do it for you in a couple minutes.
     
    digitalpoint, May 25, 2005 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This List.

    Either dumped online or perhaps zipped up for download ready for manual FTP.

    It would take me about 1.5hr I guess. Impress me :)
     
    T0PS3O, May 25, 2005 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    What operating system are you running PHP on? Do you have curl functions available in PHP?
     
    digitalpoint, May 25, 2005 IP
  5. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #5
    If it's a machine that has curl available in the shell, you can run this (put this script in it's own folder):
    <?php
    
    	$base = "http://www.actiongear.co.uk/ishop/images/1016/";
    
    	$source = file_get_contents ($base);
    
    	preg_match_all("/HREF=\"(.*)\"/", $source, $matches);
    
    	foreach ($matches[1] as $file) {
    		if (substr ($file, -4) == ".jpg" || substr ($file, -4) == ".gif" || substr ($file, -4) == ".png") {
    			shell_exec ('curl -O "' . $base . $file . '"');
    			echo '.';
    		}
    	}
    
    ?>
    PHP:
    That should download all the images to the local folder the script is in (it can be on a remote machine, so you could run it on your new server for example).
     
    digitalpoint, May 25, 2005 IP
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, can do CURL. It's freeBSD PHP 4.3something. Will try that one thanks!
     
    T0PS3O, May 25, 2005 IP
  7. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Will shell_exec work on a shared host though?
     
    T0PS3O, May 25, 2005 IP
  8. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #8
    It should...
     
    digitalpoint, May 25, 2005 IP
  9. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It seems to be going already. Nice one!
     
    T0PS3O, May 25, 2005 IP
  10. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It times out though. Any chance of coding a quick 'next 30' button type solution?

    I'll get it started with a $_GET[per_run] = 30 and split it up in steps of 30 or so...

    Actually it did about 70 of them.
     
    T0PS3O, May 25, 2005 IP
  11. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #11
    This would *probably* work...
    <?php
    
    	if (!$argv) {
    	
    		shell_exec ('nohup php "' . $_SERVER['SCRIPT_FILENAME'] . '" > /dev/null &');
    
    	} else {
    		$base = "http://www.actiongear.co.uk/ishop/images/1016/";
    	
    		$source = file_get_contents ($base);
    	
    		preg_match_all("/HREF=\"(.*)\"/", $source, $matches);
    	
    		foreach ($matches[1] as $file) {
    			if (substr ($file, -4) == ".jpg" || substr ($file, -4) == ".gif" || substr ($file, -4) == ".png") {
    				shell_exec ('curl -O "' . $base . $file . '"');
    				echo '.';
    			}
    		}
    	}
    ?>
    PHP:
    But use at your own risk. :) It should spin off a separate background process on the server, so hitting it with a browser would return right away (just kick off the process).
     
    digitalpoint, May 25, 2005 IP
  12. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #12
    OK fingers crossed... This is one of the country's major hosts so I hope not all their clients go down :D
     
    T0PS3O, May 25, 2005 IP
  13. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #13
    That didn't work and nothing broke :)

    I'll do my split up bit of code...
     
    T0PS3O, May 25, 2005 IP
  14. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #14
    This is what I've made of it but I get an error :(

    
    <?php
    
        if(!isset($_GET['done']))
        {
        	$done = 0;
        }
        else 
        {
        	$done = $_GET['done'];
        }
    
        $target = $done + 60;
        
    	$base = "http://www.actiongear.co.uk/ishop/images/1016/";
    
        $source = file_get_contents ($base);
    
        preg_match_all("/HREF=\"(.*)\"/", $source, $matches);
    
        //print_r(array_values($matches));
        
        array_chunk($matches, 60);
        
        $x = $done/60;
        
        
        
        if(!is_int($x))
        {
        	$x = 0;
        }
        
        //echo'<h4>X is '.$x.'</h4>';
        
        //print_r(array_values($matches[$x]));
        
        echo $x;
        
        foreach ($matches[$x][1] as $file) {
            
        	if (substr ($file, -4) == ".jpg" || substr ($file, -4) == ".gif" || substr ($file, -4) == ".png") {
                shell_exec ('curl -O "' . $base . $file . '"');
                echo '.';
            }
            
        }
        
        echo'<p>On to the <a href="'.$PHP_SELF.'?done='.$target.'">next stage</a>.';
    
    ?> 
    
    PHP:
    Warning: Invalid argument supplied for foreach() in ...images/i2o_test.php on line 39

    Never works when you're in a hurry... Missing another first half football match, have to stay clear of Saucy Kettle!
     
    T0PS3O, May 25, 2005 IP
  15. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Does anyone know why the code above doesn't split up the array in chunks of 60?

    The array $matches isn't affected at all by array_chunk($matches, 60); :(
     
    T0PS3O, May 26, 2005 IP
  16. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #16
    There's a firefox plugin called flashgot that can do it... you just select a bunch of images and right click->flashgot->and choose one of the download managers.

    You need a download manager, like aria, freedownloadmanager, or speed download - and then you'll have to upload them.

    I realize that shawn's solution is a pretty good one, but just for future reference or for those who can't run curl.
     
    nevetS, May 26, 2005 IP
  17. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #17
    OK. Sat down with a fresh mindset and solved it now (I always hated arrays but am slowing starting to 'get' them).

    The following code is what Shawn provided (needs CURL enabled) but splits it up in chunks so your server won't time out. I set it to 40 images each time even though my server could probably do more.

    I added some array printing functions for feedback and educational purposes.

    Works a charm. Thanks Shawn!

    
    <?php
    
        if(!isset($_GET['done']))
        {
        	$done = 0;
        }
        else 
        {
        	$done = $_GET['done'];
        }
    
        $target = $done + 40; //Change 40 if fast server
        
    	$base = "http://www.image-directory.com"; //Change to target img directory
    
        $source = file_get_contents ($base);
    
        preg_match_all("/HREF=\"(.*)\"/", $source, $matches);
    
        $matches = array_chunk($matches[1], 40); //Change 40 if fast server
        
    function do_offset($level){
        $offset = "";             // offset for subarry
        for ($i=1; $i<$level;$i++){
        $offset = $offset . "<td></td>";
        }
        return $offset;
    }
    
    function show_array($array, $level, $sub){
        if (is_array($array) == 1){          // check if input is an array
           foreach($array as $key_val => $value) {
               $offset = "";
               if (is_array($value) == 1){   // array is multidimensional
               echo "<tr>";
               $offset = do_offset($level);
               echo $offset . "<td>" . $key_val . "</td>";
               show_array($value, $level+1, 1);
               }
               else{                        // (sub)array is not multidim
               if ($sub != 1){          // first entry for subarray
                   echo "<tr nosub>";
                   $offset = do_offset($level);
               }
               $sub = 0;
               echo $offset . "<td main ".$sub." width=\"120\">" . $key_val .
                   "</td><td width=\"120\">" . $value . "</td>";
               echo "</tr>\n";
               }
           } //foreach $array
        }  
        else{ // argument $array is not an array
            return;
        }
    }
    
    function html_show_array($array){
      echo "<table cellspacing=\"0\" border=\"2\">\n";
      show_array($array, 1, 0);
      echo "</table>\n";
    }    
           
        $x = $done/40; //Change 40 if fast server
        
        if(!is_int($x))
        {
        	$x = 0;
        }
            
        html_show_array($matches[$x]);
        
        foreach ($matches[$x] as $file) {
            
        	if (substr ($file, -4) == ".jpg" || substr ($file, -4) == ".gif" || substr ($file, -4) == ".png") {
                shell_exec ('curl -O "' . $base . $file . '"');
                echo '.';
            }
            
        }
        
        echo'<p>On to the <a href="'.$PHP_SELF.'?done='.$target.'">next stage</a>.';
    
        
        
    ?> 
    
    PHP:
    Thanks again. This is a useful little script I will have a need for loads more.
     
    T0PS3O, May 26, 2005 IP