Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts

    Filename in a directory to DB

    All,
    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.

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    242
    Thanks
    1
    Thanked 57 Times in 56 Posts
    If you have a linux server, cd to your document root and execute
    PHP Code:
    find . -name '*.jpg' --name '*.png' sed -'s/\.//' images.txt 
    The you need something like the following
    PHP Code:
    $images file("images.txt");
    // BEGIN transaction here
    foreach($images as $img)
    {
            
    // add image in db here
    }
    // COMMIT transaction here 

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    If you don't have a ton of files I would suggest something like glob():
    PHP Code:
    foreach (glob('/path/to/yourdir/*') as $filename)
    {
        
    // insert in to db

    Otherwise look in to opendir() and readdir(). Both have good examples in the manual.

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    242
    Thanks
    1
    Thanked 57 Times in 56 Posts
    If all images are in one folder, use what Inigoesdr wrote. If you have many directories with subdirectories you can use what I wrote.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •