The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP: Exporting Data to Excel Reply To: PHP: Exporting Data to Excel


Good morning,

i could complete my task.
thanks for help.

I have saved the data from database in to excel using PHP code. now i want to send that file from the specific folder location which i am giving in code.
i have code. code get the same file from location even send a mail as well but file dont have content. however the file resides in audit folder have the data.

here is the code.

please help me out.

<title>Sending attachment using PHP</title>
$to = “[email protected]”;
$subject = “Audit_report”;
$message = “Hello All, PFA audit report .”;
# Open a file
$file = fopen( “G:/Smart solution/Audit/Audit_report.csv”, “r” );
if( $file == false )
echo “Error in opening file”;
# Read the file into a variable
$size = filesize(“G:/Smart solution/Audit/Audit_report.csv”);
$content = fread( $file, $size);

# encode the data for safe transit
# and insert \r\n after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));

# Get a random 32 bit number using time() as seed.
$num = md5( time() );

# Define the main headers.
$header = “From:[email protected]\r\n”;
$header .= “MIME-Version: 1.0\r\n”;
$header .= “Content-Type: multipart/mixed; “;
$header .= “boundary=$num\r\n”;
$header .= “–$num\r\n”;

# Define the message section
$header .= “Content-Type: text/plain\r\n”;
$header .= “Content-Transfer-Encoding:8bit\r\n\n”;
$header .= “$message\r\n”;
$header .= “–$num\r\n”;

ini_set("sendmail_from","[email protected]");
ini_set("sendmail_path", "C:\wamp\bin\sendmail.exe -t");    

# Define the attachment section
$header .= “Content-Type: multipart/mixed; “;
$header .= “name=\”test.csv\”\r\n”;
$header .= “Content-Transfer-Encoding:base64\r\n”;
$header .= “Content-Disposition:attachment; “;
$header .= “filename=\”Audit_report.csv\”\r\n\n”;
$header .= “$encoded_content\r\n”;
$header .= “–$num–“;

# Send email now
$retval = mail ( $to,$subject,””,$header);
if( $retval == true )
echo “Message sent successfully…”;
echo “Message could not be sent…”;
<?php header(‘location:issues.php’);

Thanks in advance