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:
Cheers,
camcim
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 ("include/dbconnect_admin.php");
// get each row of the table into a a var,
// separated by commas, ended with \n
$csv_contents = '';
$sql = "SELECT * FROM payment_details";
$result = mysql_query($sql);
while($myrow = mysql_fetch_array($result))
{
// you have a row
$csv_contents .= "{$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";
}
function write_to_file($filename, $stringtowrite, $writetype)
{
// lifted from the manual
$filesession = fopen($filename,$writetype);
fwrite($filesession,"$stringtowrite");
fclose($filesession);
}
write_to_file('download/source_book.csv',$csv_contents, 'w');
$to = "micmac@ripefruit.com";
$subject = "XER - CSV Data";
$message = "CSV Download";
// Obtain file upload vars
$fileatt = "/path/to/download/"; // Location of file on server
$fileatt_type = "csv";// Type of file being sent
$fileatt_name = "source_book.csv";// Name of file
$headers = "From: Xer Publishing <webmaster@infoquorum.com>";
// 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 = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
$ok = @mail($to, $subject, $message, $headers);
if($ok){
echo "Your information has been passed successfully";
} else {
echo "Your information could not be passed at this time<br><br>";
echo "Please try again later";
}
?>
Cheers,
camcim