Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload/delete file and email problem

Status
Not open for further replies.

camcim

Programmer
Jan 25, 2003
20
0
0
US
Hi all,
I'm currently fiddling around with a file upload script (which also emails me the file).
It appears to be neally working except i am unsure as to what to name the $fileatt_type variable. At the moment it is set to "csv" but i don't think this is correct.
I say this be it upload to the server OK but the email contains the file attachment but with no data ($csv_contents).
What do i need to name the $fileatt_type variable and how do i delete a file that is already in the download folder with the same name as the one to be uploaded?

Here's the code to date:
PHP:
<?
include (&quot;include/dbconnect_admin.php&quot;);
// get each row of the table into a a var, 
// separated by commas, ended with \n 
$csv_contents = ''; 
$sql = &quot;SELECT * FROM payment_details&quot;; 
$result = mysql_query($sql); 
while($myrow = mysql_fetch_array($result)) 
    { 
    // you have a row 
    $csv_contents .= &quot;{$myrow['listing_order']},{$myrow['payment_details_cardnumber']},{$myrow['payment_details_amount']},{$myrow['payment_details_cardholdersname']},{$myrow['payment_details_cardtype']},{$myrow['payment_details_cardexpirydate_month']},{$myrow['payment_details_cardexpirydate_year']},{$myrow['payment_memo']}\n&quot;; 
    } 


function write_to_file($filename, $stringtowrite, $writetype) 
    { 
    // lifted from the manual 
    $filesession = fopen($filename,$writetype); 
    fwrite($filesession,&quot;$stringtowrite&quot;); 
    fclose($filesession); 
    } 

write_to_file('download/source_book.csv',$csv_contents, 'w');

$to = &quot;micmac@ripefruit.com&quot;;   
$subject = &quot;XER - CSV Data&quot;;   
$message = &quot;CSV Download&quot;;

// Obtain file upload vars 
$fileatt = &quot;/path/to/download/&quot;; // Location of file on server 
$fileatt_type = &quot;csv&quot;;// Type of file being sent 
$fileatt_name = &quot;source_book.csv&quot;;// Name of file 

$headers = &quot;From: Xer Publishing <webmaster@infoquorum.com>&quot;; 
// If the file exists... 
if (file_exists($fileatt)) { 
// Read the file to be attached ('rb' = read binary) 
$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file);  

// Generate a boundary string 
$semi_rand = md5(time()); 
$mime_boundary = &quot;==Multipart_Boundary_x{$semi_rand}x&quot;; 

// Add the headers for a file attachment 
$headers .= &quot;\nMIME-Version: 1.0\n&quot; . 
&quot;Content-Type: multipart/mixed;\n&quot; . 
&quot; boundary=\&quot;{$mime_boundary}\&quot;&quot;; 

// Add a multipart boundary above the plain message 
$message = &quot;This is a multi-part message in MIME format.\n\n&quot; . 
&quot;--{$mime_boundary}\n&quot; . 
&quot;Content-Type: text/html; charset=\&quot;iso-8859-1\&quot;\n&quot; . 
&quot;Content-Transfer-Encoding: 7bit\n\n&quot; . 
$message . &quot;\n\n&quot;; 

// Base64 encode the file data 
$data = chunk_split(base64_encode($data)); 

// Add file attachment to the message 
$message .= &quot;--{$mime_boundary}\n&quot; . 
&quot;Content-Type: {$fileatt_type};\n&quot; . 
&quot; name=\&quot;{$fileatt_name}\&quot;\n&quot; . 
//&quot;Content-Disposition: attachment;\n&quot; . 
//&quot; filename=\&quot;{$fileatt_name}\&quot;\n&quot; . 
&quot;Content-Transfer-Encoding: base64\n\n&quot; . 
$data . &quot;\n\n&quot; . 
&quot;--{$mime_boundary}--\n&quot;; 
} 
$ok = @mail($to, $subject, $message, $headers); 

if($ok){
echo &quot;Your information has been passed successfully&quot;;
} else {
echo &quot;Your information could not be passed at this time<br><br>&quot;;
echo &quot;Please try again later&quot;;
}
?>

Cheers,
camcim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top