This topic is locked

Rename File Upload Without Change Ext

1/22/2020 11:00:28 AM
PHPRunner General questions
K
kathlin author

phprunner docs Rename uploaded files



// get information about uploaded files

$fileArray = my_json_decode($values["fieldname"]);



// rename files

for($i = 0; $i < count($fileArray); $i++)

{

$fileName = $fileArray[$i]["name"];

$newFileName = "files/".$values["LastName"].$i.".jpg";

rename($fileName, getabspath($newFileName));

$fileArray[$i]["name"] = $newFileName;

}



// update values of the field that stores file names

$values["fieldname"] = my_json_encode($fileArray);


this script all file rename with ext JPG,

i need rename file without ext
thanks

A
acpan 1/22/2020

Something like that in the for loop:
// get the ext of the $fileName

$ext = substr($fileName,-3);

$newFileName = "files/".$values["LastName"].$i.".$ext";
For more info on php substr function, check this:
https://www.w3schools.com/php/func_string_substr.asp
ACP