Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:03:30+00:00 2026-06-04T06:03:30+00:00

I would like to point out before I get into this that I am

  • 0

I would like to point out before I get into this that I am a PHP newb, and I have been struggling for a while with this before finally deciding that I don’t know what I’m doing with it. I don’t think I misspelled anything or mis-capitalized like this guy but, please forgive my messy code.

<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_comment = $_POST['comment'];
$field_question = $_POST['question'];
$field_support = $_POST['support'];
$field_steam = $_POST['steam'];
$field_file = $_POST['file'];
$field_message = $_POST['message'];
//Get the uploaded file information
$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);
//get the file extension of the file
$type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1);
$size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;//size in KBs
//Settings
$max_allowed_file_size = 10000; // size in KB
$allowed_extensions = array("doc", "docx", "txt", "pdf", "rtf", "otf");
//Validations
if($size_of_uploaded_file > $max_allowed_file_size )
{
$errors .= "\n Size of file should be less than     $max_allowed_file_size";
}
//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
{
$allowed_ext = true;
}
}
if(!$allowed_ext)
{
$errors .= "\n The uploaded file is not supported file type. ".
" Only the following file types are supported: ".implode(',',$allowed_extensions);
}

//copy the temp. uploaded file to uploads folder
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path))
{
if(!copy($tmp_path,$path_of_uploaded_file))
{
$errors .= '\n error while copying the uploaded file';
}
}

$to = 'me@myemail.com';
$subject = 'Contact Form Message from '.$field_name;
$attachment = chunk_split(base64_encode(file_get_contents($path_of_uploaded_file)));

$boundary = md5(date('r', time())); 

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message Type: '." ";
$body_message .= ''.$field_comment." ";
$body_message .= ''.$field_question." ";
$body_message .= ''.$field_support." ";
$body_message .= ''.$field_steam."\n";
$body_message .= 'Message: '.$field_message;



$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$boundary."\""; 

$mail_status = mail($mail_to, $subject, $attachment, $body_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to me@myemail.com');
window.location = 'contact.html';
</script>
<?php
}
?>

And I keep getting this error:

Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in /%ROOT%/contact.php on line 57

