Create Multiple Files From A Text File.

Discussion in 'PHP' started by rhoula, Jan 29, 2013.

  1. #1
    I'm looking for a script that can create multiple files from a text file.

    Lets say I have a text file named files.txt, I m trying to find a php code that will read the file and create files.

    Thank you for your help
     
    Solved! View solution.
    rhoula, Jan 29, 2013 IP
  2. dixcoder

    dixcoder Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    26
    #2
    can you plz make your problem more clear ?
     
    dixcoder, Jan 30, 2013 IP
  3. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #3
    You should tell us how You want to split this file.

    But lets say You have database dump with tables structure (i/e http://pastebin.com/Z0bqqSjR)
    You want to have each CREATE TABLE query in separate file
    then You need something like this:
    <?php
    $string = 'CREATE TABLE';
    $delimiter = 'jksdfjksdfjksd89234kjlsdfchnasd89023jsd';
     
    $file_content = file_get_contents('file.txt');
     
    $file_content = str_replace($string, $delimiter.$string, $file_content);
     
    $files_array = explode($delimiter,$file_content);
     
    $i=1;
    foreach($files_array as $file) {
     
      file_put_contents($i.'_'.md5($file).'.txt', $file);
      $i++;
    }
    ?>
    Code (markup):
    Quick and dirty code :) but this was the easiest example i could think of ;)
     
    tiamak, Jan 30, 2013 IP
  4. ArMouR

    ArMouR Well-Known Member

    Messages:
    161
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #4
    I agree with tiamak... except that you don't have to md5 (unless I missed the logic there)... you could use the name of the original file. Remember the $i is already creating something like 1... 2.... 3.... etc.
     
    ArMouR, Jan 30, 2013 IP
  5. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #5
    Sorry for not being so clear.
    What I need is a script that will read a file (example: list_of_files.txt) and create files from a list.

    List_of_files.txt will have list of file names example:

    Oklahoma.php
    California.php
    Alabama.php
    Texas.php
    Georgia.php

    after the script reads the file (list_of_files.txt) it will create files in the server: Oklahoma.php, California.php, Alabama.php, Texas.php, Georgia.php and store in each file a few lines like:
    <html>
    <header>
    <title>Oklahoma</title>
    </header>
    <body>
    Coming Soon
    </body>
    </html>



    Thank you so much
     
    rhoula, Jan 30, 2013 IP
  6. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #6
    
    <?php
    $file_content = file_get_contents('list_of_files.txt');
    $files_array = explode("\n",$file_content);
    foreach($files_array as $file_name) {
      $content = '<html><header><title>'.str_replace('.php','',$file_name).'</title></header><body>Coming Soon</body></html>';
      file_put_contents($file_name, $content);
    }
    ?>
    
    PHP:
     
    tiamak, Jan 31, 2013 IP
  7. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #7
    This is what I'm getting

    Fatal error: Call to undefined function: file_put_contents() in /homepages/40/d153471067/htdocs/websitename.com/Tools/create_files.php on line 6
     
    rhoula, Jan 31, 2013 IP
  8. #8
    LOL it seems your PHP is outdated (4.*)
    add this before previous code
    
    if (!function_exists('file_put_contents')) {
        function file_put_contents($filename, $data) {
            $f = @fopen($filename, 'w');
            if (!$f) {
                return false;
            } else {
                $bytes = fwrite($f, $data);
                fclose($f);
                return $bytes;
            }
        }
    }
     
    
    PHP:
     
    tiamak, Jan 31, 2013 IP
  9. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #9
    It's working great now. Thank you so much :)
     
    rhoula, Jan 31, 2013 IP