This topic is locked
[SOLVED]

 Rename Uploaded Images

6/24/2013 5:02:30 AM
PHPRunner General questions
petert455 author

Hi PHPRunner Team and forum members :-)
I have a menu item called "options" that is only available as a list view and edit record. There are two upload image fields on this form - only allowing 1 image each. 1 is called "wallp" and the other "logo" I would like the user to be able to upload any image xxxxx.png to field "logo" and have it renamed by code to logo.png (physical file name) every time and to over-write as well.
The same for "wallp" and have it renamed by code to wallpaper.jpg
In trial I tried, My link
By making read-only fields called "logoname" and "wallname" and added the vales "logo" and "wallpaper" to those fields with phpmyadmin. Seems like a clumsy way to go about it and I was not able to combine the two blocks of code in the same event. When I used the blocks of code 1 at a time (in event edit page before update) I would get logo0.png
// get information about uploaded files LOGO

$fileArray = my_json_decode($values["logo"]);
// rename files
for($i = 0; $i < count($fileArray); $i++)
{
$fileName = $fileArray[$i]["name"];
$newFileName = "zreportfiles/art/".$values["logoname"].$i.".png";
rename($fileName, getabspath($newFileName));
$fileArray[$i]["name"] = $newFileName;
}
$values["logo"] = my_json_encode($fileArray);
return true;
$fileArray = my_json_decode($values["wallp"]);
for($i = 0; $i < count($fileArray); $i++)
{
$fileName = $fileArray[$i]["name"];
$newFileName = "zreportfiles/art/".$values["wallname"].$i.".jpg";
rename($fileName, getabspath($newFileName));
$fileArray[$i]["name"] = $newFileName;
}
// update values of the field that stores file names

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

return true;
Not sure id this is the best way to go about it. Would prefer to just rename the file on upload
Thanks for any help or suggestions

C
cgphp 6/24/2013

You are exiting the function before the code execution has been completed. Thre are two return true; in the code. You have to remove the first one. Try the following code:

// get information about uploaded files LOGO

$fileArray = my_json_decode($values["logo"]);
// rename files
for($i = 0; $i < count($fileArray); $i++)

{

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

$newFileName = "zreportfiles/art/".$values["logoname"].$i.".png";

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

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

}
$values["logo"] = my_json_encode($fileArray);
$fileArray = my_json_decode($values["wallp"]);
for($i = 0; $i < count($fileArray); $i++)

{

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

$newFileName = "zreportfiles/art/".$values["wallname"].$i.".jpg";

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

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

}
// update values of the field that stores file names

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

return true;
petert455 author 6/24/2013

Hi Cristian,
I had tried that before the post. With the first return true; removed, only the second block executes correctly. In the ./art/ folder I get logo's th file, wallp's th file and wallp0.jpg. No logo0.png.
Thanks again for the response

C
cgphp 6/25/2013

Make sure "logoname" is the correct name of the field.

petert455 author 6/25/2013

Yes was correct I tried each block separately and it works as expected. Sorry my coding skills just are not up to a Cristian level :-)
I wanted the same concept as in this forum's member profile settings. The forum member uploads an image as his or her avatar. In my case the user of the app would upload an image(s) to customize the app to his/her needs. Once the file(s) is uploaded and renamed (always to a static name) it was previously hard coded in the html to display.
As a clumsy work-around (and interesting trivia PHPR v6.2) in the generated app folder "classes" the file "uploadhandler.php" at the section "// runnerfix" I changed .generatePassword(8) to (0) and can just tell the client to upload pre-named file(s) in this case logo.png and wallp.jpg. I know the ".generatePassword(8)" is there to prevent file collisions on a global scope. But just had to move on ;-)
Thanks for your responses