This is going to sound silly, but how do I create the txt file itself? I'm really new to PHP/MYSQL and I'm not sure how to create the txt file with the info in it. where would I define the $String_4_csv? Thanks.
Main Topics
Browse All TopicsI want to add a button on a web page that runs a PHP script that will first create a txt file then send it to the users computer. So the end-user clicks a button and a file starts to download. on the server side when they click the button it will create a txt file which is a text delimited file with the following fields from a MYSQL db:
"SELECT Student_ID, Guardians_First_Name, Guardians_last_Name, Home_Street Address, Home_Address_2, Home_city, Home_State, Home_Zip Code FROM students"
I am not sure how to write this file as I am new to PHP and MYSQL. I basically want the user to click the button and get the text file sent to them so they can use it with MS Word to do a mail merge. I was looking around for a mail merge script or program but everything I found so far seems to be way over my head to get done. So I think the easiest thing to do is just create the text delimited file and let the end-user import it into word for the mail merge. I hope I explaned this well enough. I would really appreciate any help you can give. I hope this is an easy script to come up with.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
the file put contents creates the txt file and puts into it the value in the $string4csv variable
the syntax is:
file_put_contents("NAME OF THE FILENAME","DATA YOU PUT IN IT")
e.g.
$data = "MY DATA IS HERE";
file_put_contents("testfil
this code creates the testfile.txt and if you open it you see MY DATA IS HERE
This is what I have in the file_create.php
==========================
<?php require_once('Connections/
<?php
if (!function_exists("GetSQLV
function GetSQLValueString($theValu
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_rea
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_
$query_Recordset1 = "SELECT Guardians_First_Name, Guardians_Last_Name, `Home_Street Address`, Home_Address_2, Home_City, Home_State, `Home_Zip Code` FROM students";
$Recordset1 = mysql_query($query_Records
$row_Recordset1 = mysql_fetch_assoc($Records
$totalRows_Recordset1 = mysql_num_rows($Recordset1
$data = "First,Last,Address1,Addre
file_put_contents("student
?>
<?php
file_put_contents("./expor
$file = student.'.txt';
print("<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=downloader.php?file=./
?>
==========================
The error I get is:
Notice: Use of undefined constant student - assumed 'student' in C:\Program Files\AVS Reverse 911\file_mail_student.php on line 43
Notice: Use of undefined constant student - assumed 'student' in C:\Program Files\AVS Reverse 911\file_mail_student.php on line 44
Lines 43 and 44 are:
file_put_contents("./expor
$file = student.'.txt';
and what gets put in the txt file is just "Testyou"
any ideas?
I get the same results... I made a chage that got rid of the error but the only thing in the txt file is "Testyou" which I have not entered anywhere.
==========================
mysql_select_db($database_
$query_Recordset1 = "SELECT Guardians_First_Name, Guardians_Last_Name, `Home_Street Address`, Home_Address_2, Home_City, Home_State, `Home_Zip Code` FROM students";
$Recordset1 = mysql_query($query_Records
$row_Recordset1 = mysql_fetch_assoc($Records
$totalRows_Recordset1 = mysql_num_rows($Recordset1
$data = "First,Last,Address1,Addre
file_put_contents("student
?>
<?php
file_put_contents("student
$file = "student.txt";
print("<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=downloader.php?file=./
==========================
Change your code with this one:
--------------------------
mysql_select_db($database_
$query_Recordset1 = "SELECT Guardians_First_Name, Guardians_Last_Name, `Home_Street Address`, Home_Address_2, Home_City, Home_State, `Home_Zip Code` FROM students";
$Recordset1 = mysql_query($query_Records
$data = "First,Last,Address1,Addre
file_put_contents("student
while ($row_Recordset1 = mysql_fetch_assoc($Records
$string2add = '
';
foreach($row as $key=>$value){$string2add = $string2add.','.$value;}
file_put_contents("student
}
print("<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=downloader.php?file=st
this is the exact code I have in the file_create.php
==========================
<?php require_once('../Connectio
<?php
if (!function_exists("GetSQLV
function GetSQLValueString($theValu
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_rea
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_
$query_Recordset1 = "SELECT Guardians_First_Name, Guardians_Last_Name, Home_Street_Address, Home_Address_2, Home_City, Home_State, Home_Zip_Code FROM students";
$Recordset1 = mysql_query($query_Records
$data = "First,Last,Address1,Addre
file_put_contents("student
while ($row_Recordset1 = mysql_fetch_assoc($Records
$string2add = '
';
foreach($row as $key=>$value){$string2add = $string2add.','.$value;}
file_put_contents("student
}
$file = "student.txt";
print("<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=downloader.php?file=./
mysql_free_result($Records
?>
==========================
in the downloader.php
==========================
<?PHP
$file = $_GET['file'];
header('Content-type: application/octet-stream')
header('Content-Length: '.filesize($file));
header('Content-Dispositio
readfile($file);
?>
==========================
it does create the file but the only thing in the file is the $data with no records below it... the error I see on the page is:
Notice: Undefined variable: row in C:\Program Files\AVS Reverse 911\exports\file_mail_stud
Warning: Invalid argument supplied for foreach() in C:\Program Files\AVS Reverse 911\exports\file_mail_stud
Notice: Undefined variable: row in C:\Program Files\AVS Reverse 911\exports\file_mail_stud
Warning: Invalid argument supplied for foreach() in C:\Program Files\AVS Reverse 911\exports\file_mail_stud
any ideas?
Business Accounts
Answer for Membership
by: gerodimPosted on 2007-05-30 at 04:42:12ID: 19178878
the first part of the code creates the file adding to id the data that the $string_4_csv variable contains ---------- ---------- ts/".$file name.".txt ",$string_ 4_csv); exports/$f ile\">"); ---------- ---------- ---------- ---------- ---------- --- ---------- ---------- ---------- ------ ; n: attachment; filename='.substr($file,10 ));
---------- ---------- ---------- ---------- ---------- ---
and gives it the name of the $filename var + ".txt"
---------------------PART 1-------------------------
file_put_contents("./expor
$file = $filename.'.txt';
print("<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=downloader.php?file=./
--------------------------
the downloader.php(Part 2) is a helper code that actually tells the browser to donwload the txt
(by default the browsers open txts they don't download them)
---------------------PART 2(downloader.php)---------
$file = $_GET['file'];
header('Content-type: application/octet-stream')
header('Content-Length: '.filesize($file));
header('Content-Dispositio
readfile($file);
--------------------------