0

I am taking data from text file( data is: daa1 daa2 daa3 on separate lines) then trying to make folders with exact name but only daa3 folders is created. Also when i use integer it creates all folders, same is the case with static string i.e "faraz".

$file = __DIR__."/dataFile.txt";
$f = fopen($file, "r");
$line =0;
 while ( $line < 5 )
{
    $a = fgets($f, 100); 

    $nl = mb_strtolower($line);
    $nl = "checkmeck/".$nl;
    $nl =  $nl."faraz"; // it works for static value i.e for faraz
//$nl =  $nl.$a; // i want this to be the name of folder
    if (!file_exists($nl)) {
    mkdir($nl, 0777, true);


}
$line++;
}

kindly help

5
  • It would be so much simpler using a database. Now, you most likely need to use a foreach loop instead of a while or in conjunction with. Commented Mar 16, 2014 at 5:04
  • What is the value of $a? File systems don't like special characters very well. Could be an issue with that. Commented Mar 16, 2014 at 5:05
  • The Q&A's here may very well help you. Commented Mar 16, 2014 at 5:09
  • the best function to foreach line by line in php is feof other functions will kill server memory if the file is large Commented Mar 16, 2014 at 5:17
  • use feof will fix your issue Commented Mar 16, 2014 at 5:22

2 Answers 2

3

use feof function its much better to get file content also line by line

Check this full code

 $file         = __DIR__."/dataFile.txt";
$linecount    = 0;
$handle       = fopen($file, "r");
$mainFolder   = "checkmeck";

while(!feof($handle))
{
  $line       = fgets($handle);
  $foldername = $mainFolder."/".trim($line);

  //$line is line name daa1,daa2,daa3 etc
  if (!file_exists($foldername)) {
    mkdir($foldername, 0777, true);
  }
  $linecount++;
  unset($line);
}

fclose($handle);

output folders

1countfaraz
2countfaraz
3countfaraz
Sign up to request clarification or add additional context in comments.

5 Comments

i have done this already what i m asking is how it could name the generated folders exactly the same as in dataFile.Txt (names is datafile are daa1, daa2 and daa3)
change $foldername = $mainFolder.$linecount."countfaraz"; to $foldername = $mainFolder.$line;
Try now this should work fine , i have updated the code
OMG YOU ARE SUCH A HERO.. WONDERFUL
glad its worked , next time use trim to remove any spaces inside the line
1

Not sure why you're having trouble with your code, but I find it to be more straightforward to use file_get_contents() instead of fopen() and fgets():

$file = __DIR__."/dataFile.txt";
$contents = file_get_contents($file);
$lines = explode("\n", $contents);
foreach ($lines as $line) {
    $nl = "checkmeck/". $line;
    if (!file_exists($nl)) {
        echo 'Creating file '. $nl . PHP_EOL;
        mkdir($nl, 0777, true);
        echo 'File '. $nl .' has been created'. PHP_EOL;
    } else {
        echo 'File '. $nl .' already exists'. PHP_EOL;
    }
}

The echo statements above are for debugging so that you can see what your code is doing. Once it is working correctly, you can remove them.

So you get the entire file contents, split it (explode()) by the newline character (\n), and then loop through the lines in the file. If what you said is true, and the file looks like:

daa1
daa2
daa3

...then it should create the following folders:

checkmeck/daa1
checkmeck/daa2
checkmeck/daa3

10 Comments

I m getting daa4 folder only not daa3 daa2 and daa1 folder. problem is still thr
its creating only daa3 folder
@FarazMukhtar: I added debug output to my sample code so you can see what the code is doing when you run it.
hhmm sir I appreciate your effort but this isnt running here getting daa4 folder only.. although receiving echo that file has been created but no such folder show up other than daa4
According to the PHP documentation, "file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance." So that's what I've always used and never run into any problems. Just saying. Not sure where you came up with the idea that it won't work on 99% of hosting servers, unless I'm just the luckiest man alive when it comes to this function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.