View Full Version : Filename in a directory to DB

09-19-2011, 05:27 PM
I have a folder that I have a lot of pictures downloaded to. Is there a way to have a script go through that folder and find all of the file names and add them to a database? Any advice is greatly appreciated.

Thanks in advance.

09-19-2011, 06:23 PM
If you have a linux server, cd to your document root and execute

find . -name '*.jpg' -o -name '*.png' | sed -e 's/\.//' > images.txt

The you need something like the following

$images = file("images.txt");
// BEGIN transaction here
foreach($images as $img)
// add image in db here
// COMMIT transaction here

09-19-2011, 06:52 PM
If you don't have a ton of files I would suggest something like glob() (http://php.net/glob):

foreach (glob('/path/to/yourdir/*') as $filename)
// insert in to db
Otherwise look in to opendir() (http://php.net/opendir) and readdir() (http://php.net/readdir). Both have good examples in the manual.

09-19-2011, 07:35 PM
If all images are in one folder, use what Inigoesdr wrote. If you have many directories with subdirectories you can use what I wrote.