Richard Korts
asked on
Saving File on Web Server
See attached code. This works beautifully.
I need a slight variation where I can send the file as an email attachment using php mail.
How can I do this?
Thanks
report-disp.php
I need a slight variation where I can send the file as an email attachment using php mail.
How can I do this?
Thanks
report-disp.php
ASKER
To xterm:
I many not have phrased the question properly.
At the moment, the php runs the script &Â generates a psudo Excel file which it asks in the browser where to save it. The Excel file is not actually saved on the web server. If I knew how to do that, I could probably do the rest, but thanks for your great function.
So my real question is "How do I save the generated Excel file on the server FIRST!"
I many not have phrased the question properly.
At the moment, the php runs the script &Â generates a psudo Excel file which it asks in the browser where to save it. The Excel file is not actually saved on the web server. If I knew how to do that, I could probably do the rest, but thanks for your great function.
So my real question is "How do I save the generated Excel file on the server FIRST!"
Oh, that's easy - I'll hook you up with that too, but on the way out. Â Will get back to you if nobody beats me to it.
Hi writing to a file is easy..
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);
Fwrite for each line..
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);
Fwrite for each line..
ASKER
To p_nuts,
I don't think you understand the issue............
I don't think you understand the issue............
Here is the report-disp.php script. Â It is easier to work with these things if the code is posted in the code snippet.
<? header("Content-type: application/vnd.ms-excel");
$fn = "excel" . date('Y-m-d_h_i') . ".xls";
header("Content-Disposition: attachment; filename=" . $fn);
function conv_date($x){
$dt = substr($x,6,4) . "-" . substr($x,0,2) . "-" . substr($x,3,2);
return $dt;
}
function add_days($d, $n) {
$day = substr($d,8,2);
$nd = $day + $n;
$mo = substr($d,5,2);
$dl = 31;
if ($mo == 4 || $mo == 6 || $mo == 9 || $mo == 11) {
$dl = 30;
}
if ($mo == 2) {
$dl = 28;
}
if ($nd <= $dl) {
if ($nd < 10) {
$nd = "0" . $nd;
}
$rd = substr($d,0,8) . $nd;
} else {
$nd = 1;
$mo++;
if ($mo <= 12) {
if ($mo < 10) {
$mo = "0" . $mo;
}
$rd = substr($d,0,5) . $mo . "-01";
} else {
$yr = substr($d,0,4) + 1;
$rd = $yr . "-01-01";
}
}
return $rd;
}
session_start();
if ($_SESSION['vavusr'] == "") {
header ("location: login.php");
exit;
}
$tdiff = time() - $_SESSION['alast_used'];
if ($tdiff > 7200) {
session_destroy();
header ("location: login.php");
exit;
}
$_SESSION['alast_used'] = time();
// set up database
$Host = "xyzabc.db.1and1.com";
$User = "dbo380492857";
$Password = "abcxyz";
$DBName = "db380492857";
$Link = mysql_connect ($Host, $User, $Password);
$dt = conv_date($_GET['dt']);
// get data
$qry = "SELECT * from resv where pickup like '" . $dt . "%' order by pickup";
//echo $qry . "<br>";
$res = mysql_db_query ($DBName, $qry, $Link);
$nr = mysql_num_rows($res);
// get max length of notes
$ln = 0;
for ($i = 0; $i < $nr; $i++) {
$r = mysql_fetch_array($res);
$ln = max ($ln, strlen($r['vav_notes']));
}
$wd = 15 * $ln;
$res = mysql_db_query ($DBName, $qry, $Link);?>
<html>
<head>
<title>Village Airport Van - Reservations Report</title>
<style type="text/css">
.pt18 {font-size: 18pt; font-family: Arial}
.pt14 {font-size: 14pt; font-family: Arial}
.pt12 {font-size: 12pt; font-family: Arial}
.pt11b {font-size: 11pt; font-family: Arial; font-weight:bold;}
.pt11br {font-size: 11pt; font-family: Arial; font-weight:bold; color:#FF0000;}
.pt10 {font-size: 10pt; font-family: Arial}
.pt8 {font-size: 8pt; font-family: Arial}
.pt9 {font-size: 9pt; font-family: Arial}
.pt9b {font-size: 9pt; font-family: Arial; font-weight:bold;}
.pt7 {font-size: 7pt; font-family: Arial}
td.textstr {mso-number-format:\@}
</style>
</head>
<body>
<table class="pt11b" border="1">
<tr class="pt9b">
<td align="center" bgcolor="#FFC000">601</td>
<td align="center" bgcolor="#92D050">801</td>
<td align="center" bgcolor="#FFFF00">802</td>
<td align="center" bgcolor="#A5A5A5">803</td>
<td align="center" bgcolor="#93CDDD">804</td>
<td align="center" bgcolor="#008080">901</td>
<td align="center" bgcolor="#9966FF">902 7 Pass</td>
<td align="center" bgcolor="#FF99CC">903</td>
<td align="center" bgcolor="#FF8080">904</td>
<td align="center" bgcolor="#00FFFF">905</td>
<td align="center" bgcolor="#00B0F0">906</td>
<td align="center" bgcolor="#E26B0A">907</td>
<td align="center" bgcolor="#7030A0">908</td>
<td align="center" bgcolor="#00FF00">1001</td>
<td colspan="6"> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFC000"> </td>
<td align="center" bgcolor="#92D050"> </td>
<td align="center" bgcolor="#FFFF00"> </td>
<td align="center" bgcolor="#A5A5A5"> </td>
<td align="center" bgcolor="#93CDDD"> </td>
<td align="center" bgcolor="#008080"> </td>
<td align="center" bgcolor="#9966FF"> </td>
<td align="center" bgcolor="#FF99CC"> </td>
<td align="center" bgcolor="#FF8080"> </td>
<td align="center" bgcolor="#00FFFF"> </td>
<td align="center" bgcolor="#00B0F0"> </td>
<td align="center" bgcolor="#E26B0A"> </td>
<td align="center" bgcolor="#7030A0"> </td>
<td align="center" bgcolor="#00FF00"> </td>
<td colspan="6"> </td>
</tr>
<tr style="background-color: #C5E1C5;">
<td>Date</td>
<td>Day</td>
<td>Phone</td>
<td>PUP/SAT</td>
<td>Depart</td>
<td>First Name</td>
<td>Last Name</td>
<td>House #</td>
<td>Street Name</td>
<td>Village</td>
<td>Num Pass</td>
<td>$$$</td>
<td>Driver</td>
<td>Conf. #</td>
<td>Gate</td>
<td>Airline</td>
<td>Flight</td>
<td>Depart City</td>
<td>Notes</td>
</tr>
<? for ($i = 0; $i < $nr; $i++) {
$r = mysql_fetch_array($res);
$style= "color: black;";
if ($r['ad'] == "A") {
$style = "color: red;";
}
if ($r['late'] == "Y") {
$style = "background-color: #808080; color: #FFFF00;";
}
$pu = $r['pickup'];
$puhr = substr($pu,11,2);
$hrmn = $puhr . substr($pu,14,2);
$hrmn = (string)$hrmn;
$fthr = substr($r['ftime'],11,2);
$ftime = $fthr . substr($r['ftime'],14,2);
$ts = mktime(substr($pu,11,2), substr($pu,14,2),0,substr($pu,5,2), substr($pu,8,2), substr($pu,0,4));
$dt = substr($pu,5,2) . "/" . substr($pu,8,2);
$wkdy = date('D', $ts);
$ph = $r['phone'];
if ($ph == "") {
$ph = $r['cell'];
}
$flight = "";
if ($r['flight'] != "") {
$flight = "#" . $r['flight'];
}
?>
<tr style="<? print $style; ?>">
<td><? print $dt; ?></td>
<td><? print $wkdy; ?></td>
<td><? print $ph; ?></td>
<? if ($r['ad'] == "D") { ?>
<td class="textstr" align="left"><?php print date("Hi", strtotime($hrmn)); ?> </td>
<? } else { ?>
<td class="textstr" align="left"><? print $ftime; ?></td>
<? } ?>
<? if ($r['ad'] == "D") { ?>
<td class="textstr" align="left"><? print $ftime; ?></td>
<? } else { ?>
<td> </td>
<? } ?>
<td><? print $r['fname']; ?></td>
<td><? print $r['lname']; ?></td>
<td><? print $r['number']; ?></td>
<td><? print $r['street']; ?></td>
<td><? print $r['comm']; ?></td>
<td><? print $r['npass']; ?></td>
<td>$<? print $r['feeamt']; ?></td>
<td><? print $r['driver']; ?></td>
<td><? print $r['confno']; ?></td>
<td><? print $r['gate']; ?></td>
<td><? print $r['airline']; ?></td>
<td><? print $flight; ?></td>
<td><? print $r['depart_city']; ?></td>
<td><? print $r['spec_consid']; ?></td>
</tr>
<? } ?>
</table>
</body>
</html>
I cannot test this because I do not have your data model, and I do not know whether it will work or not -- all that intermixed PHP and HTML makes it impossible to follow exactly what is going on. Â You might want to make a Google search for "MVC Design Patterns" or "Separation of Code and Presentation" and see what you can learn from the popular literature.
The strategy here is to capture the output buffer (the string that is written to the browser). Â We start that on line 3. Â Then after the HTML is closed (line 210) we go back into PHP, get the buffer contents, and write it to a file on the server.
I may try a simple experiment to see if this works. Â I'll post the demonstration script and the results after I have tested it.
The strategy here is to capture the output buffer (the string that is written to the browser). Â We start that on line 3. Â Then after the HTML is closed (line 210) we go back into PHP, get the buffer contents, and write it to a file on the server.
I may try a simple experiment to see if this works. Â I'll post the demonstration script and the results after I have tested it.
<?php
// START THE OUTPUT BUFFERS
ob_start();
header("Content-type: application/vnd.ms-excel");
$fn = "excel" . date('Y-m-d_h_i') . ".xls";
header("Content-Disposition: attachment; filename=" . $fn);
function conv_date($x){
$dt = substr($x,6,4) . "-" . substr($x,0,2) . "-" . substr($x,3,2);
return $dt;
}
function add_days($d, $n) {
$day = substr($d,8,2);
$nd = $day + $n;
$mo = substr($d,5,2);
$dl = 31;
if ($mo == 4 || $mo == 6 || $mo == 9 || $mo == 11) {
$dl = 30;
}
if ($mo == 2) {
$dl = 28;
}
if ($nd <= $dl) {
if ($nd < 10) {
$nd = "0" . $nd;
}
$rd = substr($d,0,8) . $nd;
} else {
$nd = 1;
$mo++;
if ($mo <= 12) {
if ($mo < 10) {
$mo = "0" . $mo;
}
$rd = substr($d,0,5) . $mo . "-01";
} else {
$yr = substr($d,0,4) + 1;
$rd = $yr . "-01-01";
}
}
return $rd;
}
session_start();
if ($_SESSION['vavusr'] == "") {
header ("location: login.php");
exit;
}
$tdiff = time() - $_SESSION['alast_used'];
if ($tdiff > 7200) {
session_destroy();
header ("location: login.php");
exit;
}
$_SESSION['alast_used'] = time();
// set up database
$Host = "xyzabc.db.1and1.com";
$User = "dbo380492857";
$Password = "abcxyz";
$DBName = "db380492857";
$Link = mysql_connect ($Host, $User, $Password);
$dt = conv_date($_GET['dt']);
// get data
$qry = "SELECT * from resv where pickup like '" . $dt . "%' order by pickup";
//echo $qry . "<br>";
$res = mysql_db_query ($DBName, $qry, $Link);
$nr = mysql_num_rows($res);
// get max length of notes
$ln = 0;
for ($i = 0; $i < $nr; $i++) {
$r = mysql_fetch_array($res);
$ln = max ($ln, strlen($r['vav_notes']));
}
$wd = 15 * $ln;
$res = mysql_db_query ($DBName, $qry, $Link);?>
<html>
<head>
<title>Village Airport Van - Reservations Report</title>
<style type="text/css">
.pt18 {font-size: 18pt; font-family: Arial}
.pt14 {font-size: 14pt; font-family: Arial}
.pt12 {font-size: 12pt; font-family: Arial}
.pt11b {font-size: 11pt; font-family: Arial; font-weight:bold;}
.pt11br {font-size: 11pt; font-family: Arial; font-weight:bold; color:#FF0000;}
.pt10 {font-size: 10pt; font-family: Arial}
.pt8 {font-size: 8pt; font-family: Arial}
.pt9 {font-size: 9pt; font-family: Arial}
.pt9b {font-size: 9pt; font-family: Arial; font-weight:bold;}
.pt7 {font-size: 7pt; font-family: Arial}
td.textstr {mso-number-format:\@}
</style>
</head>
<body>
<table class="pt11b" border="1">
<tr class="pt9b">
<td align="center" bgcolor="#FFC000">601</td>
<td align="center" bgcolor="#92D050">801</td>
<td align="center" bgcolor="#FFFF00">802</td>
<td align="center" bgcolor="#A5A5A5">803</td>
<td align="center" bgcolor="#93CDDD">804</td>
<td align="center" bgcolor="#008080">901</td>
<td align="center" bgcolor="#9966FF">902 7 Pass</td>
<td align="center" bgcolor="#FF99CC">903</td>
<td align="center" bgcolor="#FF8080">904</td>
<td align="center" bgcolor="#00FFFF">905</td>
<td align="center" bgcolor="#00B0F0">906</td>
<td align="center" bgcolor="#E26B0A">907</td>
<td align="center" bgcolor="#7030A0">908</td>
<td align="center" bgcolor="#00FF00">1001</td>
<td colspan="6"> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFC000"> </td>
<td align="center" bgcolor="#92D050"> </td>
<td align="center" bgcolor="#FFFF00"> </td>
<td align="center" bgcolor="#A5A5A5"> </td>
<td align="center" bgcolor="#93CDDD"> </td>
<td align="center" bgcolor="#008080"> </td>
<td align="center" bgcolor="#9966FF"> </td>
<td align="center" bgcolor="#FF99CC"> </td>
<td align="center" bgcolor="#FF8080"> </td>
<td align="center" bgcolor="#00FFFF"> </td>
<td align="center" bgcolor="#00B0F0"> </td>
<td align="center" bgcolor="#E26B0A"> </td>
<td align="center" bgcolor="#7030A0"> </td>
<td align="center" bgcolor="#00FF00"> </td>
<td colspan="6"> </td>
</tr>
<tr style="background-color: #C5E1C5;">
<td>Date</td>
<td>Day</td>
<td>Phone</td>
<td>PUP/SAT</td>
<td>Depart</td>
<td>First Name</td>
<td>Last Name</td>
<td>House #</td>
<td>Street Name</td>
<td>Village</td>
<td>Num Pass</td>
<td>$$$</td>
<td>Driver</td>
<td>Conf. #</td>
<td>Gate</td>
<td>Airline</td>
<td>Flight</td>
<td>Depart City</td>
<td>Notes</td>
</tr>
<? for ($i = 0; $i < $nr; $i++) {
$r = mysql_fetch_array($res);
$style= "color: black;";
if ($r['ad'] == "A") {
$style = "color: red;";
}
if ($r['late'] == "Y") {
$style = "background-color: #808080; color: #FFFF00;";
}
$pu = $r['pickup'];
$puhr = substr($pu,11,2);
$hrmn = $puhr . substr($pu,14,2);
$hrmn = (string)$hrmn;
$fthr = substr($r['ftime'],11,2);
$ftime = $fthr . substr($r['ftime'],14,2);
$ts = mktime(substr($pu,11,2), substr($pu,14,2),0,substr($pu,5,2), substr($pu,8,2), substr($pu,0,4));
$dt = substr($pu,5,2) . "/" . substr($pu,8,2);
$wkdy = date('D', $ts);
$ph = $r['phone'];
if ($ph == "") {
$ph = $r['cell'];
}
$flight = "";
if ($r['flight'] != "") {
$flight = "#" . $r['flight'];
}
?>
<tr style="<? print $style; ?>">
<td><? print $dt; ?></td>
<td><? print $wkdy; ?></td>
<td><? print $ph; ?></td>
<? if ($r['ad'] == "D") { ?>
<td class="textstr" align="left"><?php print date("Hi", strtotime($hrmn)); ?> </td>
<? } else { ?>
<td class="textstr" align="left"><? print $ftime; ?></td>
<? } ?>
<? if ($r['ad'] == "D") { ?>
<td class="textstr" align="left"><? print $ftime; ?></td>
<? } else { ?>
<td> </td>
<? } ?>
<td><? print $r['fname']; ?></td>
<td><? print $r['lname']; ?></td>
<td><? print $r['number']; ?></td>
<td><? print $r['street']; ?></td>
<td><? print $r['comm']; ?></td>
<td><? print $r['npass']; ?></td>
<td>$<? print $r['feeamt']; ?></td>
<td><? print $r['driver']; ?></td>
<td><? print $r['confno']; ?></td>
<td><? print $r['gate']; ?></td>
<td><? print $r['airline']; ?></td>
<td><? print $flight; ?></td>
<td><? print $r['depart_city']; ?></td>
<td><? print $r['spec_consid']; ?></td>
</tr>
<? } ?>
</table>
</body>
</html>
<?php
// CAPTURE THE OUTPUT BUFFERS
$buf = ob_get_contents();
// WRITE THE FILE TO THE SERVER
file_put_contents('my.xls', $buf);
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
To Ray_Paseur:
That looks perfect, I will try it today.
I knew there was a way to capture the output buffer, I just couldn't remember what that was called.
If it works you definitely get the points.
That looks perfect, I will try it today.
I knew there was a way to capture the output buffer, I just couldn't remember what that was called.
If it works you definitely get the points.
ASKER
To Ray_Paseur:
It doesn't work. It asks if you want to save the file or open it. I tried it both in FireFox &Â IE (knowing that should make no difference, it's running on the server).
I did it with my own variation with real data &Â it failed like that. Then I copied your code EXACTLY &Â it did the same thing.
I thought it was because of the line:
header("Content-Dispositio n: attachment; filename=" . $fn);
So I took that out. It still wants the browser to save the file.
So I'm guessing that the header
header("Content-type: application/vnd.ms-excel") ;
disallows it from being saved on the server.
Any thoughts?
It doesn't work. It asks if you want to save the file or open it. I tried it both in FireFox &Â IE (knowing that should make no difference, it's running on the server).
I did it with my own variation with real data &Â it failed like that. Then I copied your code EXACTLY &Â it did the same thing.
I thought it was because of the line:
header("Content-Dispositio
So I took that out. It still wants the browser to save the file.
So I'm guessing that the header
header("Content-type: application/vnd.ms-excel")
disallows it from being saved on the server.
Any thoughts?
ASKER
To Ray_Paseur:
One more thing. When I ran your file on my server, I called the php file ray_paseur.php
The browser save box looks like the attached. That seems odd.
ray-paseur.jpg
One more thing. When I ran your file on my server, I called the php file ray_paseur.php
The browser save box looks like the attached. That seems odd.
ray-paseur.jpg
ASKER
To Ray_Paseur:
Sorry, the previous post had the wrong image. See attached. The prior one was my file.
ray-paseur.jpg
Sorry, the previous post had the wrong image. See attached. The prior one was my file.
ray-paseur.jpg
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
@p_nuts:
Actually, the author wants to send the email first, and then push the file to the browser, so yes, the headers are needed.
Actually, the author wants to send the email first, and then push the file to the browser, so yes, the headers are needed.
Did I misread this
where I can send the file as an email attachment using php mail.
where I can send the file as an email attachment using php mail.
No, you didn't misread it, but perhaps you didn't see this update "So my real question is "How do I save the generated Excel file on the server FIRST!""
In that case I stand with writing it to a file.
And not using the output caching.. When offering it through the browser you can offer a direct link or stream it in which case you would need the headers ..
And not using the output caching.. When offering it through the browser you can offer a direct link or stream it in which case you would need the headers ..
ASKER
I'm getting nowhere with this.
I know I can't just close the question, I get all kinds of issues from the "hall monitor"
So what do I do?
I know I can't just close the question, I get all kinds of issues from the "hall monitor"
So what do I do?
Please see post id #37090612 above - is the code writing the file to disk, or is it not?
ASKER
To xterm
Sorry to be such an idiot, but I have no clue what post id #37090612 above  is.
Sorry to be such an idiot, but I have no clue what post id #37090612 above  is.
It's the ID number in the top of each message in this thread, but here, let me post it again. Â This is what I wrote last, and you hadn't responded to it:
Let's take this one thing at a time - Ray gave you these lines (sorry, I was busy this weekend and didn't get to this part):
// CAPTURE THE OUTPUT BUFFERS
$buf = ob_get_contents();
// WRITE THE FILE TO THE SERVER
file_put_contents('my.xls' , $buf);
My question to you - does this, or does this not write the file "my.xls" to the server? Â The file will obviously have to be present on the server in order for you to be able to use my attachment code to mail it out, which it seemed like is the point of your question.
My thought is this - the code above in its basic state will not work on a Unix server as pretty much none by default will allow the apache user to write anything in the web root. Â So I think "my.xls" above should be changed to "/tmp/my.xls", and then you should have the file to work with as you please. Â Whatever happens when you echo the output stream to the browser shouldn't change, as that is the code you supplied in your original post and neither Ray nor myself has modified that.
Let's take this one thing at a time - Ray gave you these lines (sorry, I was busy this weekend and didn't get to this part):
// CAPTURE THE OUTPUT BUFFERS
$buf = ob_get_contents();
// WRITE THE FILE TO THE SERVER
file_put_contents('my.xls'
My question to you - does this, or does this not write the file "my.xls" to the server? Â The file will obviously have to be present on the server in order for you to be able to use my attachment code to mail it out, which it seemed like is the point of your question.
My thought is this - the code above in its basic state will not work on a Unix server as pretty much none by default will allow the apache user to write anything in the web root. Â So I think "my.xls" above should be changed to "/tmp/my.xls", and then you should have the file to work with as you please. Â Whatever happens when you echo the output stream to the browser shouldn't change, as that is the code you supplied in your original post and neither Ray nor myself has modified that.
ok so you want to email and offer for download..
logic tells us you should email first.
let's see ..
Try this make sure you set a location to save in the fn.
http://php.net/manual/en/function.file-put-contents.php for more info
logic tells us you should email first.
let's see ..
Try this make sure you set a location to save in the fn.
http://php.net/manual/en/function.file-put-contents.php for more info
<?
session_start();
if ($_SESSION['vavusr'] == "") {
header ("location: login.php");
exit;
}
$fn = "excel" . date('Y-m-d_h_i') . ".xls";
$tdiff = time() - $_SESSION['alast_used'];
if ($tdiff > 7200) {
session_destroy();
header ("location: login.php");
exit;
}
$_SESSION['alast_used'] = time();
// set up database
$Host = "xyzabc.db.1and1.com";
$User = "dbo380492857";
$Password = "abcxyz";
$DBName = "db380492857";
$Link = mysql_connect ($Host, $User, $Password);
$dt = conv_date($_GET['dt']);
// get data
$qry = "SELECT * from resv where pickup like '" . $dt . "%' order by pickup";
//echo $qry . "<br>";
$res = mysql_db_query ($DBName, $qry, $Link);
$nr = mysql_num_rows($res);
// get max length of notes
$ln = 0;
for ($i = 0; $i < $nr; $i++) {
$r = mysql_fetch_array($res);
$ln = max ($ln, strlen($r['vav_notes']));
}
$wd = 15 * $ln;
$res = mysql_db_query ($DBName, $qry, $Link);
$output = '
<html>
<head>
<title>Village Airport Van - Reservations Report</title>
<style type="text/css">
.pt18 {font-size: 18pt; font-family: Arial}
.pt14 {font-size: 14pt; font-family: Arial}
.pt12 {font-size: 12pt; font-family: Arial}
.pt11b {font-size: 11pt; font-family: Arial; font-weight:bold;}
.pt11br {font-size: 11pt; font-family: Arial; font-weight:bold; color:#FF0000;}
.pt10 {font-size: 10pt; font-family: Arial}
.pt8 {font-size: 8pt; font-family: Arial}
.pt9 {font-size: 9pt; font-family: Arial}
.pt9b {font-size: 9pt; font-family: Arial; font-weight:bold;}
.pt7 {font-size: 7pt; font-family: Arial}
td.textstr {mso-number-format:\@}
</style>
</head>
<body>
<table class="pt11b" border="1">
<tr class="pt9b">
<td align="center" bgcolor="#FFC000">601</td>
<td align="center" bgcolor="#92D050">801</td>
<td align="center" bgcolor="#FFFF00">802</td>
<td align="center" bgcolor="#A5A5A5">803</td>
<td align="center" bgcolor="#93CDDD">804</td>
<td align="center" bgcolor="#008080">901</td>
<td align="center" bgcolor="#9966FF">902 7 Pass</td>
<td align="center" bgcolor="#FF99CC">903</td>
<td align="center" bgcolor="#FF8080">904</td>
<td align="center" bgcolor="#00FFFF">905</td>
<td align="center" bgcolor="#00B0F0">906</td>
<td align="center" bgcolor="#E26B0A">907</td>
<td align="center" bgcolor="#7030A0">908</td>
<td align="center" bgcolor="#00FF00">1001</td>
<td colspan="6"> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFC000"> </td>
<td align="center" bgcolor="#92D050"> </td>
<td align="center" bgcolor="#FFFF00"> </td>
<td align="center" bgcolor="#A5A5A5"> </td>
<td align="center" bgcolor="#93CDDD"> </td>
<td align="center" bgcolor="#008080"> </td>
<td align="center" bgcolor="#9966FF"> </td>
<td align="center" bgcolor="#FF99CC"> </td>
<td align="center" bgcolor="#FF8080"> </td>
<td align="center" bgcolor="#00FFFF"> </td>
<td align="center" bgcolor="#00B0F0"> </td>
<td align="center" bgcolor="#E26B0A"> </td>
<td align="center" bgcolor="#7030A0"> </td>
<td align="center" bgcolor="#00FF00"> </td>
<td colspan="6"> </td>
</tr>
<tr style="background-color: #C5E1C5;">
<td>Date</td>
<td>Day</td>
<td>Phone</td>
<td>PUP/SAT</td>
<td>Depart</td>
<td>First Name</td>
<td>Last Name</td>
<td>House #</td>
<td>Street Name</td>
<td>Village</td>
<td>Num Pass</td>
<td>$$$</td>
<td>Driver</td>
<td>Conf. #</td>
<td>Gate</td>
<td>Airline</td>
<td>Flight</td>
<td>Depart City</td>
<td>Notes</td>
</tr>
';
for ($i = 0; $i < $nr; $i++) {
$r = mysql_fetch_array($res);
$style= "color: black;";
if ($r['ad'] == "A") {
$style = "color: red;";
}
if ($r['late'] == "Y") {
$style = "background-color: #808080; color: #FFFF00;";
}
$pu = $r['pickup'];
$puhr = substr($pu,11,2);
$hrmn = $puhr . substr($pu,14,2);
$hrmn = (string)$hrmn;
$fthr = substr($r['ftime'],11,2);
$ftime = $fthr . substr($r['ftime'],14,2);
$ts = mktime(substr($pu,11,2), substr($pu,14,2),0,substr($pu,5,2), substr($pu,8,2), substr($pu,0,4));
$dt = substr($pu,5,2) . "/" . substr($pu,8,2);
$wkdy = date('D', $ts);
$ph = $r['phone'];
if ($ph == "") {
$ph = $r['cell'];
}
$flight = "";
if ($r['flight'] != "") {
$flight = "#" . $r['flight'];
}
$output .= '
<tr style="'. $style.'">
<td>'. $dt.'</td>
<td>'. $wkdy.'</td>
<td>'. $ph.'</td>
';
if ($r['ad'] == "D")
$output .= '<td class="textstr" align="left">'. date("Hi", strtotime($hrmn)).' </td>';
else
$output .= ' <td class="textstr" align="left">'. $ftime.'</td>';
if ($r['ad'] == "D")
$output .= '<td class="textstr" align="left">'. $ftime.'</td>';
else
$output .= ' <td> </td> ';
$output .= '
<td>'. $r['fname'].'</td>
<td>'. $r['lname'].'</td>
<td>'. $r['number'].'</td>
<td>'. $r['street'].'</td>
<td>'. $r['comm'].'</td>
<td>'. $r['npass'].'</td>
<td>$'. $r['feeamt'].'</td>
<td>'. $r['driver'].'</td>
<td>'. $r['confno'].'</td>
<td>'. $r['gate'].'</td>
<td>'. $r['airline'].'</td>
<td>'. $flight.'</td>
<td>'. $r['depart_city'].'</td>
<td>'. $r['spec_consid'].'</td>
</tr>
';
}
$output .= '
</table>
</body>
</html>
';
// still no headers send ..
// save the file for emailing.
// we need to provide the location add it to $fn is easiest
// $fn = '/www/username/tmp/'.$fn;
// or something like that.
file_put_contents($fn, $output);
// send the email
// send mail using phpmailer
$mailer = new phpmailer();
$m->AddAddress($to_emailaddress);
$m->SetFrom($youremail,$yourname);
$m->AddAttachment($fn);
$m->IsHTML(true);
$m->Body = $msg;
$m->Subject = $subject;
$m->create_body();
$m->Send();
$m->ClearAllRecipients();
// push the download
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=" . $fn);
echo $output;
// all done.
// let's put the functions somewhere out of the way.
function conv_date($x){
$dt = substr($x,6,4) . "-" . substr($x,0,2) . "-" . substr($x,3,2);
return $dt;
}
function add_days($d, $n) {
$day = substr($d,8,2);
$nd = $day + $n;
$mo = substr($d,5,2);
$dl = 31;
if ($mo == 4 || $mo == 6 || $mo == 9 || $mo == 11) {
$dl = 30;
}
if ($mo == 2) {
$dl = 28;
}
if ($nd <= $dl) {
if ($nd < 10) {
$nd = "0" . $nd;
}
$rd = substr($d,0,8) . $nd;
} else {
$nd = 1;
$mo++;
if ($mo <= 12) {
if ($mo < 10) {
$mo = "0" . $mo;
}
$rd = substr($d,0,5) . $mo . "-01";
} else {
$yr = substr($d,0,4) + 1;
$rd = $yr . "-01-01";
}
}
return $rd;
}
?>
oh yeah.. i used a phpmailer class.
You can download it here
http://phpmailer.worxware.com/
just include the file and set the email address variables and it should work.
You can download it here
http://phpmailer.worxware.com/
just include the file and set the email address variables and it should work.
sorry line 209 should not be
$mailer  = new phpmailer();
but should be
$m = new phpmailer();
$mailer  = new phpmailer();
but should be
$m = new phpmailer();
ASKER
To xterm,
It DOES NOT write the file to the server.
As I said previously, the browser presents a message asking "save or open".
It DOES NOT write the file to the server.
As I said previously, the browser presents a message asking "save or open".
@rkorts:
Yes, it is still supposed to present the save or open dialog because that is how your page operated originally. Â You asked for that functionality not to change, and it will not.
I'm asking that you please change this below:
// WRITE THE FILE TO THE SERVER
file_put_contents('my.xls' , $buf);
to:
// WRITE THE FILE TO THE SERVER
file_put_contents('/tmp/my .xls', $buf);
I think the only reason it's not writing the file is that it has no permissions to write in the DocumentRoot of the web server.
I believe if you make that change, the file will exist, and then you can use mine or anybody else's mail() function to send the file, prior to presenting it to the user's browser.
Yes, it is still supposed to present the save or open dialog because that is how your page operated originally. Â You asked for that functionality not to change, and it will not.
I'm asking that you please change this below:
// WRITE THE FILE TO THE SERVER
file_put_contents('my.xls'
to:
// WRITE THE FILE TO THE SERVER
file_put_contents('/tmp/my
I think the only reason it's not writing the file is that it has no permissions to write in the DocumentRoot of the web server.
I believe if you make that change, the file will exist, and then you can use mine or anybody else's mail() function to send the file, prior to presenting it to the user's browser.
ASKER
To all,
Clearly I did not word the original question properly.
My intent is to create an excel spreadsheet in php, save it on the web server &Â email it to some recipients
I was stupid in the way I worded it, using an existing process that works perfectly for it's intended purpose.
So I will award points &Â start over with a new question, trying to word it more appropriately.
Clearly I did not word the original question properly.
My intent is to create an excel spreadsheet in php, save it on the web server &Â email it to some recipients
I was stupid in the way I worded it, using an existing process that works perfectly for it's intended purpose.
So I will award points &Â start over with a new question, trying to word it more appropriately.
ASKER
See prior comments
You don't have to do that. Look at my code and just take out the bit above your functions it does that.
ASKER
p_nuts:
OK, I'll look at that later &Â let you know.
rkorts
OK, I'll look at that later &Â let you know.
rkorts
Did you fix it?
ASKER
p_nuts:
I copied your code &Â eliminated the part about the emailing (just to see if it saves the file). See attached. (of course I changed the REAL database access parameters).
I get this:
Fatal error: Call to undefined function: file_put_contents() in /homepages/38/d170218105/h tdocs/VAV/ save_file_ test.php on line 198
Odd, because I looked it up on php.net &Â that's an OK function
save-file-test.php
I copied your code &Â eliminated the part about the emailing (just to see if it saves the file). See attached. (of course I changed the REAL database access parameters).
I get this:
Fatal error: Call to undefined function: file_put_contents() in /homepages/38/d170218105/h
Odd, because I looked it up on php.net &Â that's an OK function
save-file-test.php
file_put_contents() should be a standard PHP function in 5.0 or later - are you running php 4.x? Â (run the phpinfo() function to find out)
Either way, you can replace these lines:
 // WRITE THE FILE TO THE SERVER
 file_put_contents('/tmp/my .xls', $buf);
With these
 // WRITE THE FILE TO THE SERVER
 $atch=fopen("/tmp/my.xls", "w");
 fputs($atch,$data);
 fclose($atch);
And then you can use the send_email() function to send the attachment as I describe above by putting in place of $fn in the function call "/tmp/my.xls" or setting $fn="/tmp/my.xls" before calling the function. Â That way you don't need the file_put_contents() function
Either way, you can replace these lines:
 // WRITE THE FILE TO THE SERVER
 file_put_contents('/tmp/my
With these
 // WRITE THE FILE TO THE SERVER
 $atch=fopen("/tmp/my.xls",
 fputs($atch,$data);
 fclose($atch);
And then you can use the send_email() function to send the attachment as I describe above by putting in place of $fn in the function call "/tmp/my.xls" or setting $fn="/tmp/my.xls" before calling the function. Â That way you don't need the file_put_contents() function
ASKER
To xterm &Â p_nuts,
This works PERFECTLY. Thanks so much, I don't know what to do about the points.
rkorts
This works PERFECTLY. Thanks so much, I don't know what to do about the points.
rkorts
Don't worry about the points - the important thing is that it works for you. Â Take care.
Diddo :P
ASKER
To xterm,
The email with attachment doesn't work.
It sends the email, but no attachment.
When I couldn't get it to work in the full program, I created a test program that JUST sends the email with a specific individual file that I KNOW is there. See attached.
You can run it at http://www.rkassoc.net/VAV/single_resev_rpt.php?conf=KH102911015
Maybe I have the path screwed up?
send-email-test.php
The email with attachment doesn't work.
It sends the email, but no attachment.
When I couldn't get it to work in the full program, I created a test program that JUST sends the email with a specific individual file that I KNOW is there. See attached.
You can run it at http://www.rkassoc.net/VAV/single_resev_rpt.php?conf=KH102911015
Maybe I have the path screwed up?
send-email-test.php
ASKER
To xterm,
Sorry, on the previous post I have the url wrong to run the test. It is
http://www.rkassoc.net/VAV/send_email_test.php
Thanks
Sorry, on the previous post I have the url wrong to run the test. It is
http://www.rkassoc.net/VAV/send_email_test.php
Thanks
trust me have a look at phpmailer class. that does it all for you and has error reporting so you find these things out easily..
if you don't want this..
change
 if (is_file($fn)) {
   $mbody.="\r\n$boundary\r\n Content-Ty pe: $type;\r\n";
   $mbody.=" name=\"$fn\"\r\n";
   $mbody.="Content-Transfer- Encoding: base64\r\n";
   $mbody.="Content-Dispositi on: attachment;\r\n";
   $mbody.=" filename=\"$fn\"\r\n\r\n";
   $fp=fopen($fn,'rb'); $matt=fread($fp,filesize($ fn)); fclose($fp);
   $mbody.=chunk_split(base64 _encode($m att),72);
  }
to
 if (is_file($fn)) {
   $mbody.="\r\n$boundary\r\n Content-Ty pe: $type;\r\n";
   $mbody.=" name=\"$fn\"\r\n";
   $mbody.="Content-Transfer- Encoding: base64\r\n";
   $mbody.="Content-Dispositi on: attachment;\r\n";
   $mbody.=" filename=\"$fn\"\r\n\r\n";
   $fp=fopen($fn,'rb'); $matt=fread($fp,filesize($ fn)); fclose($fp);
   $mbody.=chunk_split(base64 _encode($m att),72);
  }
else
{
echo ' it is not a file so i am screwed';
exit;
}
if the echo is there you know the path is wrong..
if you don't want this..
change
 if (is_file($fn)) {
   $mbody.="\r\n$boundary\r\n
   $mbody.=" name=\"$fn\"\r\n";
   $mbody.="Content-Transfer-
   $mbody.="Content-Dispositi
   $mbody.=" filename=\"$fn\"\r\n\r\n";
   $fp=fopen($fn,'rb'); $matt=fread($fp,filesize($
   $mbody.=chunk_split(base64
  }
to
 if (is_file($fn)) {
   $mbody.="\r\n$boundary\r\n
   $mbody.=" name=\"$fn\"\r\n";
   $mbody.="Content-Transfer-
   $mbody.="Content-Dispositi
   $mbody.=" filename=\"$fn\"\r\n\r\n";
   $fp=fopen($fn,'rb'); $matt=fread($fp,filesize($
   $mbody.=chunk_split(base64
  }
else
{
echo ' it is not a file so i am screwed';
exit;
}
if the echo is there you know the path is wrong..
ASKER
To p_nuts,
Thanks for all your help.
I tried the class you referenced.
(1) The example didn't work; php error.
(2) I removed the offending code from the example, it still didn't work, php error in the class itself.
So I put in the code you suggested with the echo if file not there, no echo, it sends the mail, NO ATTACHMENT.
Why is this so incredibly complex????
Thanks for all your help.
I tried the class you referenced.
(1) The example didn't work; php error.
(2) I removed the offending code from the example, it still didn't work, php error in the class itself.
So I put in the code you suggested with the echo if file not there, no echo, it sends the mail, NO ATTACHMENT.
Why is this so incredibly complex????
ASKER
To p_nuts,
I'm opening a new question on php mail with attachment. Look for it. I don't want to ask for more help with a closed question.
Thanks
I'm opening a new question on php mail with attachment. Look for it. I don't want to ask for more help with a closed question.
Thanks
Open in new window