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
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
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.
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
<?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:
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
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: