Jump to content

Loop around a directory and add files to database


rondog

Recommended Posts

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?

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 />";
                }
        }

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

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?

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?

cool  :D

 

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;
?>

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

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";} ?>

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!

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());
}
?>

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);

?>

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/");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.