This topic is locked
[SOLVED]

 Rename Another Uploaded File Too?

9/30/2019 5:53:39 PM
PHPRunner General questions
H
harveyspecter author

Hello i have that code to change an uploaded file name on display name and disk name. Its working really good.

//EVRAK

$fileArray2 = my_json_decode($values["Evrak"]);



// rename each file. In this example - convert to lowercase.

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

{

$fileArray2[$i]["usrName"] = "".$values["Davaci"]." ".$values["evrak_nedeni"].".pdf";

}



// update values of the field that stores file names

$values["Evrak"] = my_json_encode($fileArray2);
// get information about uploaded files

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



// rename files

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

{

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

$newFileName = "files/".$values["Davaci"]." ".$values["evrak_nedeni"].".pdf";

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

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

}



// update values of the field that stores file names

$values["Evrak"] = my_json_encode($fileArray);
return true;


But i have another upload field in that table. field name is "kargo_fisi". It has the same "View As/Edit As" settings and same sql options.

It also needs to change the uploaded files display and disk name but a little bit different. In needs to rename

$fileArray2[$i]["usrName"] = "".$values["Davaci"]." ".$values["istenme_tarihi"].".pdf";


If i add the same code above after "return true;" it displays nothing as uploaded. And dont rename it on disk just uploads the file with same name.
How can i make it done? Thanks alot for your help!

Sergey Kornilov admin 9/30/2019

First of all, your code is not optimal, you are going through the list of files twice. You can do it in one go renaming files and updating the display name at the same time. Less code - fewer chances to make an error.
Then copy the whole code snippet, paste it above return true;, replace "Evrak" with "kargo_fisi" and change the line of code that updates the display name. It should work.