This topic is locked

rename upload image and filename

8/31/2019 4:20:50 PM
PHPRunner Tips and Tricks
A
acpan author

I need to upload a logo image and rename it to a specific filename "logo.png".
It is very simple, but it always take me some time to do that properly and the online help does not show clearly how to process the table field that stores the image data in JSON format.
So i would just post here to remind myself and also it may be helpful to others. It would be better if some of this process can be done by the next PHPR release or add more details to the online help.
The steps only cater for update of record (Not for adding new record) as the default data is populated by code when user visit the page.
Steps:

  1. Go to Designer page and select the field that stores the logo file's JSON formated text, in my case: defaultLogo (text field type).
    Set it accordingly as follows:
    a. view as 'File', Check on "Show file name" and "Show thumbnail".
    b. edit as 'file/image':

    (i) Set upload folder with php expression: $folder = "images"; $file["name"] = "logo.png";

    (ii) File settings: max 1 file

    (iii) Advanced Settings: create thumbnail on the fly and allow "png" type only without quote.
    (Note: i prefer to store the logo in images folder, instead of default files folder)
  2. Before Record Update event:


// Ensure only when a logo is uploaded, then we change the file name and rename physical image file uploaded.
if (strlen($values["defaultLogo"]) > 0 )

{
// Example of data stored in defaultLogo field:

/*

[{"name":"images\/logo_lesqftiw.png","usrName":"logo.png","size":29968,"type":"image\/png","thumbnail":"images\/thlogo_b4z7g738.png",

"thumbnail_type":"image\/png","thumbnail_size":29968,"searchStr":"logo.png,!:sStrEnd"}]

// where: name, thumbnail will be changed to logo.png and their corresponding physical files.

*/
// To start, we decode the JSON formated data for the uploaded image into Array format

// so that we can manipulate the data before updating to the DB and change the physical filename later.
// We restrict to one image file for logo, but still use $fileArray[0],

// if $fileArray without [0], the array will be overwritten without the index 0,

// hence corrupt the JSON format which caters for multiple files upload
$fileArray = json_decode($values["defaultLogo"],true);



$old_name = $fileArray[0]['name'];

$_SESSION["old_name"] = $old_name;

$new_name = "images/logo.png";

$_SESSION["new_name"] = $new_name;

$fileArray[0]['name'] = $new_name;

$fileArray[0]['usrName'] = "logo.png";



// do the same for thumbnail

$old_thumbnail = $fileArray[0]['thumbnail'];

$_SESSION["old_thumbnail"] = $old_thumbnail;

$new_thumbnail = "images/tnlogo.png";

$_SESSION["new_thumbnail"] = $new_thumbnail;

$fileArray[0]['thumbnail'] = $new_thumbnail;
// Write back to JSON fields to be updated to DB

$values["defaultLogo"] = json_encode($fileArray,true);

}
return true;


3. After Record Updated Event:


// Ensure only when logo is uploaded, then we delete and rename image file uploaded.

if (strlen($values["defaultLogo"]) > 0 )

{
// Delete previous logo.png and its thumbnail physical files in image folder



$files_to_delete = [

"'".$_SESSION["new_name"]."'",

"'".$_SESSION["new_thumbnail"]."'" ];

// delete files first

foreach ($files_to_delete as $file)

{

if (file_exists($file))

{

unlink($file);

}

else

{

$message = "$file not found.
";

}

}
// Start: rename physical thumnbnail file
$old_thumbnail = $_SESSION["old_thumbnail"];

$new_thumbnail = $_SESSION["new_thumbnail"];



if(file_exists($new_thumbnail))

{

$message = "Error While Renaming $old_thumbnail.
" ;

}

else

{

if ( !(file_exists($old_thumbnail)) )

{

$message = "$old_thumbnail deleted or not found.
" ;

$values["defaultLogo"] ="";

}

else

{

if(rename( $old_thumbnail, $new_thumbnail))

{

$message = "Successfully Renamed $old_thumbnail to $new_thumbnail.
" ;

}

else

{

$message = "$old_thumbnail => $new_thumbnail: A File With The Same Name Already Exists.
" ;

}

}

}

// End: rename physical thumbnail file
// Start: rename physical logo file
$old_name = $_SESSION["old_name"];

$new_name = $_SESSION["new_name"];
if(file_exists($new_name))

{

$message = "Error While Renaming $old_name.
" ;

}

else

{

if ( !(file_exists($old_name)) )

{

$message = "$old_name deleted or not found.
" ;

$values["defaultLogo"] ="";

}

else

{

if(rename( $old_name, $new_name))

{

$message = "Successfully Renamed $old_name to $new_name.
" ;

}

else

{

$message = "$old_name => $new_name: A File With The Same Name Already Exists.
" ;

}

}

}

// End: rename physical logo file
} // END: if (strlen($values["defaultLogo"]) > 0 )


3. That's it. You can now upload any image file in png format and name it "logo.png" by the script.

S
silvio 9/2/2019

Really nice tip

A
acpan author 10/10/2019

Update:
I am glad that the online help for PHPRunner version 10.3 has been updated with example to rename file after upload. Here's the link:
Rename File Example
Sergey and Team are quick to respond to help users.
ACP

admin 10/11/2019

ACP,
thank you for the article and for credit but this article was there for ages.

A
acpan author 10/11/2019

Hi,
Thanks, i think i might have missed that previously. It is hell lots of good info in there to learn each time:)
ACP



ACP,
thank you for the article and for credit but this article was there for ages.