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.
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.
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
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).
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.
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).
OK fingers crossed... This is one of the country's major hosts so I hope not all their clients go down
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!
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);
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.
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.