Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

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
Avatar of xterm
xterm

Here is a function I wrote to send an attachment via the PHP mail() function, and it will take your existing $fn as an argument.  It will need the full path to the file, so if $fn now is just "foo.xls", you will need to append the path and make it "/path/to/foo.xls" for this to work unless foo.xls is actually in the current directory that your web page is in.
function send_email($from,$rcpt,$body,$subj,$rpath,$fn) {
  $boundary = '-----_NEXT_'.md5(uniqid(time()));
  $bounddef = substr($boundary,2,strlen($boundary));
  $body=stripslashes($body); $subj=stripslashes($subj);
  // ALL ADDITIONAL HEADERS
  $headers = "X-Mailer: Xterm's Mailer 1.0\r\n";
  if ($att) {
    $headers.="MIME-Version: 1.0\r\nContent-Type: multipart/mixed;\r\n";
    $headers.=" boundary=\"$bounddef\"\r\n";
    $mbody="This is a multi-part message in MIME format.\r\n\r\n";
    $mbody.="Sorry, your email reader does not support MIME\r\n\r\n";
    $mbody.="$boundary\nContent-Type: text/plain; charset=us-ascii; ";
    $mbody.="format=flowed\r\nContent-Transfer-Encoding: 7bit\r\n\r\n";
    $mbody.="$body\r\n\r\n";
    $type="application/vnd.ms-excel";
    if (is_file($fn)) {
      $mbody.="\r\n$boundary\r\nContent-Type: $type;\r\n";
      $mbody.=" name=\"$fn\"\r\n";
      $mbody.="Content-Transfer-Encoding: base64\r\n";
      $mbody.="Content-Disposition: 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($matt),72);
    }
    $mbody.="$boundary--"; $body=$mbody;
  }
  // SEND MAIL
  mail("$rcpt", "$subj","$body","From: $from\r\n$headers\r\n","-f".$rpath);
}

Open in new window

Avatar of Richard Korts

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!"
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.
Avatar of p_nuts
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..
To p_nuts,

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">&nbsp;</td>										
	</tr>
  <tr>
			<td align="center" bgcolor="#FFC000">&nbsp;</td>
			<td align="center" bgcolor="#92D050">&nbsp;</td>		
			<td align="center" bgcolor="#FFFF00">&nbsp;</td>
			<td align="center" bgcolor="#A5A5A5">&nbsp;</td>		
			<td align="center" bgcolor="#93CDDD">&nbsp;</td>
			<td align="center" bgcolor="#008080">&nbsp;</td>		
			<td align="center" bgcolor="#9966FF">&nbsp;</td>
			<td align="center" bgcolor="#FF99CC">&nbsp;</td>			
			<td align="center" bgcolor="#FF8080">&nbsp;</td>
			<td align="center" bgcolor="#00FFFF">&nbsp;</td>		
			<td align="center" bgcolor="#00B0F0">&nbsp;</td>
			<td align="center" bgcolor="#E26B0A">&nbsp;</td>		
			<td align="center" bgcolor="#7030A0">&nbsp;</td>
			<td align="center" bgcolor="#00FF00">&nbsp;</td>	
			<td colspan="6">&nbsp;</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>&nbsp;</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>

Open in new window

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.
<?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">&nbsp;</td>										
	</tr>
  <tr>
			<td align="center" bgcolor="#FFC000">&nbsp;</td>
			<td align="center" bgcolor="#92D050">&nbsp;</td>		
			<td align="center" bgcolor="#FFFF00">&nbsp;</td>
			<td align="center" bgcolor="#A5A5A5">&nbsp;</td>		
			<td align="center" bgcolor="#93CDDD">&nbsp;</td>
			<td align="center" bgcolor="#008080">&nbsp;</td>		
			<td align="center" bgcolor="#9966FF">&nbsp;</td>
			<td align="center" bgcolor="#FF99CC">&nbsp;</td>			
			<td align="center" bgcolor="#FF8080">&nbsp;</td>
			<td align="center" bgcolor="#00FFFF">&nbsp;</td>		
			<td align="center" bgcolor="#00B0F0">&nbsp;</td>
			<td align="center" bgcolor="#E26B0A">&nbsp;</td>		
			<td align="center" bgcolor="#7030A0">&nbsp;</td>
			<td align="center" bgcolor="#00FF00">&nbsp;</td>	
			<td colspan="6">&nbsp;</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>&nbsp;</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);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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-Disposition: 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?

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
To Ray_Paseur:

Sorry, the previous post had the wrong image. See attached. The prior one was my file.
ray-paseur.jpg
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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.
Did I misread this

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 ..
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?
Please see post id #37090612 above - is the code writing the file to disk, or is it not?
To xterm

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.
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
<? 
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">&nbsp;</td>										
	</tr>
  <tr>
			<td align="center" bgcolor="#FFC000">&nbsp;</td>
			<td align="center" bgcolor="#92D050">&nbsp;</td>		
			<td align="center" bgcolor="#FFFF00">&nbsp;</td>
			<td align="center" bgcolor="#A5A5A5">&nbsp;</td>		
			<td align="center" bgcolor="#93CDDD">&nbsp;</td>
			<td align="center" bgcolor="#008080">&nbsp;</td>		
			<td align="center" bgcolor="#9966FF">&nbsp;</td>
			<td align="center" bgcolor="#FF99CC">&nbsp;</td>			
			<td align="center" bgcolor="#FF8080">&nbsp;</td>
			<td align="center" bgcolor="#00FFFF">&nbsp;</td>		
			<td align="center" bgcolor="#00B0F0">&nbsp;</td>
			<td align="center" bgcolor="#E26B0A">&nbsp;</td>		
			<td align="center" bgcolor="#7030A0">&nbsp;</td>
			<td align="center" bgcolor="#00FF00">&nbsp;</td>	
			<td colspan="6">&nbsp;</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>&nbsp;</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;
}

?>

Open in new window

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.
sorry line 209 should not be

$mailer  = new phpmailer();

but should be

$m = new phpmailer();

To xterm,

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.
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.



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.
p_nuts:

OK, I'll look at that later & let you know.

rkorts
Did you fix it?
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/htdocs/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
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
To xterm & p_nuts,

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
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
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
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\nContent-Type: $type;\r\n";
      $mbody.=" name=\"$fn\"\r\n";
      $mbody.="Content-Transfer-Encoding: base64\r\n";
      $mbody.="Content-Disposition: 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($matt),72);
    }

to
 if (is_file($fn)) {
      $mbody.="\r\n$boundary\r\nContent-Type: $type;\r\n";
      $mbody.=" name=\"$fn\"\r\n";
      $mbody.="Content-Transfer-Encoding: base64\r\n";
      $mbody.="Content-Disposition: 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($matt),72);
    }
else
{
echo ' it is not a file so i am screwed';
exit;
}

if the echo is there you know the path is wrong..

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????
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