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

  • Home
  • SEARCH
  • 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

There may be more than one way to ask this question, so here's a
It may not be best practice but are there ways of removing unsused classes
This may be a matter of style, but there's a bit of a divide
It seems to me there are other functional dependencies you may wish to declare
TextMate may be the best editor out there, but is has a big disadvantage:
There may be situations where I would need to find an object by parameters
During software development, there may be bugs in the codebase which are known issues.
I am trying to match floating-point decimal numbers with a regular expression. There may
With a JTree, assuming the root node is level 0 and there may be
Using ASP.NET MVC there are situations (such as form submission) that may require a

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.