I'm inheriting some work in which the clients files are organized so every user has his own directory tha tfiles are uploaded to. Now I have to inventory those files and insert them into a database for further use within a site.
What I'm attempting to do is loop through and grab all the users directories and set it as a variable, then nest a loop to grab the filenames within that directory and set those as individual variables associated with the user. I know this is inefficient.
Here's my database:
DROP TABLE IF EXISTS user_artwork;
CREATE TABLE user_artwork (
id int(5) NOT NULL auto_increment,
userid varchar(13) default NULL,
filename varchar(50) default NULL,
path varchar(100) default NULL,
url varchar(150) default NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
And my PHP:
/*******************/
/* cpfunctions.php */
/*******************/
<?php
function ftp_connection()
{
$fp = ftp_connect("xx.xx.xx.xx");
$login = ftp_login ($fp, "xxxxxxx", "xxxxxxxxx");
}
function directory($basedir, $AllDirectories=array()) {
ftp_connection();
#Create array for current directories contents
$ThisDir=array();
#switch to the directory we wish to scan
chdir($basedir);
$current=getcwd();
#open current directory for reading
$handle=opendir(".");
while ($file = readdir($handle)) {
#Don't add special directories '..' or '.' to the list
if (($file!='..') & ($file!='.')) {
if (is_dir($file)) {
#build an array of contents for this directory
array_push($ThisDir,$current.'/'.$file);
}
}
}
closedir($handle);
#Loop through each directory, run RecurseDir function on each one
foreach ($ThisDir as $key=>$var) {
array_push($AllDirectories, $var);
$AllDirectories=directory($var, $AllDirectories);
}
#make sure we go back to our origin
chdir($basedir);
return $AllDirectories;
}
?>
/*******************/
/* importer.php */
/*******************/
<?php
include("cpfunctions.php");
//import script
//connect by FTP
$ftp = ftp_connection();
//read directories
$dirlist = directory('/home/digikitt/public_html');
foreach ($dirlist as $key=>$val) {
$dirs = explode("/", $val);
// set user based on directory name
$user = $dirs[6];
//read files in directory
foreach(ftp_nlist($ftp,$val) as $filename)
{
echo "\$userid = $user<br />";
echo "\$filename = $filename<br />";
echo "\$path = '/home/digikitt/public_html/$val/";
echo "\$path = 'http://www.digikitten.com/playhouse/files/$user/$filename<br /><br />";
}
echo $user;
//write filename to database
}
?>
Aaron