Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 572743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:39:02+00:00 2026-05-13T13:39:02+00:00

there may be other ways to do this but I’m looking for an fairly

  • 0

there may be other ways to do this but I’m looking for an fairly easy set-up because it’s basically a one-time process.

I have 50 state directories with a handful of txt files in each one.

I want to step into each directory, “read” each file (and then do a mysql insert of each file)

I’ve tried a few variations, but each time I try to loop through and use readfile or file_get_contents it breaks after the first file and I get failed to open stream: errors on the remainder of the list.

I’ve searched for the pitfalls of using these functions in a loop expecting many reasons why not to, but not getting answers.

thanks
came back to add example code – I see there is an answer listed so I ‘ll check that out also. (none of this is mine, I simply found a function to capture the file list)


    function listFilesInDir($start_dir)
        {

        /*
        returns an array of files in $start_dir (not recursive)
        */

        $files = array();
        $dir = opendir($start_dir);
        while(($myfile = readdir($dir)) !== false)
                {
                if($myfile != '.' && $myfile != '..')
                    {
                        $files[] = $myfile;
                     }
                }
        closedir($dir);
        return $files;
        }


        $dir = 'path/to/files';
        $Docs = listFilesInDir($dir);

        foreach($Docs as $key => $fileName)
        {

        // HERE IS WHERE I TRIED THE file_get_contents
        $content = file_get_contents($fileName);

        //even doing an echo as a test would break it after the first file
        echo $content;

        //ultimately I would just do INSERT INTO here for mysql




        }

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:39:02+00:00Added an answer on May 13, 2026 at 1:39 pm

    Some variation of the below is what i would use. YMMV depending on what you’re doing. If you post your code we can address your specific implementation instead of just providing alternate solutions 🙂

    $dir = new DirectoryIterator('/path/to/states');
    foreach($dir as $file)
    {
      if(!$file->isDot() && $file->isFile() && strpos($file->getFilename(), '.txt') !== false)
      {
         $content = file_get_contents($file->getPathname());
         if($content)
         {
            // do your insert code
         }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I may get some heat for this question because there are a lot out
This may not be possible for the carousel but is there a way to
This may fall under you can't, and there's no reason to anyway, but I'm
There exist other ways of linking to JS, apart from this (the usual).. <script
I have the following String Sample however in some occurences there may be one
Is there an easy way to recursively ZIP a directory that may or may
There is any ways to listing all main directories present in php server(may it
This may be a little subjective, but I have often found that it can
I've been searching this web-site and other sources for an answer, but haven't found
This may seem an odd thing to want to do but ignoring that, is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.