Kingy Posted February 13, 2008 Share Posted February 13, 2008 I'm completely lost on how to do this, and im pretty sure it can be. What im looking it do is read all the files in a certain directory and then add them all to a mysql database. When i say add them to a database, i just mean add the name and perhaps the link to the file? any help would be gladly appreicated Link to comment https://forums.phpfreaks.com/topic/90847-reading-directories/ Share on other sites More sharing options...
Isityou Posted February 13, 2008 Share Posted February 13, 2008 This should work, modify to your needs. <?php // Open up the directory $directoryHandle = opendir('images/test/'); // Now read through the directory and insert folder and files names into the database while (false !== ($file = readdir($handle))) { $sql = "INSERT INTO files (files_filename) VALUES ('$file')"; mysql_query($sql); } ?> Link to comment https://forums.phpfreaks.com/topic/90847-reading-directories/#findComment-465650 Share on other sites More sharing options...
sKunKbad Posted February 13, 2008 Share Posted February 13, 2008 <?php // this file shows how to list the files in a specific directory //works with php4 if ($handle = opendir('/home/content/img')) { echo "Directory handle: $handle\n"; echo "Files:\n"; while (false !== ($file = readdir($handle))) { if($file != "." && $file != "..") { echo "$file<br />\n"; } } closedir($handle); } //works with php5 echo "The name of this file, including full server path is: ".$_SERVER['SCRIPT_FILENAME']. ".<br />The following is the list of files withing the same directory<br /><br />"; $d = scandir('C:/wamp/www/script_library/'); foreach($d as $f) { if ($f != '.' && $f != '..') { echo $f . "<br />"; } } ?> This doesn't insert the files into the database, but I was just being lazy. Link to comment https://forums.phpfreaks.com/topic/90847-reading-directories/#findComment-465652 Share on other sites More sharing options...
Kingy Posted February 13, 2008 Author Share Posted February 13, 2008 thanks both of you, i modified them to suit my needs, cheers heaps Link to comment https://forums.phpfreaks.com/topic/90847-reading-directories/#findComment-465663 Share on other sites More sharing options...
Kingy Posted February 13, 2008 Author Share Posted February 13, 2008 new question, i want to do this every time something new is added to the directory, so how can i make mysql not insert the files that are already in there? Link to comment https://forums.phpfreaks.com/topic/90847-reading-directories/#findComment-465668 Share on other sites More sharing options...
Kingy Posted February 13, 2008 Author Share Posted February 13, 2008 ok i sorted that problem out, is there way to read sub directories as well?? Link to comment https://forums.phpfreaks.com/topic/90847-reading-directories/#findComment-465677 Share on other sites More sharing options...
Sulman Posted February 13, 2008 Share Posted February 13, 2008 you can determine if something is a directory or not by using is_dir(). If it is then recurse to check that directory etc. Link to comment https://forums.phpfreaks.com/topic/90847-reading-directories/#findComment-465751 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.