rondog Posted September 25, 2007 Share Posted September 25, 2007 I have a bunch of folder with flash video files. I am going to need to be searchable pretty soon, but instead of having to input each on single handedly, is their a way I can choose a folder and loop around it and have it insert into a database with the name of the flv? Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/ Share on other sites More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 The following function is what you use to read a directory, http://us3.php.net/manual/en/function.readdir.php. And here's a post that might be of interest to you, http://www.phpfreaks.com/forums/index.php/topic,160786.0.html... Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355076 Share on other sites More sharing options...
rondog Posted September 25, 2007 Author Share Posted September 25, 2007 ok I managed to loop around the directory and have it echo out each file. Where I am echoing $file, could I just do my insert statement there and it will input into all separate rows? <?php include 'connect.php'; $dir = opendir ("cam_a/tape_1"); Â Â Â Â while (false !== ($file = readdir($dir))) { Â Â Â Â Â Â Â Â if (strpos($file, '.flv',1)||strpos($file, '.jpg',1) ) { Â Â Â Â Â Â Â Â Â Â echo "$file <br />"; Â Â Â Â Â Â Â Â } Â Â Â Â } Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355116 Share on other sites More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 You can, but if you collate all the results you can insert them all at once, which will obviously lessen the load on the mysql communications. Here's the syntax: INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9); ref: http://dev.mysql.com/doc/refman/5.0/en/insert.html Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355119 Share on other sites More sharing options...
rondog Posted September 25, 2007 Author Share Posted September 25, 2007 Can you help me figure out how that statement would go? I am new to both php and mysql so im still rusty. Â Say I have a table called videos.. Â INSERT INTO videos (vidname) VALUES ($file) Â I understand that will insert each file in the videos table under the vidname field, but how would I collate all the results? Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355133 Share on other sites More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 Collate like this (for this instance! You'll need to predefine $s) //echo "$file <br />"; $s .= "(".$file."), "; Then do the sql like: $sql = "INSERT INTO videos (vidname) VALUES ".$s; Oh you might need to get rid of the last comma? Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355139 Share on other sites More sharing options...
rondog Posted September 25, 2007 Author Share Posted September 25, 2007 alright I think I am understanding this a little better now. I changed my code to this <?php include 'connect.php'; $dir = opendir ("cam_a/tape_1"); Â Â Â Â while (false !== ($file = readdir($dir))) { Â Â Â Â Â Â Â Â if (strpos($file, '.flv',1)||strpos($file, '.jpg',1) ) { Â Â Â Â Â Â Â Â Â // echo "$file <br />"; $s .= "(".$file."), "; Â Â Â Â Â Â Â Â } Â Â Â Â } $sql = "INSERT INTO videos (vidname) VALUES ".$s; echo $sql; ?> Â and its is echoing out: INSERT INTO videos (vidname) VALUES (a3-t1-001.flv), (a3-t1-002.flv), (a3-t1-003.flv), (a3-t1-004.flv), (a3-t1-005.flv), (a3-t1-006.flv), (a3-t1-007.flv), (a3-t1-008.flv), (a3-t1-009.flv), (a3-t1-010.flv), (a3-t1-011.flv), (a3-t1-012.flv), (a3-t1-013.flv), (a3-t1-014.flv), (a3-t1-015.flv), (a3-t1-016.flv), (a3-t1-017.flv), (a3-t1-018.flv), (a3-t1-019.flv), (a3-t1-020.flv), (a3-t1-021.flv), (a3-t1-022.flv), (a3-t1-023.flv), (a3-t1-024.flv), (a3-t1-025.flv), (thats about 1/5th the length it reall it, but is that the right output? It looks like what you had above. How would I strip out the last comma? Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355151 Share on other sites More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 $sret = substr($sret, 0, count($sret)-2); or something... Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355154 Share on other sites More sharing options...
rondog Posted September 25, 2007 Author Share Posted September 25, 2007 cool  It was -3, but I got the idea, so just to confirm, I would then do mysql_query($sret); right? <?php include 'connect.php'; $dir = opendir ("cam_a/tape_1"); while (false !== ($file = readdir($dir))) { if (strpos($file, '.flv',1)||strpos($file, '.jpg',1) ) { // echo "$file <br />"; $s .= "(".$file."), "; } } $sql = "INSERT INTO videos (vidname) VALUES ".$s; $sret = substr($sql, 0, count($sql)-3); echo $sret; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355163 Share on other sites More sharing options...
rondog Posted September 25, 2007 Author Share Posted September 25, 2007 I have another question I have a folder structure like the one shown below, Is their some sort of loop I can do that will look in however many directories start with "cam_" and then loop however many times inside that folder? My issue is, sometimes I'll have cam_a and cam_c with no cam_b. Or Ill have tape_1 through 4, no tape_5 and then tape_6 through 8  I have a folder structure set up like this:  cam_a  tape_1  tape_2  tape_3  tape_4 cam_b  tape_1  tape_2  tape_3  tape_4 cam_c  tape_1  tape_2  tape_3  tape_4  I want to make a for loop that I would oly have to run once to insert all my values Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355303 Share on other sites More sharing options...
darkfreaks Posted September 25, 2007 Share Posted September 25, 2007 example  <?php $array = array($_POST[variable],$_POST[variable2],$_POST[variable3]); if (in_array($_POST[variable],$_POST[variable2],$_POST[variable3],$array, true)) { insert into database;} else { echo "error";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355319 Share on other sites More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 Well... Make reply #8 into a function which you call giving it the dirname as an argument. Then as it reads through, if it checks if each entry is a dir or file, if file (do something), but if it's a dir then call the function. It may sound a bit mad but it's called a recursive function, it's how a directory walk works! Have a go an then report back! Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355321 Share on other sites More sharing options...
rondog Posted September 25, 2007 Author Share Posted September 25, 2007 Well... Make reply #8 into a function which you call giving it the dirname as an argument. Then as it reads through, if it checks if each entry is a dir or file, if file (do something), but if it's a dir then call the function. It may sound a bit mad but it's called a recursive function, it's how a directory walk works! Have a go an then report back! Â I dont really know where to begin with this. I made it into a function to accept 2 arguments, camFolder and tapeFolder. Did I even right the function correctly? I have been doing actionscript for a little while and thats how I would pass arguments if it was flash. Here is what I made up and thats about as far as I got with it :-/ <?php include 'connect.php'; //find "cam_*" folder //find "tape_*" folder //run function insertAllVids(cam_a,tape_1) //start the loop over and next calling insertAllVids(cam_a,tape_2) for example function insertAllVids($camFolder,$tapeFolder) { $camera = $camFolder; $tape = $tapeFolder; $dir = opendir ("../$camera/$tape"); while (false !== ($file = readdir($dir))) { if (strpos($file, '.flv',1)) { $s .= "('$camera','$tape','".$file."','','','','','','val1','','',''), "; } } $sql = "INSERT INTO videos (camera,tape,vidname,client,title,segtitle,location,dateshot,rights,keywords,comments,rating) VALUES ".$s; $sret = substr($sql, 0, count($sql)-3); $query = mysql_query($sret) or die(mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355331 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 func walkies( dir )  loop ( fn = readdir )    if file then echo fn    else walkies ( fn )  That's basically how I see the 'sudo code! Hope this helps you understand... Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355538 Share on other sites More sharing options...
rondog Posted September 26, 2007 Author Share Posted September 26, 2007 Geez I feel bad, your giving me this help and I dont know how to interpret that. I kinda figured I could just call a function so many times as to what folders were there so I came up with this which does it all in one swoop, but I would rather elaborate on your code example. <?php include 'connect.php'; function insertAllVids($camFolder,$tapeFolder) { $camera = $camFolder; $tape = $tapeFolder; $dir = opendir ("../$camera/$tape"); while (false !== ($file = readdir($dir))) { if (strpos($file, '.flv',1)) { $s .= "('$camera','$tape','".$file."','','','','','','','','',''), "; } } $sql = "INSERT INTO videos (camera,tape,vidname,client,title,segtitle,location,dateshot,rights,keywords,comments,rating) VALUES ".$s; $sret = substr($sql, 0, count($sql)-3); $query = mysql_query($sret) or die(mysql_error()); } //camera A insertAllVids(cam_a,tape_1); insertAllVids(cam_a,tape_2); insertAllVids(cam_a,tape_3); insertAllVids(cam_a,tape_4); insertAllVids(cam_a,tape_5); insertAllVids(cam_a,tape_6); insertAllVids(cam_a,tape_7); insertAllVids(cam_a,tape_; insertAllVids(cam_a,tape_9); //camera B insertAllVids(cam_b,tape_1); insertAllVids(cam_b,tape_2); insertAllVids(cam_b,tape_3); //camera C insertAllVids(cam_c,tape_1); insertAllVids(cam_c,tape_3); ?> Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355753 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 Right this is just a simplified version of what's referenced in 'reply#1'... I wont be testing it so it might have a few bugs, but it shouldn't... amend as appropriate... function getdir($base) { $d = array(); $dirs=opendir($base); while (($dir=readdir($dirs))!==false) { $d[] = $base.$dir; } return $d; } function walkies($base) { $dirs = getdir($base); foreach($dirs as $path) { if (is_file($path)) { // It's a file // add it, echo it or something echo $path; } else { // It's a directory, call this function again! traverseDirTree($path); } } } walkies("mydir/"); Quote Link to comment https://forums.phpfreaks.com/topic/70646-loop-around-a-directory-and-add-files-to-database/#findComment-355764 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.