File loop exceeding 30 second limit

Hi I am relatively new to PHP and have had some success using php.net for a lot of my scripting, however I am having trouble with this script I am using. Basically I have a large number of files in a folder and a database table that logs the entries when I parse them through, which will also allow me to see which files need updating when they become available. What I have done is created a script that identifies and returns the files that are not included in the list, which works up to a point, but when this list becomes too large the script ceases to function properly.

Here is my code:

[code]<?php

// Database credentials
include(‘config.php’);

// Function to list the file names, seperated by a comma
function direktorylist ($direkt) {
$files = “”;
$filenomArray = array();

// Look for all xml files type ‘s’ and put them into an array
$result = mysql_query(“SELECT u_xml FROM filebank WHERE file_type = ‘s’”);
while($row = mysql_fetch_array($result)) {
array_push($filenomArray,$row[‘u_xml’]);
}
// Open directory, check to see if the file already exists on the database,
// if not return them in a comma seperated string
if ($handle = opendir($direkt)) {
$file_names = “”;
while (false !== ($file = readdir($handle))) {
if ($file != “.” && $file != “…”) {
$file = str_replace(".xml","",$file);
if (!in_array($file,$filenomArray,true)) {
$file_names .=$file.",";
}
}
}
// Remove the last comma
$file_names = substr($file_names,0,strlen($file_names)-1);
closedir($handle);
} else {
$file_names = “could not open directory {$direkt}”;
}
return $file_names;
}

// Fatal error: Maximum execution time exceeds… etc etc…
echo direktorylist(‘Folder1/’);

?>[/code]

I believe line 23 – “if (!in_array($file,$filenumArray,true))” is the culprit here, so what I am wondering is if there is a more efficient way to search arrays, do I need to make another loop, or a different array, or have I just gotten something horribly wrong?

Lucas.

It appears that I was going about it the wrong way, I have solved my timeout issue by putting all of the values in to the same array, then sorting the values and weeding out the duplicates. It just took a little lateral thinking, hopefully this may help somebody in the future – other suggestions welcome.

[code]<?php

include(‘config.php’);

// List the file names, seperated by a comma
function direktorylist ($direkt) {
$files = “”;
$filenomArray = array();
// Put all of the files in the directory in to an array
if ($handle = opendir($direkt)) {
while (false !== ($file = readdir($handle))) {
if ($file != “.” && $file != “…”) {
$file = str_replace(".xml","",$file);
array_push($filenomArray,$file);
}
}
}
closedir($handle);
// Search and place all of the database entries in to this same array
$result = mysql_query(“SELECT u_xml FROM apo_filebank WHERE xml_type = ‘s’”) or die(mysql_error());
$db_array = mysql_fetch_array($result);
while($row = mysql_fetch_array($result)) {
array_push($filenomArray,$row[‘u_xml’]);
}
// Sort the array
sort($filenomArray);
// Set the first 3 instances of the array
$duplicate1 = $filenomArray[0];
$duplicate2 = $filenomArray[1];
$duplicate3 = $filenomArray[2];
$string = “”;
// Go through the array and add any unique values to the string accordingly
foreach ($filenomArray as $sorted) {
if ($duplicate2 != $duplicate1 && $duplicate2 != $duplicate3) {
$string .= $duplicate2.",";
}
$duplicate1 = $duplicate2;
$duplicate2 = $duplicate3;
$duplicate3 = $sorted;
}
// Remove the trailing comma
$string = substr($string,0,strlen($string)-1);
return $string;
}

// A wonderful comma seperated list, tada!
echo direktorylist(‘Folder1/’);

?>[/code]

Lucas.

As I can’t seem to find the edit button, I’d just like to add that the line $db_array = mysql_fetch_array($result); needn’t be there.

Sponsor our Newsletter | Privacy Policy | Terms of Service