Tairoc
Tairoc

Reputation: 649

Warning: implode(): Invalid arguments passed

I have gone through a ton of this invalid arguments passed messages on this forum and I am very sorry but have not found any example that helps my situation.

As you can see from the code below with the generous help of Rasclatt, I have several field names including 9 files to be uploaded to the server while the rest of the fields get submitted to the database.

When I attempt to run the code, I get, "Warning: implode(): Invalid arguments passed in..." which is on line 94 - the start of the INSERT statement.

An important point to note is that not all files can be uploaded at one time during an insert.

Users can elect to upload all files, just as they can elect to just upload one file during an insert iteration.

Any idea how to resolve this?

<?php
   error_reporting(E_ERROR | E_WARNING | E_PARSE);

 include("../Connections/Connect.php");

// this function is used to sanitize code against sql injection attack.
function ms_escape_string($data) {
        if ( !isset($data) or empty($data) ) return '';
        if ( is_numeric($data) ) return $data;

        $non_displayables = array(
            '/%0[0-8bcef]/',            // url encoded 00-08, 11, 12, 14, 15
            '/%1[0-9a-f]/',             // url encoded 16-31
            '/[\x00-\x08]/',            // 00-08
            '/\x0b/',                   // 11
            '/\x0c/',                   // 12
            '/[\x0e-\x1f]/'             // 14-31
        );
        foreach ( $non_displayables as $regex )
            $data = preg_replace( $regex, '', $data );
        $data = str_replace("'", "''", $data );
        return $data;
    }

    // You may want to add document root
    $target = $_SERVER['DOCUMENT_ROOT']."/uploads";
    // I am filtering the files incase there are empty uploads
    // You need to have the proper file input name (item)
    $_FILES['item']['tmp_name'] =   array_filter($_FILES['item']['tmp_name']);
    $_FILES['item']['name'] =   array_filter($_FILES['item']['name']);
    $_FILES['item']['type'] =   array_filter($_FILES['item']['type']);
    $_FILES['item']['size'] =   array_filter($_FILES['item']['size']);


    foreach($_FILES['item']['name'] as $i => $value ) {
            $file_name              =   $_FILES['item']['name'][$i];
            $file_size              =   $_FILES['item']['size'][$i];
            $file_tmp               =   $_FILES['item']['tmp_name'][$i];
            $file_type              =   $_FILES['item']['type'][$i];

            $bidDate                =   ms_escape_string($_POST['txtBidDate']);
            $dueDate                =   ms_escape_string($_POST['txtDueDate']);
            $dueTime                =   ms_escape_string($_POST['txtDueTime']);
            $bidTitle               =   ms_escape_string($_POST['BidTitle']);
            $bidId                  =   ms_escape_string($_POST['BidID']);
            $desc                   =   ms_escape_string($_POST['Description']);
            $dept                   =   ms_escape_string($_POST['Department']);
            $bidContact             =   ms_escape_string($_POST['BidContact']);
            $contactEmail           =   ms_escape_string($_POST['ContactEmail']);
            $contactPhone           =   ms_escape_string($_POST['ContactPhone']);
            $numBids                =   ms_escape_string($_POST['NumofBids']);
            $awardDate              =   ms_escape_string($_POST['txtAwardDate']);
            $awardrecip1            =   ms_escape_string($_POST['AwardRecip']);
            $bidType                =   ms_escape_string($_POST['BidType']);
            $lastUpdate             =   ms_escape_string($_POST['txtLastUpdate']);
            $notes                  =   ms_escape_string($_POST['Notes']);
            $status                 =   ms_escape_string($_POST['Status']);

            $sqlArr['values'][$i]   =   "'".ms_escape_string($_FILES['item']['name'][$i])."'";
            $sqlArr['columns'][$i]  =   "Addend".$i;
            $sqlArr['columns']  =   "SignInSheet";
            $sqlArr['columns']  =   "TabSheet";
            $sqlArr['columns']  =   "BidFile";
            // At this point you are only notifying user.
            // You have no code to prevent this limitation.
            if ($file_type!="application/pdf" || $file_type!="image/gif" || $file_type!="image/jpeg")
                 $echo =    'You can only upload PDFs, JPEGs or GIF files.<br>';
            // So far, this is just for notification, you haven't
            // actually done anything about this limitation
            if($file_size >  (8 * 1024 * 1024))
                $echo='File size must be less than 8 MB';

            // Makes the folder if not already made.
            if(!is_dir($target))
                mkdir($target,0755,true);

            //Writes the files to the server
            if(move_uploaded_file($_FILES['item']['tmp_name'][$i], $target."/".$file_name)) {
                //If all is ok
                echo "The file ". $file_name. " has been uploaded to the directory and records saved to the database";
            }
            else {

            //Gives and error if its not
            echo "Sorry, there was a problem uploading your file.";
            }
        }
if(isset($sqlArr['columns'])) {
    $sql="INSERT INTO bids (BidDate,DueDate,DueTime,BidTitle,BidID,Description,,'".implode("','",$sqlArr['columns'])."',Department,Xcontract,ContactEmail,ContactPhone,NumofBids,AwardDate,AwardRecip1,BidType,LastUpdate,Notes,BidStatus)
              VALUES ('$bidDate', '$dueDate','$dueTime',$bidTitle','$bidId','$desc',".implode(",",$sqlArr['values']).", '$dept','$bidContact','$contactEmail','$contactPhone','$numBids','$awardDate','$awardrecip1','$bidType','$lastUpdate','$notes',$status')" ;
    $objQuery = sqlsrv_query($conn, $sql);
    sqlsrv_close($conn);
} ?>

Upvotes: 0

Views: 974

Answers (1)

user557846
user557846

Reputation:

one problem:

 $sqlArr['columns']  =   "SignInSheet";
 $sqlArr['columns']  =   "TabSheet";
 $sqlArr['columns']  =   "BidFile";

is overwriting the value, not creating an array, you want:

 $sqlArr['columns'][]  =   "SignInSheet";
 $sqlArr['columns'][] =   "TabSheet";
 $sqlArr['columns'][]  =   "BidFile";

Upvotes: 1

Related Questions