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 1102251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:09:25+00:00 2026-05-17T01:09:25+00:00

This below goes through files in a directory, reads them and saves them in

  • 0

This below goes through files in a directory, reads them and saves them in files of 500 lines max to a new directory.
This works great for me (thanks Daniel) but, I need a modification.
I would like to save to alpha num based files.

First, sort the array alpha numerically (already lowercase) would be the first step I assume.

Grab all of the lines in each $incoming.”/.txt” that start with “a” and put them into a folder at $save500.”/a” but, a max of 500 lines each.
(I guess it would be best to start with the first at the top of the sort so “0” not “a” right?)

All the lines that start with a number, go into $save500.”/num”.

None of the lines will start with anything but a-z0-9.

This will allow me to search my files for a match more efficiently using this flatfile method. Narrowing it down to one folder.

$nextfile=0;
    if (glob("" . $incoming . "/*.txt") != false){
     $nextfile = count(glob("" . $save500 . "/*.txt"));
     $nextfile++;
    }
    else{$nextfile = 1;}
    /**/
     $files = glob($incoming."/*.txt");
     $lines = array();
     foreach($files as $file){
     $lines = array_merge($lines, file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES));
    }
     $lines = array_unique($lines);
    /*this would put them all in one file*/
    /*file_put_contents($dirname."/done/allofthem.txt", implode("\n", $lines));*/
    /*this breaks them into files of 500*/
     foreach (array_chunk($lines, 500) as $chunk){
     file_put_contents($save500 . "/" . $nextfile . ".txt", implode("\n", $chunk));
     $nextfile++;
    }

Each still need to be in a max of 500 lines.

I will graduate to mysql later on. Only been doing this a couple months now.

As if that is not enough. I even thought of taking the first two characters off. Making directories with subs a/0 thru z/z!

Could be the wrong approach above since no responses.

But I want a word like aardvark saved to the 1.txt the a/a folder (appending). Unless 1.txt has 500 lines then save it to a/a 2.txt.

So xenia would be appended to the x/e folder 1.txt file unless there are 500 lines so create 2.txt and save it there.

I will then be able to search for those words more efficiently without loading a ton into memory or looping through files /lines that won’t contain a match.

Thanks everyone!

  • 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-17T01:09:26+00:00Added an answer on May 17, 2026 at 1:09 am

    I wrote some code here that should do what you’re looking for, it’s not a perfomance beauty but should do the job. Try it in a safe environment, no guarantee for any data-loss 😉

    Comment if there are any errors, it’s pretty late here 😉 I have to get some sleep 😉

    NOTE: This one only works if every line has at least 2 characters! 😉

    $nextfile=0;
    
    if (glob("" . $incoming . "/*.txt") != false){
      $nextfile = count(glob("" . $save500 . "/*.txt"));
      $nextfile++;
    }
    else
    {
      $nextfile = 1;
    }
    
    
    
    $files = glob($incoming."/*.txt");
    $lines = array();
    foreach($files as $file){
      $lines = array_merge($lines, file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES));
    }
    
    
    $lines = array_unique($lines);
    
    
    /*this would put them all in one file*/
    /*file_put_contents($dirname."/done/allofthem.txt", implode("\n", $lines));*/
    /*this breaks them into files of 500*/
    
    // sort array
    sort($lines);
    
    // outer grouping
    $groups     = groupArray($lines, 0);
    $group_keys = array_keys($groups);
    
    foreach($group_keys as $cKey) {
      // inner grouping
      $groups[$cKey] = groupArray($groups[$cKey], 1);
    
      foreach($groups[$cKey] as $innerKey => $innerArray) {
        $nextfile = 1;
        foreach(array_chunk($innerArray, 500) as $chunk) {
          file_put_contents($save500 . "/" . $cKey . "/" . $innerKey . "/" . $nextfile . ".txt", implode("\n", $chunk));    
          $nextfile++;
        }
      }
    
    }
    
    
    function groupArray($data, $offset) {
    
      $grouped = array();
    
      foreach($data as $cLine) {
        $key = substr($cLine, $offset, 1);
        if(!isset($grouped[$key])) {
          $grouped[$key] = array($cLine);
        } 
        else
        {
          $grouped[$key][] = $cLine;
        }
      }
    
      return $grouped;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large number of csv files that look like this below: xxxxxxxx
This below codes give me error below: How to generate this codes help me
In this below program i am printing the contents of the div when i
My friend done this below coding for custom control <a href=javascript:__doPostBack('id','msg');>click</a> now i want
I have class like this below shown. which contains the shopping items where the
I have this below code and it work fine header (content-type: text/xml); $xml =
How can I make something like this below work? <?PHP $_SESSION['signup_errors']['test1']; $_SESSION['signup_errors']['test2']; $_SESSION['signup_errors']['test3']; $_SESSION['signup_errors']['test4'];
I have a code like this below, which gives me a $link that equals
I'm wondering about the feasability of this below. Of course, I'm not asking for
I have a code like this below in /root_project/main.cpp : #include theoraplayer/TheoraVideoClip.h unsigned int

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.