I am trying to run a script on my hosting account to find photos that were stored in various directory so I can get the correct file path and then move them into a new directory based upon the gallery name.
I have it running but some of the galleries have almost 200 photos and the script stops working part way through. Not sure how to fix it.
[php]
<?php $servername = ""; $username = ""; $password = ""; $dbname = ""; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } set_time_limit(0); ignore_user_abort(1); ////////////////////////////////////////////////////////////////////////// //////////// SEARCH FUNCTION /////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// function rsearch($folder, $pattern) { $iti = new RecursiveDirectoryIterator($folder); foreach(new RecursiveIteratorIterator($iti) as $file){ if(strpos($file , $pattern) !== false){ return $file; } } return false; } ////////////////////////////////////////////////////////////////////////// $sql = "SELECT id, name FROM prc_igallery order by id ASC"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. ""; $gallery_id = $row["id"]; //$gallery_id = "7"; $gallery_name = $row["name"]; //The name of the directory that we need to create. $directoryName = $gallery_name; //Check if the directory already exists. if(!is_dir($directoryName)){ //Directory does not exist, so lets create it. mkdir($directoryName, 0755); } /////////////////////////////////////////////////////////////////////////////////////////////////// // NOW FIND INDIVIDUAL PHOTOS FOR EACH GALLERY///////////////////////////////////////////////////// $sql2 = "SELECT * FROM prc_igallery_img where gallery_id = $gallery_id order by id ASC"; $result2 = $conn->query($sql2); $rowcount=mysqli_num_rows($result2); printf("Result set has %d rows.\n",$rowcount); echo("
"); if ($result2->num_rows > 0) { // output data of each row while($row2 = $result2->fetch_assoc()) { echo "gallery id: " . $row2["gallery_id"]. " - Photo: " . $row2["filename"]. "
"; //////////////////////////////////////////////////////////////////////////////////////////////////// //// FIND FILE IN IGALLERY DIRECTORY ////////////////////////////////////////////////////////////// $folder = "/home/content/r/c/p/quake/html/images/igallery/original/"; $pattern = $row2["filename"]; /////////////////////////////// /// call search function //// ////////////////////////////// $filepath = rsearch($folder, "/".$pattern); ///////////////////////////////////////////// echo $filepath; echo ("
"); unset($folder); unset($pattern); unset($filepath); //sleep(2); //////////////////////////////////////////////////////////////////////////////////////////////////// } } else { echo "0 results"; } $gallery_id=""; // Free result set mysqli_free_result($result2); echo "
"; /////////////////////////////////////////////////////////////////////////////////////////////////// } } else { echo "0 results"; } $conn->close(); ?>
[/php]