I try to solve my error today through the internet. means...for the first time i register into a few forum. until now(night), i still cant found the solution.
What should i do?

i create a booking form with the attachment. But my problem is, i unable to open that attachment(pdf file). after the form send to my email(just testing), then i download it. Here's the problem comes out. 'Unable to open the file. Not a valid PDF file.'

and 1 more thing, this error also comes out after i submit my form.
Warning: fclose(): supplied argument is not a valid stream resource in D:\xampplite\htdocs\borneotours02\booking2.php on line 268

My form will save the data inside database and also will send the it to the person's email(who submit it).

Here's my code:

<? require_once("Connections/pamconnection.php");
$cart_id=session_id();

if($_POST['Submit']=='Submit'){

if(mysql_query("INSERT INTO inquiry_log1 (id, session_id, tour, name, contact, email02, pdf_title, itinerary, date02,class_travel,f_detail, airlines, status, date_posted, time)
VALUES ('', '".$cart_id."',
'".mysql_real_escape_string($_POST['tour'])."',
'".mysql_real_escape_string($_POST['name'])."',
'".mysql_real_escape_string($_POST['contact'])."',
'".mysql_real_escape_string($_POST['email02'])."',
'".mysql_real_escape_string($_POST['pdf_file'])."',
'".mysql_real_escape_string($_POST['itinerary'])."',
'".mysql_real_escape_string($_POST['date02'])."',
'".mysql_real_escape_string($_POST['class_travel'])."',
'".mysql_real_escape_string($_POST['flight_detail'])."',
'".mysql_real_escape_string($_POST['airlines'])."',
1, '".date("Y-m-d")."', '".date("g:i a")."')"))


if($_FILES['pdf_file']!='')
{
$fileatt = $HTTP_POST_FILES['pdf_file']['tmp_name'];
$fileatt_type = $HTTP_POST_FILES['pdf_file']['type'];
$file_name = $HTTP_POST_FILES['pdf_file']['name'];
$ext = substr(strrchr($fileatt_type, "/"), 1);
switch ( $ext )
{
case 'pdf':
$fileatt_name = $file_name;
break;
case 'msword':
$fileatt_name = $file_name;
break;
case 'vnd.openxmlformats-officedocument.wordprocessingml.document':
$fileatt_name = $file_name;
break;
}
}

$email_from = $_POST['email02']; // Who the email is from
$email_subject = "Outbound Booking Form"; // The Subject of the email
$email_message.='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<!--<style>
.title{ font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; font-weight:bold; }
.content{ font-family:Verdana, Arial, Helvetica, sans-serifl; font-size:12px;}
</style>-->
<body>
<table width="600" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
<tr><td>
<table width="100%" border="0" align="center" cellpadding="4" cellspacing="6" bgcolor="#DCE1E9" class="content">
<tr>
<td colspan="2" class="title">Online Booking Form</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="right">'.date("jS F Y").'</td>
</tr>
<tr class="style9">
<td>&nbsp;</td>
</tr>';

if($_POST['tour']!=''){
$email_message.='<tr class="style9">
<td width="32%" class="title02">Tour Package Name</td>
<td width="68%" class="email_message">'.$_POST['tour'].'</td>
</tr>';}
$email_message.='
<tr class="style9"><td colspan="2" class="title" bgcolor="#FFFFFF"><strong>CONTACT DETAILS</strong></td></tr>';
if($_POST['name']!=''){
$email_message.='<tr class="style9">
<td width="32%" class="title02">Name</td>
<td width="68%" class="email_message">'.$_POST['name'].'</td>
</tr>';}
if($_POST['contact']!=''){
$email_message.='<tr class="style9">
<td class="title02">Contact Person</td>
<td class="email_message">'.$_POST['contact'].'</td>
</tr>';}
if($_POST['email02']!=''){
$email_message.='<tr class="style9">
<td class="title02">Email Address</td>
<td class="email_message">'.$_POST['email02'].'</td>
</tr>';}
$email_message.='
<tr class="style9"><td colspan="2" class="title" bgcolor="#FFFFFF"><strong>ITINERARY</strong></td></tr>';
if($_POST['pdf_file']!=''){
$email_message.='<tr class="style9">
<td valign="top" class="title02">Itinerary File</td>
<td class="email_message">'.$_POST['pdf_file'].'</td>
</tr>';}
if($_POST['itinerary']!=''){
$email_message.='<tr class="style9">
<td valign="top" class="title02">Itinerary</td>
<td class="email_message">'.$_POST['itinerary'].'<br><br></td>
</tr>';}


$email_message.='
<tr class="style9"><td colspan="2" class="title" bgcolor="#FFFFFF"><strong>FLIGHTS</strong></td></tr>';
if($_POST['date02']!=''){
$email_message.='<tr class="style9">
<td valign="top" class="title02">Date Of Flights</td>
<td class="email_message">'.$_POST['date02'].'</td>
</tr>';}
if($_POST['class_travel']!=''){
$email_message.='<tr class="style9">
<td valign="top" class="title02">Class Of Travel</td>
<td class="email_message">'.$_POST['class_travel'].'</td>
</tr>';}


if($_POST['f_detail']!=''){
$email_message.='<tr class="style9">
<td valign="top" class="title02">Flight Details</td>
<td class="email_message">'.$_POST['f_detail'].'</td>
</tr>';}

if($_POST['airlines']!=''){
$email_message.='<tr class="style9">
<td valign="top" class="title02">Preferred Of Airlines</td>
<td class="email_message">'.$_POST['airlines'].'</td>
</tr>';}

$email_message.='<tr class="style9">
<td colspan="2" valign="top"><div align="center">
</div></td>
</tr>
</table>
</td></tr></table></body>
</html>';

$email_to = "ee_elizebert@hotmail.com"; // Who the email is to
ini_set(SMTP, "mail.sarawakhost.com");
ini_set(smtp_port, "587");
ini_set(sendmail_from, $email);
$headers = "From: ".$email_from;


$file = fopen($fileatt,'rb');
//$data = fread($file,filesize($fileatt));
fclose($file);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$email_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" .
$email_message .= "\n\n";

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";


$ok = mail($email_to, $email_subject, $email_message, $headers);

if($ok) {
$send='<font color=#336600>Feedback sent</font>';
} else {
$send='<font color=#CC3300>Failed to send. Please try again.</font>';
}



}?>

    Correction: for this code, i already fix a few but i still cant do my form.

    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    $data = chunk_split(base64_encode($data));

      You should check to see if your call to [man]fopen[/man] works. You might also want to check if the file [man]is_readable[/man] before attempting to do anything with it.

      Lastly, you might want to check the value returned by filesize($fileatt) to make sure it's not zero.

      Sounds to me like you are having a problem opening the file.

        Now, after i add in these code:

        $file = fopen($fileatt,'rb'); 
        $data = fread($file,filesize($fileatt)); 
        fclose($file); 
        $data = chunk_split(base64_encode($data)); 

        I cant see my attachment go to my email anymore.

        You should check to see if your call to fopen works. You might also want to check if the file is_readable before attempting to do anything with it.

        <-- how to check fopen? because inside my form, after i submit it, the error still appear.

          ms_liz_87;10980034 wrote:

          ...

          <-- how to check fopen? because inside my form, after i submit it, the error still appear.

          $file = fopen($fileatt,'rb'); 
          if($file == false) {
             die("Could not open file '$fileatt'");
          }
          

            This is my new code after i modified it.
            Nogdog,
            This error comes out is...Could not open file '' after i try use the code u give.

            <? require_once("Connections/pamconnection.php");
            
            
            if($_POST['Submit']=='Submit'){
            
            
            
            if($_FILES['pdf_file']!='')
            {
            	$fileatt = $_FILES['pdf_file']['tmp_name'];
            	$fileatt_type = $_FILES['pdf_file']['type']; 
            	$file_name = $_FILES['pdf_file']['name'];
            	$ext = substr(strrchr($fileatt_type, "/"), 1);
            	switch ( $ext )
            	{ 
            		case 'pdf':
            			$fileatt_name = $file_name;
            			break;
            		case 'msword': 
            			$fileatt_name = $file_name;
            			break;
            		case 'vnd.openxmlformats-officedocument.wordprocessingml.document': 
            			$fileatt_name = $file_name;
            			break; 
            	}
            }	
            
            
            $email_to = "ee_elizebert@hotmail.com"; // Who the email is to 
            ini_set(SMTP, "mail.sarawakhost.com");
            ini_set(smtp_port, "587");
            ini_set(sendmail_from, $email);
            $headers = "From: ".$email_from; 
            
            $email_to = "ee_elizebert@hotmail.com"; // Who the email is to 
            ini_set(SMTP, "mail.sarawakhost.com"); 
            ini_set(smtp_port, "587"); 
            ini_set(sendmail_from, $email); 
            $headers = "From: ".$email_from; 
            
            $file = fopen($fileatt,'rb'); 
            if($file == false) { 
               die("Could not open file '$fileatt'"); 
            }
            
            $data = fread($file,filesize($fileatt)) or die ("\n Unable to read file"); 
            fclose($file) or die("Unable to close file"); 
            $data = chunk_split(base64_encode($data)); 
            
            $semi_rand = md5(time()); 
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
            
            $headers .= "\nMIME-Version: 1.0\n" . 
            "Content-Type: multipart/mixed;\n" . 
            " boundary=\"{$mime_boundary}\""; 
            
            $email_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" . 
            $email_message . "\n\n"; 
            
            $email_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($email_to, $email_subject, $email_message, $headers); 
            
            if($ok) { 
            $send='<font color=#336600>Feedback sent</font>';
            } else { 
            $send='<font color=#CC3300>Failed to send. Please try again.</font>';
            } 		
            
            
            
            }?>

              It would appear $fileatt is empty?

              Maybe try adding this to the top of the script to make sure all notices/warnings are displayed:

              <?php
              ini_set('display_errors', 1); // set to 0 for production version
              error_reporting(E_ALL);
              

                NogDog: Ok. i try put it at the top. then, this error showing up after i submit the form.


                Notice: Undefined variable: fileatt in D:\xampplite\htdocs\borneotours02\booking02.php on line 267

                Notice: Undefined variable: fileatt in D:\xampplite\htdocs\borneotours02\booking02.php on line 269
                Could not open file ''

                $file = fopen($fileatt,'rb'); //<--- line 267
                if($file == false) { 
                   die("Could not open file '$fileatt'"); 
                }

                  You have not defined the variable $fileatt which is supposed to contain the filename of the file you are trying to open before you try to open it.

                    My first guess would be that this if() condition is not firing:

                    if($_FILES['pdf_file']!='') 
                    

                    ...which would suggest that the file was not uploaded (or you have a typo in the field name you used). First thing to check is that your <form> tag that submits the file is using the correct enctype:

                    <form enctype="multipart/form-data" method="post" . . . >
                    

                      yup. that one, already confirm it. if i put my form online, there will show a lot of error. but inside my own server, its ok. thats mean, when the form goes to my email, it will show multiple form to my email just like this.(attachment)

                        Write a Reply...