Jump to content

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);
}
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.