Upload multiple files, insert into database, send email with multiple attachments

WordPress Custom code to upload files and save data into the database after successful upload send all files as attachments in an email to user.

Do you want professional help? We’ll repair it for you! Get assist Contact here!


    $uploaddir = '/home/public_html/wp-content/uploads/order-doc-folder/';
    foreach($_FILES['docat'] as $key => $doc){
      if($_FILES['docat']['name'][$key] != ''){
            $temp = explode(".", $_FILES['docat']['name'][$key]);
            $newfilename = $_REQUEST['order_id'].'-'.round(microtime(true)) . '-'.$temp[0].'.' . end($temp);
            $uploadfile = $uploaddir . basename($newfilename);
            if( move_uploaded_file($_FILES['docat']['tmp_name'][$key], $uploadfile)){
                $filename[] = $newfilename;
                $filenamed[] = $uploadfile;
$wpdb->query('update submit_order set status="'.$_POST['status'].'" where id = '.$_REQUEST['order_id']);
$wpdb->query("insert into order_status_history set status='".$_POST['status']."', comment='".$_POST['comment']."', filelist = '".serialize($filename)."', product_id = ".$_REQUEST['order_id']);
    $subject = 'Your Order status update';
    $message = '
Dear Customer,
Your order no: '.$result->u_id.' current status is '.$_POST['status'].'
';         $senderName = "Talk For WEB";     $senderEmail = "info@talkforweb.com.au";       $from = $senderName." <".$senderEmail.">";     $headers = "From: $from";       // Boundary       $semi_rand = md5(time());       $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";         // Headers for attachment       $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";         // Multipart boundary       $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .     "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";         // Preparing attachment     if(!empty($filenamed)){         for($i=0;$iemail, $subject, $message, $headers, $returnpath);       $updateo = true;     // Return true, if email sent, otherwise return false     if($mail){         return true;     }else{         return false;     } 

Upload multiple files, insert into database, send email with multiple attachments

How to use MySQL in PHP 7 version

MySQL extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0 version. For a large and old application, this is difficult to search and replace each function.

We can use MySQL functions by creating a wrapper function for each below is running code.

function mysql_connect($host,$user,$pass){

function mysql_select_db($db){
     /* use global so that we can use this connection in other mysql functions */
     global $dbconn;
     if ($dbconn->connect_error) {
          // trigger your error message
function mysql_query($query){
     global $dbconn;
     $mysqli_row = mysqli_query($dbconn, $query);
     return $mysqli_row;

function mysql_insert_id(){
    global $dbconn;
    return mysqli_insert_id($dbconn);

function mysql_fetch_array($result){
    $row = mysqli_fetch_array($result, MYSQLI_BOTH);
    /* MYSQLI_BOTH will return both associative and numeric array */
    return $row;
function mysql_num_rows($result){
    $row_cnt = mysqli_num_rows($result);
    return $row_cnt;

function mysql_real_escape_string($val){
    global $dbconn;
    $rl = mysqli_real_escape_string($dbconn, $val);
    return $rl;