HTML FORM文件上传发送下载链接到电子邮件

时间:2011-07-24 23:13:40

标签: html email file-upload

我有一个HTML,PHP - Jquery表单。最后一个输入是文件上传。我想将文件名显示为URL链接,以便管理员可以通过单击链接下载文件。

以下是文件>的示例http://www.bgv.co.za/testspace/abstract.php

以下是代码:

<?php

$subject = "Abstract Submission";
$sasquach = $_FILES['datafile']['name'];

//If the form is submitted

if(isset($_POST['submit'])) {

//Check to make sure that the first name field is not empty
if(trim($_POST['firstname']) == '') {
    $hasError = true;
} else {
    $firstname = trim($_POST['firstname']);
}

//Check to make sure that the last name field is not empty
if(trim($_POST['lastname']) == '') {
    $hasError = true;
} else {
    $lastname = trim($_POST['lastname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

//Check to make sure that the theme has been selected
if(($_POST['theme']) == '') {
    $hasError = true;
} else {
    $theme = ($_POST['theme']);
}

//Check to make sure that the type of abstract has been selected
if(($_POST['type']) == '') {
    $hasError = true;
} else {
    $type = ($_POST['type']);
}

//Сheck that we have a file
if((!empty($_FILES["datafile"])) && ($_FILES['datafile']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 2mb
  $filename = basename($_FILES['datafile']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["datafile"]["type"] == "image/jpeg") && 
    ($_FILES["datafile"]["size"] < 2100000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/upload/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['datafile']['tmp_name'],$newname))) {
           $sasquach = "The file has been saved as: ".$newname;
        } else {
           //echo "Error: A problem occurred during file upload!";
        }
      } else {
         //echo "Error: File ".$_FILES["datafile"]["name"]." already exists";
      }
  } else {
     //echo "Error: Only .jpg images under 2mb are accepted for upload";
  }
} else {
 //echo "Error: No file uploaded";
}

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = 'info@bgv.co.za'; //Put your own email address here
    $body = "First Name: $firstname \n\nLast Name: $lastname \n\nEmail: $email \n\nTheme: $theme \n\nType of Abstract: $type \n\nUpload: $sasquach \n\n";
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;

}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Orchard Systems 2012 Symposium Register Here" />
<meta name="keywords" content="Orchard Systems, Fruit Growers" />
<title>Orchard Systems 2012 | Abstract Form</title>

<link rel="stylesheet" type="text/css" media="all" href="css/style.css" />
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" media="all" href="css/styleie7.css" />
<![endif]-->

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> 
<script src="jquery.validate.pack.js" type="text/javascript"></script> 

<script type="text/javascript"> 
$(document).ready(function(){

$('#abstractform').validate({

showErrors: function(errorMap, errorList) {
   //restore the normal look
   $('#abstractform div.xrequired').removeClass('xrequired').addClass('_required');
   //stop if everything is ok
   if (errorList.length == 0) return;
   //Iterate over the errors
   for(var i = 0;i < errorList.length; i++)
   $(errorList[i].element).parent().removeClass('_required').addClass('xrequired');
},

submitHandler: function(form) {             
    $('h1.success_').removeClass('success_').addClass('success_form');
    $("#content").empty();
    $("#content").append("<div id='sadhu'>This is just plain text. I need me a variable of somethink</div>");
    $('#abstractform').hide();
    form.submit();
}
});
});
</script> 

<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
</head>

<body class="abstract">

