chrisd1891 Posted October 18, 2010 Share Posted October 18, 2010 Hey all! I'm writing a simple script to scan a local directory named 'files' and all sub directories of 'files', adding each file to a database in the same table. To do this, I'm using scandir on the current working directory/files to start, and iterating over each file in a loop, but if the file is a directory, I append the directory name to the cwd/files and call scandir on that, creating a list of files in the subfolder, and then I call my addFiles() function recursively, with this as the argument. As of now, it just stops at the second call to addFiles. It adds all the top level files fine, stops there. That's where all my print_r() lines have gotten me. Here's my code... <!-- To change this template, choose Tools | Templates and open the template in the editor. --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <?php include 'DBConnect.php'; $db = new DBConnect() or die("Can't connect to DB"); $conn = $db->getConn(); mysql_select_db('download', $conn) or die("Can't select DB"); $dir = getcwd(); $dir = $dir . '\files' . "\\"; $fid = 1; $dir = addslashes($dir); $files = scandir($dir); echo $fid . " "; echo $dir; function addFiles($files) { global $dir; global $conn; global $fid; unset($files[0]); unset($files[1]); print_r($files); foreach ($files as $aFile) { if (is_file($dir.$aFile)) { echo "<li> $aFile </li>"; $query = "INSERT INTO files (file_id, file_path, file_name) values ($fid, '$dir', '$aFile')"; mysql_query($query, $conn) or die("Something wrong with query"); $fid = $fid + 1; } else if (is_dir($dir.$aFile)) { $fid = $fid + 1; $aDir = $dir . $aFile . "\\"; echo $dir; addFiles(scandir($dir.$aFile)); } } } addFiles($files); ?> </body> </html> Thanks in advance for any ideas. Also, if I'm approaching this problem completely wrong, let me know! I just came up with this and I feel like it should work... Link to comment https://forums.phpfreaks.com/topic/216195-recursive-function-not-working-correctly/ Share on other sites More sharing options...
chrisd1891 Posted October 19, 2010 Author Share Posted October 19, 2010 Arr! I just got it all working. This was baffling me for days, but naturally right after I post a topic I figure it out by myself. Here's what I came up with, if anyone is curious, <?php include 'DBConnect.php'; $db = new DBConnect() or die("Can't connect to DB"); $conn = $db->getConn(); mysql_select_db('download', $conn) or die("Can't select DB"); $dir = getcwd(); $dir = $dir . '\files' . "\\"; $fid = 1; $files = scandir($dir); echo $fid . " "; echo $dir; function addFiles($dir, $fid, $conn) { global $fid; $files = scandir($dir); print_r($files); foreach ($files as $aFile) { if (is_file($dir.$aFile)) { echo "<li> $aFile </li>"; $qDir = mysql_real_escape_string(addslashes($dir)); $myFile = mysql_real_escape_string($aFile); $fid = $fid + 1; $query = "INSERT INTO files (file_id, file_path, file_name) values ($fid, '$qDir', '$myFile')"; mysql_query($query, $conn) or die("Something wrong with query"); } else if (is_dir($dir.$aFile) && $aFile != '.' && $aFile != '..') { addFiles($dir.$aFile."\\", $fid, $conn); } } } addFiles($dir, $fid, $conn); ?> Link to comment https://forums.phpfreaks.com/topic/216195-recursive-function-not-working-correctly/#findComment-1123636 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.