Well gee, to set add the links automatically, you could just set it up so that you have a web interface to upload the files too 😉 Or, I guess you could set up a cron-job. That cron would execute a script which would itterate through your downloads directory, find each file. Based on the filename, see if it's in the database. If not, add it, otherwise skip over and go to the next. Something like the following (which works by the way 😉):
<?php
function itterateDir($dir, $array=array())
{
$dh = opendir($dir);
while(false !== ($item = readdir($dh)))
{
if($item != '.' && $item != '..')
{
if(is_dir($dir.'/'.$item))
{
$array = itterateDir($dir.'/'.$item, $array);
}
else
{
$array[$dir][] = $item;
}
}
clearstatcache();
}
if(is_resource($dh))
closedir($dh);
return $array;
}
function updateItem($base, $item)
{
$path = $base.'/'.$item;
$timestamp = time();
$query = sprintf("INSERT INTO `files` (filename, filepath, added) VALUES ('%s', '%s', $timestamp)",
mysql_real_escape_string($item),
mysql_real_escape_string($path)
);
$res = @mysql_query($query);
if(!$res)
return false;
else
return true;
}
// Okay, so we itterate through our returned array
// Querying to see which files are there or not ;)
// We'll also update them as necessary.
function updateFiles($array)
{
$db = @mysql_connect('mysql.localhost', 'username', 'password');
// If we don't have a database connection... fail
if(!is_resource($db))
{
echo 'Unable to connect to Database.';
exit;
}
@mysql_select_db('database', $db);
foreach($array AS $base=>$ary)
{
foreach($ary AS $id=>$item)
{
$query = sprintf("SELECT * FROM `files` WHERE `filepath` LIKE '%%%s%%'",
mysql_real_escape_string($item)
);
$result = @mysql_query($query);
$num = mysql_num_rows($result);
if(!$result)
{
echo 'Query error [#'.mysql_errno().']: '.mysql_error().'<br>';
break;
}
else if($num == 0)
{
$rslt = updateItem($base, $item);
if($rslt)
echo 'Successfully added '.$item.' to the downloads database.<br>';
else
echo 'Unsuccessful in adding '.$item.' to the downloads database.<br>';
}
else
echo 'File already in database ('.$item.')<br>';
}
}
@mysql_close($db);
}
// Okay, so when it's called, we need to first call itterate dir
// (possibly with a specific directory) and then update the files
// as needed as per updateFiles says we need.
$root = './downloads';
$array = itterateDir($root);
if(is_array($array) && !empty($array))
{
// Okay, so we have a valid array which isn't empty
// Let's update the database.
updateFiles($array);
}
?>