<div id="container">
    <div class="sidebar">
        <img src="images/orchardsystems2012.png" title="Orchard Systems 2012 Logo" />
        <div class="data"><p>
        10th International<br/>Symposium on<br/>Orchard Systems</p></div>
        <div class="location"><p>
        Stellenbosch<br/>South Africa<br/><span>3 - 6 December</span><br/>2012</p>
        </div><a><img class="button" src="images/button_interested.png" title="I am interested - keep me informed" /></a>
    <a href="registration.html" title="Click here to register"><img class="button" src="images/button_attend.png" title="I want to attend - registration form" /></a>
    <a href="abstract.html" title="Click here to submit an abstract" ><img class="button" src="images/button_abstract.png" title="I want to take part - submit an abstract" /></a>
        <img src="images/ishslogo.gif" style="margin:45px 63px 0px 63px;" />
    </div>

    <div id="intainer">

        <div id="menu">
            <ul>
                <li><a href="index.html" tabindex="i" title="Orchard Systems 2012 | Home" >Home</a></li>
                <li><a href="aboutus.html" tabindex="au" title="About Us" >About Us</a></li>
                <li><a href="programme.html" tabindex="p" title="Programme" >Programme</a></li>
                <li><a href="registration.html" tabindex="r" title="Registration Form" >Registration</a></li>
                <li><a href="venue.html" tabindex="v" title="Venue" >Venue</a></li>
                <li><a href="accommodation.html" tabindex="a" title="Accommodation" >Accommodation</a></li>
                <li><a href="tours.html" tabindex="t" title="Tours">Tours</a></li>
                <li><a href="contact.html" tabindex="c" title="Contact Form">Contact</a></li>
            </ul>
        </div>

        <div class="header">
        <h3 class="pagetitle">Submit an Abstract</h3>       
        </div>
        <?php if(isset($hasError)) { //If errors are found ?>
        <h1 class="error">Sorry, but you need fill in all the fields!</h1>
        <?php } ?>
        <h1 class="success_">Thank You!</h1>

        <div id="content">
        <p class="home_copy">You can also send your abstract via email to <a>reventer@netactive.co.za</a></p> 

        <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
        <div id="sadhu">
        <p class="general_site">First Name:<strong><?php echo $firstname;?></strong></p>
        <p class="general_site">Last Name:<strong><?php echo $lastname;?></strong></p>
        <p class="general_site">Email:<strong><?php echo $email;?></strong></p>
        <p class="general_site">Theme:<strong><?php echo $theme;?></strong></p>
        <p class="general_site">Type:<strong><?php echo $type;?></strong></p>
        <p class="general_site">Upload:<strong><?php print_r($_FILES['datafile']['name']);?></strong></p>
        </div>
        <?php } ?>

            <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="abstractform">
            <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="2100000" />
            <div class="_required"><p class="label_left">First Name</p><input type="text" name="firstname" id="firstname" value="" maxlength="100" placeholder="" class="required"  /></div><br/><br/>
            <div class="_required"><p class="label_left">Last Name</p><input type="text" name="lastname" id="lastname" value="" maxlength="100" placeholder="" class="required"  /></div><br/><br/>
            <div class="_required"><p class="label_left">E-mail address</p><input type="text" name="email" id="email" value="" maxlength="100" placeholder="" class="required email"  /></div><br/><br/>
            <div class="_required"><p class="label_left">Theme</p>
            <select name="theme" id="theme" class="required">
            <option value="" selected="selected">Select a theme</option>
            <option value="Environmental Physiology of Fruit Crops" >Environmental Physiology of Fruit Crops</option>
            <option value="Orchard and Plantation Systems" >Orchard and Plantation Systems</option>
            <option value="Rootstock, Breeding and Evaluation" >Rootstock, Breeding and Evaluation</option>
            </select></div>
            <br/>
            <div class="_required"><p class="label_left">Abstract Type</p>
            <select name="type" id="type" class="required">
            <option value="" selected="selected">Choose</option>
            <option value="Oral" >Oral</option>
            <option value="Poster" >Poster</option>
            </select></div>
            <br/>
            <div class="_required"><p class="label_left">Upload Abstract</p>
            <input class="required" id="datafile" type="file" name="datafile"></div>
            <input type="submit" value="submit" name="submit" id="submit" />
        </form>

        <p class="notice">Abstracts submission deadline: <span>15 June 2012</span></p>
        <p class="notice">Poster submission deadline:   <span>15 July 2012</span></p><br/>              
        </div>

    </div>


</div>
<div id="footer">
    <div class="footer_content">
        <div class="copyright"><a href="http://www.kreatif.co.za" target="_blank" title="website designed and developed by Kreatif Code.Design">© Orchard Systems 2012<br/>
Designed by kreatif.co.za</a></div>
    <span class="contactno">Tel +27 21 000 0000</span>
    <span class="emailus"><a href="mailto:info@orchardsystems2012.co.za?subject=Email enquiry from the website" title="Click here to email us">info@orchardsystems2012.co.za</a></span>
    </div>
</div>

</body>
</html>

目前我拥有它所以你可以看到文件名和文件类型即。 picture.jpg 但我想要http://www.bgv.co.za/testspace/upload/picture.jpg

请帮忙

1 个答案:

答案 0 :(得分:0)

您需要指向文件的完整网址。

更改以下行:

       $sasquach = "The file has been saved as: ".$newname;

       $sasquach = "The file has been saved as: ".$newname;
       $link= 'http://www.yoursite.com/'.$_SERVER['PATH_INFO'].'/upload/'.$filename;

$body = "First Name: $firstname \n\nLast Name: $lastname \n\nEmail: $email \n\nTheme: $theme \n\nType of Abstract: $type \n\nUpload: $sasquach \n\n";

要:

$body = "First Name: $firstname \n\nLast Name: $lastname \n\nEmail: $email \n\nTheme: $theme \n\nType of Abstract: $type \n\nUpload: $sasquach \n\nLink: $link";

您还可以检测网址,而不是使用$_SERVER['REQUEST_URI']对其进行硬编码并删除脚本名称。