My questions are, what am I doing wrong in my attempt to pass on the uploaded file as an attachment? And, why does $path_of_uploaded_file seem to be empty? Also, not everyone will be uploading files, so how could I allow the submit?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:03:31+00:00Added an answer on June 4, 2026 at 6:03 am

    Decided to go about it differently and cleaner.

    <?php
    //start session
    session_start();
    
    // prints form
    function print_form(){
    ?>
    
    <form method="post" action="<?php echo $_SERVER[’PHP_SELF’];?>" id="uploadform" enctype="multipart/form-data">
    <input name="name" id="name" type="text" style="border: none;background-color: transparent;background-image: url(images/text_field.png);width: 418px;height: 48px;padding-left: 15px;background-repeat: no-repeat;background-size: 450px 48px;padding-right: 13px;" class="field" value="Name" tabindex="1"/></p>
    <p>
    <input name="email" id="email" type="text" style="border: none;background-color: transparent;background-image: url(images/text_field.png);width: 418px;height: 48px;padding-left: 15px;background-repeat: no-repeat;background-size: 450px 48px;padding-right: 13px;float:right;margin-top: -65px;" class="field" value="Email" tabindex="2"/>
    </p>
    <p>
    <table width="950" style="padding-top: 10px;text-align: center;"><tr><td width="115"><input name="comment" type="checkbox" value="Comment" /> Comment </td><td width="119"><input name="question" type="checkbox" value="Question" /> Question </td><td width="139"><input name="support" type="checkbox" value="Support" /> Support </td><td width="171"><input name="steam" type="checkbox" value="Street Team" /> Street Team</td><td width="382"><input type="hidden" name="MAX_FILE_SIZE" value="2048"><span style=";margin-left: -180px;">Resume : </span><br />
    <span style="font-size:9px;margin-left: -180px;">1 file only, max file size 2 MB.</span><br /><span style="font-size:9px;margin-left: -180px;">Allowed file formats are </span><br /><span style="font-size:9px;margin-left: -180px;">.doc, .docx, .txt, .pdf, .rtf, .otf</span><br /></label>
    <input name="attachment" id="attachment" style="position: absolute;margin-left: 100px;margin-top: -40px;" type="file" tabindex="7"></td></tr></table><br />
    <textarea name="comments" id="comments" style="background-color: #fff;width: 922px;height: 172px;padding-left: 10px;background-repeat: no-repeat;padding-top:8px;resize:none;border-color: #CCC;padding-right: 15px;border-width: 2px;margin-left: 7px;border-radius: 1em;box-shadow: inset #EEE 0.25em 0.25em 0.3em;" rows="" cols="" class="field" tabindex="6">Message
    </textarea>
    </p>
    <p>
    <p><input type="submit" name="submit" id="submit" style="width: 112px;height: 32px;background-image: url(images/submit.png);color: transparent;border: none;background-repeat: no-repeat;background-position: -5px;float: right;" value="Send"  tabindex="8"/></p>
    <p><input type="hidden" name="submitted"  value="true" /></p>
    </form>
    <?php
    }
    
    // enquiry form validation
    
    function process_form() {
    // Read POST request params into global vars
    $to = "email@email.com";
    
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $comment = trim($_POST['comment']);
    $question = trim($_POST['question']);
    $support = trim($_POST['support']);
    $steam = trim($_POST['steam']);
    $comments = trim($_POST['comments']);
    $from = $email;
    $subject = 'Contact Form Message From '.$name;
    
    // Allowed file types. add file extensions WITHOUT the dot.
    $allowtypes=array("doc", "docx", "txt", "pdf", "rtf", "otf");
    
    // Require a file to be attached
    $requirefile="true";
    
    // Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handle it
    // post_max_size, upload_max_filesize, file_uploads, max_execution_time!
    $max_file_size="2048";
    
    // Thank you message
    $thanksmessage="Your message has been sent, we will respond as soon as possible.";
    
    $errors = array(); //Initialize error array
    
    //checks for a name
    if (empty($_POST['name']) ) {
    $errors[]='Please enter your name.';
    }
    
    //checks for an email
    if (empty($_POST['email']) ) {
    $errors[]='Please enter your email.';
    } else {
    
    if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email'])))) {
    $errors[]='Please enter a valid email address.';
    } // if eregi
    } // if empty email
    
    
    //checks for a message
    if (empty($_POST['comments']) ) {
    $errors[]='Did you want to tell us something?';
    }
    
    // checks for required file
    //if($requirefile=="true") {
    //  if($_FILES['attachment']['error']==4) {
    //      $errors[]='You forgot to attach a file';
    //  }
    //}
    
    //checks attachment file
    // checks that we have a file
    if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) {
    // basename -- Returns filename component of path
    $filename = basename($_FILES['attachment']['name']);
    $ext = substr($filename, strrpos($filename, '.') + 1);
    $filesize=$_FILES['attachment']['size'];
    $max_bytes=$max_file_size*1024;
    
    //Check if the file type uploaded is a valid file type. 
    if (!in_array($ext, $allowtypes)) {
    $errors[]="Invalid extension for your file: <strong>".$filename."</strong>";
    
    // check the size of each file
    } elseif($filesize > $max_bytes) {
    $errors[]= "Your file: <strong>".$filename."</strong> is too big. Max file size is ".$max_file_size."kb.";
    }
    
    } // if !empty FILES
    
    if (empty($errors)) { //If everything is OK
    
    // send an email
    // Obtain file upload vars
    $fileatt      = $_FILES['attachment']['tmp_name'];
    $fileatt_type = $_FILES['attachment']['type'];
    $fileatt_name = $_FILES['attachment']['name'];
    
    // Headers
    $headers = "From: $email";
    
    // create a boundary string. It must be unique
    $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";
    $message.="--{$mime_boundary}\n";
    $message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message.="Content-Transfer-Encoding: 7bit\n\n";
    $message.="From: ".$name."\n";
    $message.="Email: ".$email."\n";
    $message .= 'Message Type: '." ";
    $message .= ''.$comment." ";
    $message .= ''.$question." ";
    $message .= ''.$support." ";
    $message .= ''.$steam."\n";
    $message.="Message: ".$comments."\n\n";
    
    if (is_uploaded_file($fileatt)) {
    // Read the file to be attached ('rb' = read binary)
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    
    // 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";
    }
    
    
    // Send the completed message
    
    $envs = array("HTTP_USER_AGENT", "REMOTE_ADDR", "REMOTE_HOST");
    foreach ($envs as $env)
    $message .= "$env: $_SERVER[$env]\n";
    
    if(!mail($to,$subject,$message,$headers)) {
    exit("Mail could not be sent. Sorry! An error has occurred, please email info@cannysoftware.com.\n");
    } else {
    echo '<div id="formfeedback"><h3>Thank You!</h3><p>'. $thanksmessage .'</p></div>';
    unset($_SESSION['myForm']);
    print_form();
    
    } // end of if !mail
    
    } else { //report the errors
    echo '<div id="formfeedback"><h3>Error!</h3><p>The following error(s) have occurred:<br />';
    foreach ($errors as $msg) { //prints each error
    echo " - $msg<br />\n";
    } // end of foreach
    echo '</p><p>Please correct the errors and try again.</p></div>';
    print_form();
    } //end of if(empty($errors))
    
    } // end of process_form()
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, before I ask, i would like to point out that this question
I would like to get started with developing NFC apps for android. I have
I've been trying to figure this out for a while now and can't figure
I have table that has a column of type DateTime, I would like to
I would like to test if a point is within a particular distance of
I would like to implement an distributed Point-Of-Sale system, somewhat like the one described
I would like to covert a 4 digit hexadecimal code point to UTF8. For
We're approaching a point of replacing several of our developer PCs and would like
For given floating point numbers x and a , I would like to compute
I would like to print raw phone number record (31999999) as 31-999-999 (PHP). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.