aquatradehub Posted June 22, 2013 Share Posted June 22, 2013 Hi, I got this script. I need to create a large amount of multiple files in the designated directory. However the script only creates the first item in the array. Where am I going wrong? <?php // creates the variables for data, directory, and file name $your_data = "This is the data to be stored in the text file."; $dir_name = "_tropical"; // Notice, no backslash. chmod($dir_name, 0777); $dir_path = $dir_name."/"; // We introduce the backslash. $filenames = array("africanButterCatfish.php", "africanKnifeFish.php", "africanSnakehead.php"); file_put_contents($dir_path.$filenames[0],$your_data); file_put_contents($dir_path.$filenames[0],$your_data,FILE_APPEND); ?> Many Thanks Paul Quote Link to comment https://forums.phpfreaks.com/topic/279469-array-error-only-writing-1st-item-in-array/ Share on other sites More sharing options...
trq Posted June 22, 2013 Share Posted June 22, 2013 $filenames[0] refers to the first element in the array. Quote Link to comment https://forums.phpfreaks.com/topic/279469-array-error-only-writing-1st-item-in-array/#findComment-1437446 Share on other sites More sharing options...
aquatradehub Posted June 22, 2013 Author Share Posted June 22, 2013 Hi trq, thanks for your reply. Is there a quick way to write this code so it creates each file in the array? I just dont want to keep typing: file_put_contents($dir_path.$filenames[0],$your_data); file_put_contents($dir_path.$filenames[0],$your_data,FILE_APPEND); file_put_contents($dir_path.$filenames[1],$your_data); file_put_contents($dir_path.$filenames[1],$your_data,FILE_APPEND); file_put_contents($dir_path.$filenames[2],$your_data); file_put_contents($dir_path.$filenames[2],$your_data,FILE_APPEND); Thanks Paul Quote Link to comment https://forums.phpfreaks.com/topic/279469-array-error-only-writing-1st-item-in-array/#findComment-1437447 Share on other sites More sharing options...
trq Posted June 22, 2013 Share Posted June 22, 2013 So use a loop. See http://php.net/foreach Quote Link to comment https://forums.phpfreaks.com/topic/279469-array-error-only-writing-1st-item-in-array/#findComment-1437449 Share on other sites More sharing options...
aquatradehub Posted June 23, 2013 Author Share Posted June 23, 2013 Sorry, I am a bit of a novice at this. I got <?php // creates the variables for data, directory, and file name $your_data = "This is the data to be stored in the text file."; $dir_name = "_tropical"; // Notice, no backslash. chmod($dir_name, 0777); $dir_path = $dir_name."/"; // We introduce the backslash. $filenames = array("africanButterCatfish.php", "africanKnifeFish.php", "africanSnakehead.php"); foreach ($result_array as $filenames) { file_put_contents($dir_path.$filenames,$your_data); file_put_contents($dir_path.$filenames,$your_data,FILE_APPEND); } ?> But get this error Warning: Invalid argument supplied for foreach() in /home/vol15/byethost7.com/b7_12797881/htdocs/tropical1.php on line 10 To be honest, I got no idea as to what i am doing here lol. So any help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/279469-array-error-only-writing-1st-item-in-array/#findComment-1437453 Share on other sites More sharing options...
aquatradehub Posted June 23, 2013 Author Share Posted June 23, 2013 Its ok, done it <?php // creates the variables for data, directory, and file name $your_data = "This is the data to be stored in the text file."; $dir_name = "_tropical"; // Notice, no backslash. chmod($dir_name, 0777); $dir_path = $dir_name."/"; // We introduce the backslash. $filenames = array("africanButterCatfish.php", "africanKnifeFish.php", "africanSnakehead.php"); foreach( $filenames as $file){ file_put_contents($dir_path.$file,$your_data); file_put_contents($dir_path.$file,$your_data,FILE_APPEND); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279469-array-error-only-writing-1st-item-in-array/#findComment-1437454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.