Jump to content


Photo

Array error - only writing 1st item in array


  • Please log in to reply
5 replies to this topic

#1 aquatradehub

aquatradehub

    Advanced Member

  • Members
  • PipPipPip
  • 57 posts

Posted 22 June 2013 - 11:14 PM

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



#2 trq

trq

    Advanced Member

  • Administrators
  • 31,039 posts
  • LocationSydney, Australia.

Posted 22 June 2013 - 11:31 PM

$filenames[0] refers to the first element in the array.

#3 aquatradehub

aquatradehub

    Advanced Member

  • Members
  • PipPipPip
  • 57 posts

Posted 22 June 2013 - 11:39 PM

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



#4 trq

trq

    Advanced Member

  • Administrators
  • 31,039 posts
  • LocationSydney, Australia.

Posted 22 June 2013 - 11:48 PM

So use a loop.

See http://php.net/foreach

#5 aquatradehub

aquatradehub

    Advanced Member

  • Members
  • PipPipPip
  • 57 posts

Posted 23 June 2013 - 12:06 AM

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



#6 aquatradehub

aquatradehub

    Advanced Member

  • Members
  • PipPipPip
  • 57 posts

Posted 23 June 2013 - 12:28 AM

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users