Jump to content

Using fopen inside a foreach loop


pimpaulogy

Recommended Posts

There's probably a simple solution, but being I am learning PHP on the fly (thanks to google), I seem to have hit a road block.

 

I have recently create a script that will take a list of computers that I entered via a HTML form which will take each computer listed and create a batch file (titled computername-batchfile.bat).

 

To do so, I create an array based on the list of computers (which is a text file), use the explode function to look for each line break, and then use the foreach function to take each computer and create a batch for each. The problem is that they way I do it seems a bit "hoaky" - being that within the foreach function, I:

 

exec(echo some random text > computername-batchfile.bat);

exec(echo more random text >> computername-batchfile.bat);

......

 

As you see, I just execute the echo command on the server to write text to a file.

 

What I want to do is not use the echo command on the server, but use PHP functions to do all the file creation and writing.

 

I know how to create a file (fopen) and I know how to write text to that file (fwrtie), but when I put these functions inside the foreach function, the only result I get is one file created (and written to), even though I had 2 or more computer names entered in the computer list text file, and the one file that is created will have the info for the last computer in that list.

 

Here is some sample code that I am using:

 

To start, we will say that I entered the follow computer names in my form and submitted it to the php page:

Computer1

Computer2

Computer3

 

Here is the php page code:

//Writes HTML form text to text file called scan_list.txt

//$scan_list variable comes from the form (I set it earlier in the page)

$myFile = "scan_list.txt";

$fh = fopen($myFile, 'w') or die("can't open file");

$stringData = "$scan_list";

fwrite($fh, $stringData);

fclose($fh);

 

//Sets the scan_list.txt file into the array $list_comp_names

$listfile= file_get_contents("scan_list.txt");

$list_comp_names = explode("\n",$listfile);

 

//Actions to run against each computer with variable name of $list_comp_name

foreach ($list_comp_names as $list_comp_name) {

//Here is where the failure begins (I think)

//Creates file (if doesn't exist) or overwrites to make blank

$fp = fopen("$list_comp_name-test.bat", 'w');

//Just writes computer name inside newly create $list_comp_name-test.bat file

fwrite($fp, "$list_comp_name");

//Closes the file

fclose($fp);

}

 

Now, I want to write more than that, but hopefully that illustrates what I am trying to do.  My desired outcome would be that 3 files were created called:

Computer1-test.bat

Computer2-test.bat

Computer3-test.bat

 

And if you open up each file, you will see either Computer1, Computer2, or Computer3 as text (respectively).

 

Unfortunately, the results that I am getting is just one file:

Computer3-test.bat

with the text Computer3 in it.

 

Can somebody help me with what I am doing wrong or what function(s) I need to use to achieve this?  I am able to achieve this using my other method (using Windows echo function), but I'm sure there is a better way for PHP to do it all and not rely on Windows.

 

Thank you in advance (even if you just read the post and then moved on).

Paul

 

 

Link to comment
Share on other sites

Use the php manual.

file()

fie_get_contents()

file_put_contents()

foreach()

 

form.php

$post1

$post2

$post3

$write="$post1\n$post2\n$post3";

file_put_contents($file,$write);

 

do_stuff.php

$files=file("$file");

foreach($files as $f)

{

$ext='-test.bat';

$write_file="$f$ext";

file_put_contents("$write_file","$f");

}

 

or something similar

 

 

HTH

Teamatomic

Link to comment
Share on other sites

teamatomic, thank you for the reply.

 

I took your advice/code, changed a couple of names, and adapted it to my existing script for other actions based on the variables passed to it from the HTML form where I enter the computer names and such.

 

This is what I tried:

 

$scan_file_name = "scan_list.txt";

file_put_contents($scan_file_name,$scan_list);

$computer_names_list=file("$scan_file_name");

foreach($computer_names_list as $computer_name_list)

{

$ext='-name.bat';

$name_bat_file="$computer_name_list$ext";

file_put_contents("$name_bat_file","$computer_name_list");

}

 

Looking at the scan_list.txt file, the computer names are being listed there so the first part of the code is working properly:

computer1

computer2

computer3

 

Unfortunately, the result is the same as my previous code, only one file (computer3-name.bat) is created and with computer3 as the text within the file.

 

Do you know of any other functions that act like foreach, but aren't foreach?

 

It just seems really weird to me that I can use this same syntax and call echo through exec(echo $computer_name_list > $computer_name_list-name.bat); and it will create 3 files named computer1-name.bat, computer2-name.bat, and computer3-name.bat with the appropriate computer name as text inside the file.

Link to comment
Share on other sites

Call off the dogs - I figured it out, and of course is was one little bit of code that was missing.

 

It's in the explode function, I had:

$list_comp_names = explode("\n",$listfile); - in which I remember reading that it looks for the characters before the \n on each line.  Don't know why this worked one way, but not this way, but what it did was still leave a trailing space behind the computer name, which caused an error in creating the file, except for the last one is which that trailing space never exists.

 

So, I added \r to the expression:

$list_comp_names = explode("\r\n",$listfile); - and who would have thunk it but it works.

 

I do appreciate the help because with your answer, it proved to me that it should be possible.  I just needed to figure out what I was missing.

Link to comment
Share on other sites

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.