This topic is locked

Rename uploaded files using uniqid() - unique identifier based on the current time in microseconds.

9/5/2020 12:49:09 AM
PHPRunner Tips and Tricks
Myr0n author

Each file uploaded has 3 names: display name, that you can see on the site, thumbnail and physical file name on the disk.
Use the following code in Add page: Before record added and/or Edit page: Before record updated events.
I documented it so that you can understand the code by yourself.
REMARKS:

You can use any file name extension

The file name can contain dots.

This code snippet use a folder name called "files" to save the uploaded files, *you should change it to fit your needs.

The field name that is using this code snippet is"FIELD_NAME",**
you should change it to fit your needs.*



// get information about uploaded files

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

$newSearchStr = "";

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

{

//separating file names and extensions.

$filenameArray = explode('.', preg_replace("/(\.)(?=\S+\.)/", "_", $fileArray[$i]["usrName"]));

//generating the name to use as thumbnail, display and physical name using uniqid()

$filenameArray[0] = uniqid(); //Gets a prefixed unique identifier based on the current time in microseconds.

//getting original file names

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

$thumbnailFileName = $fileArray[$i]["thumbnail"];

//updating the new name

$newThumbnailFileName = "files/th-".$filenameArray[0].".".$filenameArray[1];

$newPhysicalFileName = "files/".$filenameArray[0].".".$filenameArray[1];

// renaming physical files on the disk

rename($physicalFileName, getabspath($newPhysicalFileName));

rename($thumbnailFileName, getabspath($newThumbnailFileName));

//updating the new names

$fileArray[$i]["usrName"] = $filenameArray[0].".".$filenameArray[1]; //display name

$fileArray[$i]["name"] = $newPhysicalFileName; //physical name

$fileArray[$i]["thumbnail"] = $newThumbnailFileName; //thumbnail physical name

//

if($i == 0)

{

$newSearchStr = $fileArray[$i]["usrName"].",!";

}else

{

$newSearchStr = $newSearchStr . $fileArray[$i]["usrName"] . ",";

}

}

$newSearchStr = $newSearchStr . "!:sStrEnd";

$fileArray[0]["searchStr"] = $newSearchStr;

// updating values of the fields that stores display and physical file names

$values["FIELD_NAME"] = my_json_encode($fileArray);
S
skbrasil 9/9/2020

I tested the current code to rename the images but it doesn't work in the current version of phprunner.
It would be great to have a rewrite of the image or by default when uploading the image it will receive a universal identification uuid ()
this would be great.
Another option to add in the next phprunner updates the option to rename images and files, if the project owner ticks the option to rename the same image during any operation, be it edit or update it, it is renamed with an uuid ()
Expected result:

Original

phprunner-10.jpg

OK

be68e4febd344f2a77fdb229f0bbcb26-700.jpg