Advertisement
christopherreay

Builds image moving script

Aug 12th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. <?php
  2. //// CONNECT TO DATABASE ////
  3. include 'h/1.c/db_connect.php';
  4. include 'i/global_variables.php';
  5. /////////////////////////////
  6.  
  7. //Store the table name to make it easy to move this code to another Site
  8. $table_name = "antiquesnow_ads";
  9. //Base URL for downloads. Trailing "/" is required
  10. $base_url   = "http://www.antiques-now.co.uk/antiques-for-sale-images/";
  11.  
  12. //SQL Query to get all picture filenames from teh DB
  13. $result_category = mysql_query("SELECT * FROM $table_name WHERE live >= 1 AND ((pic_name IS NOT NULL)  OR (pic_name != ''))");
  14.  
  15. //store the field names in an array so we can iterate through them with "foreach"
  16. $field_names = array("pic_name", "pic_name2", "pic_name3", "pic_name4");
  17.  
  18. //Container for the " " separated list of all the image names
  19. $p_wget_strings = array();
  20. for ($i = 0; $i < 20; $i++)
  21. { $p_wget_strings[$i] = "";
  22. }
  23. //Counter
  24. $counter = 0;
  25. $p_counter = 0;
  26. //Loop through all the results from the database, and build the string of filenames
  27. while ( $row = mysql_fetch_array($result_category) )
  28. { foreach ($field_names as $field_name)
  29.   { //Store the value of this field for this record
  30.     $field_value = $row[$field_name];
  31.     //Check if this field_value is NULL or Emtpy
  32.     if (    $field_value != NULL
  33.         &&  $field_value != ""
  34.        )
  35.     { //Add the field_value to the great big string, with the url prefix
  36.       $p_wget_strings[$p_counter] .= " \n".$base_url.$field_value;
  37.       //Increment parrallel counter
  38.       $p_counter++;
  39.       //limit to 20 parrallel wget instances
  40.       if ($p_counter == 20)
  41.       { $p_counter = 0;
  42.       }
  43.       //Increment Counter
  44.       $counter++;
  45.     }
  46.   }
  47. }
  48.  
  49. $wget_input_file_dir = "~/imageURLs/$table_name";
  50. //build the script
  51. $script_string = "echo About to copy $counter items from $base_url. Y to continue [n]\n";
  52. $script_string .= "read\n";
  53. $script_string .= "if [ \$REPLY = \"Y\" ]\n";
  54. $script_string .= "then\n";
  55. $script_string .= "mkdir -p $wget_input_file_dir\n";
  56. $counter = 0;
  57. foreach ($p_wget_strings as $wget_instance)
  58. { $counter ++;
  59.   $filename = "imageURLs_$counter";
  60.   echo $filename."\n";
  61.   $full_file_path = $wget_input_file_dir."/".$filename;
  62.   $fileRes = fopen($full_file_path, "w");
  63.   fwrite($fileRes, $wget_instance);
  64.   fclose($fileRes);
  65.   $script_string .=  "  wget -i $full_file_path -o ${wget_input_file_dir}/logfile_$counter &";
  66. }
  67. $script_string .= "fi\n\n";
  68.  
  69. //output to the browser
  70. echo $script_string;
  71. //Flush output cache
  72. flush();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement