Hi

I've been trying to get a email form which allows my users to send photos as an attachemt via a "upload" button. I've got it working i.e. the form is sending me the data plus the photos, but I'm still getting this error when "submit" is pressed.

Notice: Undefined variable: fileatt_name in d:\inetpub\mm.models121.co.uk\uploader.php on line 94

This is line 94 of my code :

" filename=\"{$fileatt_name}\"\n" .

I'm not sure what is going on here.

Anybody got any ideas?

Leroy

Dani AI

Generated

The error means the script is inserting a filename variable that was never set. The upload loop already reads the original filename into the variable $name, so either use that value when building the MIME headers or explicitly set the variable the header expects before you compose the attachment section. was correct to point out that the header is referencing the wrong/undefined variable.

Quick checklist to stop other common problems:

  • Confirm the upload actually succeeded by checking the file error value (verify UPLOAD_ERR_OK) and that tmp_name points to a real file.
  • Read the file from the server temporary path (tmp_name) and base64‑encode that content before appending it to the message; do not try to use a client-side path. This is the usual cause when attachments open empty (as reported by ).
  • Use is_uploaded_file() (or move_uploaded_file() if saving) rather than relying only on file_exists(), and verify filesize() > 0.
  • Build MIME parts with consistent CRLF (\r\n) line endings, start each boundary with --, and include the closing -- on the final boundary so mail clients parse attachments correctly.

Security and reliability notes:

  • Sanitize the incoming filename (use basename) and guard against header injection.
  • Manual MIME assembly is fragile; for production use a maintained mail library (PHPMailer/SwiftMailer) that handles multipart/attachment details for you.
  • Ignore suggestions to try to capture a full client filesystem path via JS — browsers do not reliably expose that and it isn’t needed. ’s auto-upload idea is unnecessary and can introduce security/complexity.

Short practical next steps: replace the undefined filename reference with the variable that actually holds the uploaded name, run print_r($_FILES) to confirm values, then resend and inspect the raw MIME to verify the attachment block contains the base64 data.

Recommended Answers

All 4 Replies

Here is the code I'm using. I'm not sure how I'm supposed to make the variable?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Send Your Photo's to Models 121</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
    font-weight: bold;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: x-small;
}
.style2 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style3 {font-size: x-small}
.style4 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: x-small; }
-->
</style>
</head>
<body>


<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){

   // we'll begin by assigning the To address and message subject
   $to="lee@leeosullivan.com";
   $subject="Modeling Photos";


   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['fromname'])." < ".stripslashes($_POST['fromemail'])." >";

   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   // now we'll build the message headers
   $headers = "From: $from\r\n" .
   "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";

   // here, we'll start the message body.
   // this is the text that will be displayed
   // in the e-mail
   $message="These photos have been sent by {$from}  ";

   // next, we'll build the invisible portion of the message body
   // note that we insert two dashes in front of the MIME boundary 
   // when we use it
   $message = "This is a multi-part message in MIME format.\n\n" .
      "--{$mime_boundary}\n" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
   $message . "\n\n";

   // now we'll process our uploaded files
   foreach($_FILES as $userfile){
      // store the file information to variables for easier access
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];

      // if the upload succeded, the file will exist
      if (file_exists($tmp_name)){

         // check to make sure that it is an uploaded file and not a system file
         if(is_uploaded_file($tmp_name)){

            // open the file for a binary read
            $file = fopen($tmp_name,'rb');

            // read the file content into a variable
            $data = fread($file,filesize($tmp_name));

            // close the file
            fclose($file);

            // now we encode it and split it into acceptable length lines
            $data = chunk_split(base64_encode($data));
         }

         // now we'll insert a boundary to indicate we're starting the attachment
         // we have to specify the content type, file name, and disposition as
         // an attachment, then add the file content.
         // NOTE: we don't set another boundary to indicate that the end of the 
         // file has been reached here. we only want one boundary between each file
         // we'll add the final one after the loop finishes.
         $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n";
      }
   }
   // here's our closing mime boundary that indicates the last of the message
   $message.="--{$mime_boundary}--\n";
   // now we just send the message
   if (@mail($to, $subject, $message, $headers))
      echo "Thank you for posting your photos to Models 121.  We will be in touch very soon.";
   else
      echo "Failed to send";
} else {
?>
<div align="center"><img src="logoforflashcards.jpg" width="167" height="40">
</div>
<p align="center" class="style1">Photo uploading page.</p>
<p align="center" class="style1">To complete your application, please send us at least 10 of your best photos.</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1" class="style1">
   <div align="center">
     <table width="600" border="0" align="center" cellpadding="3" cellspacing="3">
      <tr>
         <td width="283" height="29"><div align="left">Your Name:&nbsp;&nbsp;               
             <input type="text" name="fromname">
        </div></td>
         <td width="26">&nbsp;</td>
         <td width="291">&nbsp;</td>
       </tr>
       <tr>
         <td><div align="left">Your E-Mail:&nbsp;            
           <input type="text" name="fromemail">
         </div></td>
         <td>&nbsp;</td>
         <td>&nbsp;</td>
       </tr>
       <tr>
         <td>&nbsp;</td>
         <td>&nbsp;</td>
         <td>&nbsp;</td>
       </tr>
       <tr>
         <td><div align="left">Photo 1 :
            <input type="file" name="file1">
         </div></td>
         <td><div align="left"></div></td>
         <td><div align="left">Photo 6 :
            <input name="file6" type="file" id="file6">
         </div></td>
       </tr>
       <tr>
         <td><div align="left">Photo 2 :
            <input type="file" name="file2">
         </div></td>
         <td><div align="left"></div></td>
         <td><div align="left">Photo 7 :
            <input name="file7" type="file" id="file7">
         </div></td>
       </tr>
       <tr>
         <td><div align="left">Photo 3 :
            <input name="file3" type="file" id="file3">
         </div></td>
         <td><div align="left"></div></td>
         <td><div align="left">Photo 8 :
            <input name="file8" type="file" id="file8">
         </div></td>
       </tr>
       <tr>
         <td><div align="left">Photo 4 :
            <input name="file4" type="file" id="file4">
         </div></td>
         <td><div align="left"></div></td>
         <td><div align="left">Photo 9 :
            <input name="file9" type="file" id="file9">
         </div></td>
       </tr>
       <tr>
         <td><div align="left">Photo 5 :
            <input name="file5" type="file" id="file5">
         </div></td>
         <td><div align="left"></div></td>
         <td><div align="left">Photo 10 :
            <input name="file10" type="file" id="file10">
         </div></td>
       </tr>
       <tr>
         <td>&nbsp;</td>
         <td><span class="style4"><strong>
         <input type="submit" name="Submit" value="Upload">
         </strong></span></td>
         <td>&nbsp;</td>
       </tr>
        </table>
   </div>
   <p align="center">&nbsp;</p>
   <p align="center">&nbsp;</p>
   <p align="center">&nbsp;</p>
   <p align="center">&nbsp;</p>
</form>
<p align="center" class="style4"><strong>
  <?php } ?>
</strong></p>
<p align="center" class="style4">&nbsp;</p>
<p align="center" class="style4">&nbsp;</p>
<p align="center" class="style4"><strong>
  </strong>
</p>
</body>
</html>

Like the error message makes perfectly clear, the variable is not defined anywhere before that call. Are you trying to send over the attachment's filename? Judging from your code, isn't that being stored in $name?

-Fredric

Hi,
I am also coding the same but sending word and excel as attachment. If the explictly give the path name from server its working , but when i try to browse file from my PC its not working.
Email is send but if i open the attachment there is no text/content in it.
How do i need to open a file from my PC and attach through mail.

<form action="mail_attachment.php" method="POST" enctype="multipart/form-data">

<input type="file" name="fileatt"><br>

<input type="submit">
</form>
$fileatt = "/home/myfile/public_html/myreport.xls"; // Path to the file 
$fileatt_type = "application/octet-stream"; // File Type 
$fileatt_name = "myreport.xls"; // Filename that will be used for the file as the attachment 

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

Insted of giving the path i need to browse from my PC.
Please help
Thanks.

Member Avatar for Member #210412

add a simple java script to upload file automatically to web server temp cache ( i meant to call upload file page ) and catch full path of the uploaded file then you will be able to do what you want..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.