View Full Version : Filename in a directory to DB
09-19-2011, 06: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.
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, 07: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.
If all images are in one folder, use what Inigoesdr wrote. If you have many directories with subdirectories you can use what I wrote.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.