发送邮件并下载文件时单击下载按钮


Send mail and download the file when click on DOWNLOAD button ?

1) 当我点击下载按钮时,只有文件被下载...

2)当我添加" onClick="return sendMail('. $id .')"时 在标签和"返回假"在脚本中只发送邮件

         how can get both the facility on a single click ?         
         // DOWNLOAD LINK
    echo '<a href="'.$path.'" onClick="sendMail('. $id .')" class="links">Download</a>';
       //SCRIPT for sending mail
              <script>
               function sendMail(str)
                {alert(str);
              if (str=="")
              {
             document.getElementById("txtHint").innerHTML="";
                   return;
                  }
              if (window.XMLHttpRequest)
             {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
               }
            else
             {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
            xmlhttp.onreadystatechange=function()
               {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
               {
              document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                }
              }
          xmlhttp.open("POST","sendMail.php?q="+str,true);
           xmlhttp.send();
        //return false; 
             }
         </script>
       // sendMail.php
         <?php include_once("includes/connection.php");
          $q = ($_REQUEST['q']);

            $sqlid="SELECT * FROM files_table WHERE id = '".$q."'";
           $result = mysqli_query($conn,$sqlid);
           $row=mysqli_fetch_array($result);
          //collect data from table
          $id=$row['id'];
          $email =$row['user_email'];
          $message=$row['message'];
          $file_type=$row['file_type'];
           $download=$row['file_name'];
          $time=$row['time'];

          $to=$email;
      $from = "support@print.com"; // sender
           $subject = $id.'   '.$message;
          $message = "  
          'Dear Sir,
          Greetings from Printing! India's leading Printing company.
         Please let us know for any queries or concerns. We are here to serve you better.
           Assuring you of our best services at all times and looking forward to a long and pleasant association
Regards,
Team Printing";
      // message lines should not exceed 70 characters (PHP rule), so wrap it
          $message = wordwrap($message, 70);
        // send mail
        mail($to,$subject,$message,"From: $from'n");
         echo "Mail Sent"; 
         ?>

      //download.php
             <?php             
            //Set the time out to 0
           set_time_limit(0);
           //Path to the download file
           $Location_to_download_files = 'downloads/'.$_REQUEST['file_to_download'];
          function File_Downloads($file, $name, $mime_type='')
            {
              //Check premission for the file 
                if(!is_readable($file)) die('Sorry, the file could not found or its inaccessible.');
                 $size = filesize($file);
                $name = rawurldecode($name);
                /* Find out the MIME type. You can add any other required download file format of your choice or remove if necessary */
                 $known_mime_types = array(
                  //Programs Extensions
                 "html" => "text/html",
                 "htm" => "text/html",
                //Archives
                   "zip" => "application/zip",
              //Documents
            "pdf" => "application/pdf",
            "doc" => "application/msword",
            "docx" => "application/msword",
             "xls" => "application/vnd.ms-excel",
            "ppt" => "application/vnd.ms-powerpoint",
             "txt" => "text/plain",
              //Executables
             "exe" => "application/octet-stream",
              //Images
            "gif" => "image/gif",
           "png" => "image/png",
            "jpeg"=> "image/jpg",
           "jpg" =>  "image/jpg",
            "php" => "text/plain",
  //Audio
 "mp3" => "audio/mpeg",
 "wav" => "audio/x-wav",
 //Video
 "mpeg" => "video/mpeg",
 "mpg" => "video/mpeg",
 "mpe" => "video/mpeg",
 "mov" => "video/quicktime",
 "avi" => "video/x-msvideo"
    );
        if($mime_type == "")
          {
           $file_extension = strtolower(substr(strrchr($file,"."),1));
            if(array_key_exists($file_extension, $known_mime_types))
           {
           $mime_type=$known_mime_types[$file_extension];
          } 
            else 
            {
            $mime_type="application/force-download";
            };
             };
          //turn off output buffering to decrease cpu usage
          @ob_end_clean(); 
        // required for IE, otherwise Content-Disposition may be ignored
           if(ini_get("zlib.output_compression"))
          ini_set("zlib.output_compression", "Off");
        header("Content-Type: " . $mime_type);
           header("Content-Disposition: attachment; filename='".$name."'");
       header("Content-Transfer-Encoding: binary");
         header("Accept-Ranges: bytes");
      /* The three lines below basically make the 
            download non-cacheable */
            header("Cache-control: private");
             header("Pragma: private");
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
           // multipart-download and download resuming support
            if(isset($_SERVER['HTTP_RANGE']))
            {
         list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
          list($range) = explode(",",$range,2);
          list($range, $range_end) = explode("-", $range);
          $range=intval($range);
          if(!$range_end) {
        $range_end=$size-1;
        } else {
    $range_end=intval($range_end);
      }
          $new_length = $range_end-$range+1;
          header("HTTP/1.1 206 Partial Content");
          header("Content-Length: $new_length");
           header("Content-Range: bytes $range-$range_end/$size");
           } else {
        $new_length=$size;
        header("Content-Length: ".$size);
         }  
         /* Will output the file itself */
          $chunksize = 1*(1024*1024); //You can change this if you wish
         $bytes_send = 0;
            if ($file = fopen($file, 'r'))
         {
        if(isset($_SERVER['HTTP_RANGE']))
          fseek($file, $range);
      while(!feof($file) && (!connection_aborted()) && ($bytes_send<$new_length))
         {
    $buffer = fread($file, $chunksize);
    print($buffer);
    flush();
    $bytes_send += strlen($buffer);
       }
       fclose($file);
          } 
         else
        //If no permissiion
            die('Error - The file you attempted to download can not be openned at the moment. Please try again or contact the site admin if this problem persist.');
  //die
     die();
      }
           //Call the function to download with file path,file name and file type
     File_Downloads($Location_to_download_files, $_REQUEST['file_to_download'], 'text/plain');
?>

从Donwload link(href)中删除Onclick,

并将该函数调用到 PHP 函数 function File_Downloads($file, $name, $mime_type='') { //Your send mail function echo '<script> function sendMail(str) { //your content } </script>'; }