...

View Full Version : Writing $_POST data to excel file



madmatter23
01-27-2010, 08:11 PM
Hello,

I currently have a working script that takes data from a POST submission and adds to an an excel file (I didn't make the script).

I'd like to simply take this working script (on newsletter.php) and use it on another page (tellafriend.php) to write to another xls file. For some reason, it won't work on tellafriend.php.

Here's the original script (newsletter.php):



/*------ COMPOSE/BUILD DATA ---------*/
$data = $_POST['name'] . "\t" . $_POST['email'] . "\t" . $_POST['street'] . "\t" . $_POST['city'] . "\t" . $_POST['state'] . "\t" . $_POST['zip'] . "\t" . $_POST['identification'];

$file='/PATH/spreadsheet/newsletter.xls';
write_to_file($file,$data);


here's the script I've added to tellafriend.php:


/*------ COMPOSE/BUILD DATA ---------*/

$data = $_POST['name'] . "\t" . $_POST['youremail'] . "\t" . $_POST['friendemail'];

$file='/PATH/spreadsheet/tellafriend.xls';
write_to_file($file,$data);

echo "debug information: <br /> \$data = $data <br /> \$file = $file <br />";

I've placed both xls files in the same exact directory. I've also made sure that they both have permissions set to 777. When I run tellafriend.php, the $file and $data variables have the correct information in them.

Everything seems to be working right... the script runs, the variables are populated, no errors occur (not even in the error log). But for some reason, it doesn't write to the tellafriend.xls file!

Any ideas? Thanks you!

mlseim
01-27-2010, 08:27 PM
This looks like a function: write_to_file($file,$data);

Do you know where that function is located, and you can show it to us?

madmatter23
01-27-2010, 08:42 PM
Ah, I had assumed that write_to_file() was a basic php function, not a custom one.

I found the function. Here's the original:


function write_to_file($f='data.xls', $data='')
{
$file = $f;
$num_records=0;

if(is_readable($file))
{
$records=file($file);
$fp = fopen($file, "a+t");
if (flock($fp, 2))
{
// do an exclusive lock
foreach($records as $record)
{
$num_records++;
}

if(is_writable($file))
{
if($num_records < 1)
{
fwrite($fp, "First Name\tEmail Address\tStreet Address\tZip Code\tState\tIdentification\n");
fwrite($fp, $data . "\n");
}
else
{
fwrite($fp, $data . "\n");
}
}

flock($fp, 3); // release the lock
}
fclose($fp);
}
}


I've changed tellafriend.php to call write_to_file2() and modified the original:

function write_to_file2($f='data.xls', $data='')
{
$file = $f;
$num_records=0;

if(is_readable($file))
{
$records=file($file);
$fp = fopen($file, "a+t");
if (flock($fp, 2))
{
// do an exclusive lock
foreach($records as $record)
{
$num_records++;
}

if(is_writable($file))
{
if($num_records < 1)
{
fwrite($fp, "Name\tEmail\tFriend's Email\n");
fwrite($fp, $data . "\n");
}
else
{
fwrite($fp, $data . "\n");
}
}

flock($fp, 3); // release the lock
}
fclose($fp);
}
}

The only difference is that the table headers have changed. This makes no difference, since the headers are already set, and this condition is only called when $num_records<1, right?

I'm not sure what data.xls is... I can't seem to find it on the server anywhere.

Any thoughts? Thank you for your help!

mlseim
01-27-2010, 09:21 PM
I wonder if somewhere else, "data.xls" is being used,
and put into "newsletter.xls". I can't figure out any
other way that "newsletter.xls" is working.

When you do "tellafriend.xls", is it affecting "newsletter.xls"?

very strange indeed.

madmatter23
01-27-2010, 09:41 PM
Well at least I'm not the only one that's confused...

No, the tellafriend.php has absolutely no affect on newsletter.xls, event when it's using the exact same function!

newletter.php still works! It adds to the excel file. I can't figure it out at all. And I'm sure that data.xls does not exist on the server. Wouldn't have to exist in the same directory anyway?

It doesn't even make sense that $f=data.xls. Does that mean it's ignoring the $file variable that's being passed into it? I've tried to replace $f=data.xls with just $f so that it uses the $file variable that's given to it, but it just does nothing. Weird that it wouldn't even give an error...

mlseim
01-27-2010, 11:20 PM
I'm hoping someone else sees something that we're missing ... I just can't see it.

Just for kicks, post the complete "newsletter.php" script.


.

madmatter23
02-11-2010, 07:14 PM
Just wanted to let you know that I got it working.

I couldn't find any problems in the code, so I guessed that there might be some hidden aspect of newletter.xls that was affecting the script.

Even though the format and permissions were identical, I decided to take newsletter.xls, duplicate it, remove the content, change the headers, rename it to tellafriend.xls, and upload it to the server.

I ran the script and Voila! It worked!

I have no idea why.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum