file upload - Random name value

Hello all.I have been working on my file uploads to AWS.

Buckets are all set and everything is working however i now need to generate random file names when uplaoding.I have created the code in Row_inserting


// Row Inserting event
function Row_Inserting(?array $oldRow, array &$newRow): ?bool
{
    // Enter your code here
    // To cancel, set return value to false
    // To skip for grid insert/update, set return value to null
    $zz1 = CurrentDateTime();
    $zz2 = '.';
    $zz3 = rand(1111111111,9999999999);
    $zz4 = $zz1.$zz2.$zz3 ;
    	if ($newrow["contract"] != "") {
    		$newrow["contract"] = "$zz4" ;
    	}
    return true;
}

I am not getting errors but the file name is not changing.
any help here will be great.

Perhaps you might need this PHP mt_rand, instead of the old rand function.

Thank you for the update.

here is the code now

// Row Inserting event
function Row_Inserting(?array $oldRow, array &$newRow): ?bool
{
    // Enter your code here
    // To cancel, set return value to false
    // To skip for grid insert/update, set return value to null
    $zz1 = CurrentDateTime();
    $zz2 = '.';
    $zz3 = mt_rand(1111111111,9999999999);
    $zz4 = $zz1.$zz2.$zz3 ;
    	if ($newrow["contract"] != "") {
    		$newrow["contract"] = "$zz4" ;
    	}
    return true;
}

i am still not getting the name to change.

You should post the result or the output of your code so others are able to know what is the exactly going on in that event.Please note that I have just tested mt_rand() by creating a simple php file with the following code:

<?php

$my_random_code = mt_rand(1111111111, 9999999999);

echo $my_random_code;

?>

then each time I run the code from my web server, it will return the different value, as follows:Attempt 1 returned: 5905811817
Attempt 2 returned: 8088328633
Attempt 3 returned: 1737125304
… and so forth …

as well, you posted

    	if ($newrow["contract"] != "") {
    		$newrow["contract"] = "$zz4" ;

you’re checking for contract “is not empty”… is that correct? or should it be: if ($newrow[“contract”] == “”)
is contract in fact not blank/empty/null

Need some help here please.I have a table that has multiple different fields :

CREATE TABLE `comp_link` (
  `pk` int(11) NOT NULL,
  `comp_name` varchar(255) DEFAULT NULL,
  `email_add` varchar(255) DEFAULT NULL,
  `notes` longtext DEFAULT NULL,
  `mod_fica` int(1) DEFAULT 2,
  `mod_job` int(1) DEFAULT 2,
  `mod_note` int(1) DEFAULT 2,
  `mod_deadfile` int(1) DEFAULT 2,
  `mod_sars_calendar` int(1) DEFAULT NULL,
  `mod_x1` int(1) DEFAULT NULL,
  `mod_x2` int(1) DEFAULT NULL,
  `mod_x3` int(1) DEFAULT NULL,
  `mod_x4` int(1) DEFAULT NULL,
  `mod_x5` int(1) DEFAULT NULL,
  `auth_person` varchar(900) DEFAULT NULL,
  `auth_contact1` varchar(900) DEFAULT NULL,
  `auth_contact2` varchar(900) DEFAULT NULL,
  `bill_contact_name` varchar(900) DEFAULT NULL,
  `bill_contact_email` varchar(900) DEFAULT NULL,
  `bill_contact_number` varchar(900) DEFAULT NULL,
  `billing_add_1` varchar(900) DEFAULT NULL,
  `billing_add_2` varchar(900) DEFAULT NULL,
  `billing_add_3` varchar(900) DEFAULT NULL,
  `billing_add_4` varchar(900) DEFAULT NULL,
  `billing_add_5` varchar(900) DEFAULT NULL,
  `billing_add_6` varchar(900) DEFAULT NULL,
  `contract` varchar(255) DEFAULT NULL,
  `contract_expiry` date DEFAULT NULL,
  `bank_letter` varchar(45) DEFAULT NULL,
  `bill_type` int(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

i am trying to have files uploaded (with a changed name) into contract. I have got the code to work but the problem is when the file is not uploaded or other items change.Here is my code:

    public function rowUpdating(array $oldRow, array &$newRow): ?bool
    {
        // Enter your code here
        // To cancel, set return value to false
        // To skip for grid insert/update, set return value to null
		if ($newRow["contract"] != "") {
			}
		elseif ($newRow["contract"] == $oldRow["contract"]) {
			}
		elseif ($newRow["contract"] != $oldRow["contract"]) {
			$files = $newRow["contract"];
			$zz1 = CurrentDate();
			$zz2 = '.';
			$zz3 = mt_rand(1111111111,9999999999);
			$file_extension = substr(strtolower(strrchr($files, ".")), 1);
			$zz4 = $zz1.$zz2.$zz3.".".$file_extension ;
			$newRow["contract"] = "$zz4" ;
			}
        return true;
        }

In the debug i am getting the following:

Warning
C:\xampp\htdocs\backoffice1\models\CompLink.php(3044): Undefined array key "contract"

the line in question here is :

		if ($newRow["contract"] != "") {

Any assistance or nudge in the right direction would be really gratefull.

You should check if isset($newRow[“contract”]) first.