I use this code below to update my database when I add new photos, but I want it to sort the pictures by filename before they are added, I know I need to use the sort function but I am not sure of where to place it, any help would be really useful.
PHP Code:
// Define the full path to the folder whose contents you want to list
$path = ".";
//File Types allowed
$pattern="\.(jpg|jpeg)$";
// Open the directory
$dir_handle = @opendir($path) or die("Error opening $path");
// Loop through the files and filter out no jpgs
while(false !== ($file = readdir($dir_handle))){
if(eregi($pattern, $file)){
$first_letter = $file{0};
$query = "SELECT filename FROM pictures WHERE filename='$file'";
$result = mysql_query($query) or die('Error, query failed one');
if (mysql_num_rows($result)==''){
//Insert data into database
$query = "SELECT * FROM pictures WHERE category='$first_letter'";
$result = mysql_query($query) or die('Error, query failed');
$count = mysql_num_rows($result);
$count = $count + 1;
//Insert data into database
$sql="INSERT INTO pictures (filename, category, seq_num, large, thumb)VALUES('$file', '$first_letter', '$count', 'http://www.calumogg.co.uk/pictures/large/$file', 'http://www.calumogg.co.uk/pictures/thumbs/$file')";
$result = mysql_query($sql) or die('Error, query failed two');
echo "<p>$file has been added to the database</p>";
}
}
Thanks in advance
Bookmarks