Please enable JavaScript to view this site.

Navigation: Advanced topics > Events > Sample events > Upload

PHPrunner32x32     PHPRunner manual


Rename uploaded files

Scroll Prev Next More

 

Each file uploaded to the disk has two names: display name, that you can see on the site, and physical file name on the disk.

 

Let's study how to change both display and physical file names. Use the following code in Add page: Before record added and/or Edit page: Before record updated events.

 

Note: Change red values to match your project.

 

1. Change the display name of the uploaded file

 

// get information about uploaded files
$fileArray = my_json_decode($values["fieldname"]);
 
// rename each file. In this example - convert to lowercase.
for($i = 0; $i < count($fileArray); $i++)
{
  $fileArray[$i]["usrName"] = strtolower($fileArray[$i]["usrName"]);
}
 
// update values of the field that stores file names
$values["fieldname"] = my_json_encode($fileArray);

 

Note: In this example, fieldname stores the names of uploaded files.

 

2. Change the physical name of the uploaded file

 

This code snippet changes the file names to the LastName field values.

 

// 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);

 

Note: In this example, fieldname stores the names of uploaded files.

See also:

Export/Import pages

"View as" settings: File

"Edit as" settings: File/Image

 

Created with Help+Manual 7 and styled with Premium Pack Version 3 © by EC Software