Jump to content

[SOLVED] Creating files from an array using fopen and fwrite


moleculo

Recommended Posts

Hello folks, I can make a script that works fine for creating files on a single row, but I can't get it to create the content of an entire array.

 

Here's my scripts:

 

<form action=createfile.php method=get>
File name - <input type="text" name="file_name" />
/ File Extension:
<select name="file_ext">
<option value=".doc">.doc</option>
</select>
<input type="submit" name="submit" value="Submi" />
require_once('mysqladmin.php');
$query = "SELECT * FROM table WHERE itemno != ' ' ORDER BY no";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);

$i=0;
do
{
echo "
<input type=checkbox name=\"no[]\" value=\"$row[0]\"></input></td>
        <input type=text name=\"qty[]\" value=\"0\" size=\"3\"></input></td>";
        }while ($row = mysql_fetch_array($result, MYSQL_NUM));

 

Then in createfile.php, I have:

 

if(isset($_GET['submit'])) 
{ 
for ($i=0; $i<count($_GET['no']);$i++) 
{
	$qty = $_GET['qty'][$i];
        $no = $_GET['no'][$i];
	require_once('mysqladmin.php');
	$query = "SELECT * FROM table WHERE no = '$no' ORDER BY no";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result, MYSQL_NUM);
	[b]echo "$row[1],$row[2],$row[3]<br>";[/b]
}

$file_directory = "/direcotry/"; //the directory you want to store the new file in
	$file_name = strip_tags($_GET['file_name']);//the file's name, stripped of any dangerous tags
	$file_ext = strip_tags($_GET['file_ext']); //the file's extension, stripped of any dangerous tags
	$file = $file_directory.$file_name.$file_ext; //this is the entire filename
	$create_file = fopen($file, "w+"); //create the new file

	if(!$create_file)
	{
	die("There was an error creating/opening the file! Make sure you have the correct permissions!\n");
	}
	$chmod = chmod($file, 0755); //set the appropriate permissions.
	//attempt to set the permissions
	if(!$chmod)
	{echo("There was an error changing the permissions of the new file!\n"); //error changing the file's permissions
	} 
	//attempt to write basic content to the file
	if (fwrite($create_file, "

	[b]$row[1], $row[2], $row[3]<br>[/b]

	") === FALSE) {
	echo "Error writing to file: ($file)\n";
	}
	fclose($create_file);
	echo "<br>File was created successfully!\n"; //tell the user that the file has been created successfully
	exit;

 

The 2 parts that I made bold is where I'm getting a discrepancy.

 

The first one, under echo, I get the entire array of rows that were submitted.

 

The 2nd one, under if (fwrite($create_file, I only get the last row from the array. This is the important one where I need it to retrieve the entire array and write it into the file.

 

Any ideas?

 

 

Link to comment
Share on other sites

I think you want fopen( "...", "a" ); for appending.  If you always open with mode "w+", from the manual:

 

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.  

 

So you're opening the file each time and erasing the previous contents, then writing the next row and closing it.  That's why you always have the last row there.  I often have this problem, you think you're not outputting the right thing, but you are; you're just erasing it over and over.

Link to comment
Share on other sites

Ahh, that was bug #1 I think.  I overlooked the main problem:

for ($i=0; $i<count($_GET['no']);$i++) 
{
...
$query = "SELECT * FROM table WHERE no = '$no' ORDER BY no";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);
[b]echo "$row[1],$row[2],$row[3]<br>";[/b]
}

You're selecting the order rows from table and printing the first one for each variable in the get['no'] array.  But later:

fwrite($create_file, "$row[1], $row[2], $row[3]<br>") === FALSE) {
echo "Error writing to file: ($file)\n";
}

You only write the last row to the file.  Basically, whatever is inside the FOR loop gets done once for each GET['no'] item, and anything outside gets done only once.  There are two ways to fix this; either put the fwrite (and all other necessary code) inside the FOR loop, or you can build a string in the FOR loop and write it once.  I'd prefer the second.

$logString = "";
for ($i=0; $i<count($_GET['no']);$i++) 
{
$qty = $_GET['qty'][$i];
        $no = $_GET['no'][$i];
require_once('mysqladmin.php');
$query = "SELECT * FROM table WHERE no = '$no' ORDER BY no";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);
echo "$row[1],$row[2],$row[3]<br>";
$logString .= "$row[1],$row[2],$row[3]\n";
}
...
$create_file = fopen($file, "w+"); //create the new file
fwrite($create_file, $logString);

At this point it wouldn't matter if you had mode "w+" or "a" since you're only writing once, and you'd probably want "w+" otherwise the file will keep growing larger each time the page is run.

Link to comment
Share on other sites

Wow, thanks, your 2nd idea worked.

 

I had already tried both of those ideas before.

 

Including it in the for loop did not work.

 

The 2nd idea, I had tried to do it like this:

$content = $row[0].",".$row[1].",".$row[2].",".$row[3].<br>";

(That still yielded one row)

but adding the dot before the equal sign fixed it (.=)

 

Oddly, the way you wrote it like this: -did not work.

$logString .= "$row[1],$row[2],$row[3],$row[4],$row[5],$qty\n";

So for some reason, it has to be written like this:

$logString .= $row[0].",".$row[1].",".$row[2].",".$row[3].<br>";

 

Thanks again for your help. I really appreciate it.

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.