I have text file and I want to save it in multiple file names with same content. Like "save as" and type different file names. However, do one by one is consume too much time and I need script that can save my text file with my prefered keywords as filename. Is there script that can do this job?
In PHP, put myfile.txt in a directory and upload a script with the following code in that same directory: <?php $content = file_get_contents('myfile.txt'); $filenames = array('thisfile.txt', 'thatfile.txt', 'anothername.txt'); foreach ($filenames as $var){ $fl=@fopen($var,'w+'); fwrite($fl,$content); fclose($fl); } ?> Code (markup): Depending on your server settings you might need to change the permissions/CHMOD the directory accordingly.
Thank you very much for your help but I have 10,000 of keywords to be filename. I just want script to get content from text file that prepared for filename and place them as name for content file. Like this one CONTENT.TXT --> SAVE AS --> NAME1.TXT<----- GET CONTENT FROM FILENAMES.TXT NAME2.TXT NAME3.TXT NAME4.TXT Code (markup): Is it possible to do that?
Replace the filenames array line with: $filenames = explode("\n", file_get_contents('filenames.txt')); Code (markup): This would be for a filenames.txt with the desired names being separated by a new line, if they were separated by something else, simply replace \n with the separator.
Sorry to ask you again, how can I replace space in generated filename with "-" file-name.txt instead of file name.txt cheers
No problem : <?php $content = file_get_contents('myfile.txt'); $filenames = explode("\n", file_get_contents('filenames.txt')); foreach ($filenames as $var){ $fl=@fopen(str_replace(' ','-',$var),'w+'); fwrite($fl,$content); fclose($fl); } ?> Code (markup): str_replace(' ','-',$var) is the part that replaces a space with a hyphen.
Thanks again but I have problem when using them - Error message appeared Warning: fwrite(): supplied argument is not a valid stream resource in c:\appserv\www\duplicator\duplicator-v2.php on line 9 Warning: fclose(): supplied argument is not a valid stream resource in c:\appserv\www\duplicator\duplicator-v2.php on line 10 Code (markup): Here is the total code <?php $content = file_get_contents('real-estate-stem.txt'); $filenames = explode("\n", file_get_contents('real-estate-subdomain.txt')); foreach ($filenames as $var){ $fl=@fopen(str_replace(' ','-',$var),'w+'); fwrite($fl,$content); fclose($fl); } ?> Code (markup): in file "real-estate-subdomain.txt" each keyword has been separated by enter like this colorado-real-estate-license.txt oklahoma-real-estate-license.txt Code (markup): any suggestion? Thank you very much.
<?php error_reporting( E_ALL ); $content = file_get_contents( 'real-estate-stem.txt' ); $filenames = split( "\n", file_get_contents('real-estate-subdomain.txt') ); foreach ( $filenames as $keyword ) { if( !( $handle = fopen( str_replace( ' ', '-', $keyword ), 'w+') ) ) { die( sprintf( 'Cannot open %s for writing', $keyword ) ); } elseif( !fwrite( $handle, $content ) ) { die( sprintf( 'Cannot write to %s', $keyword ) ); } elseif( !fclose( $handle ) ) { die( sprintf( 'Cannot close file handle %s', $keyword ) ); } } ?> PHP: Some more about where it goes wrong would be good
Thank you Krakjoe but some error still occured Warning: fopen(colorado-real-estate-license.txt ): failed to open stream: Invalid argument in c:\appserv\www\duplicator\duplicator-v2.php on line 10 Cannot open colorado-real-estate-license.txt for writing Code (markup): However, green rep added Any suggestion? cheers
<?php error_reporting( E_ALL ); $content = file_get_contents( 'real-estate-stem.txt' ); $filenames = split( "\n", file_get_contents('real-estate-subdomain.txt') ); foreach ( $filenames as $keyword ) { if( !( $handle = fopen( trim( str_replace( ' ', '-', $keyword ) ), 'w+') ) ) { die( sprintf( 'Cannot open %s for writing', $keyword ) ); } elseif( !fwrite( $handle, $content ) ) { die( sprintf( 'Cannot write to %s', $keyword ) ); } elseif( !fclose( $handle ) ) { die( sprintf( 'Cannot close file handle %s', $keyword ) ); } } ?> PHP:
you might need to add write permission (chmod to 777) to your directory from ftp client, file manager, or control panel.