Jump to content

Array error - only writing 1st item in array


aquatradehub

Recommended Posts

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

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

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 :)

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);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.