This topic is locked
[SOLVED]

 Save only the names of the photos in a File / Image type field

12/24/2020 5:22:27 AM
PHPRunner General questions
A
alfonso authorDevClub member

When multiple photos are selected in an Image / File type field, the information that is saved in the table field is very complex to be able to be used later outside of phprunner. Is it possible that I saved only the names of the photos separated by commas?

A
alfonso authorDevClub member 12/26/2020

For example, I save 2 photos and I get all this code

[{"name":"\/fotografias\/mas\/nameofphoto1_l0bcobz3.jpg","usrName":"nameofphoto1.jpg","size":10774,"type":"image\/jpeg","thumbnail":"\/fotografias\/mas\/thnameofphoto1_3pz4od6j.jpg","thumbnail_type":"image\/jpeg","thumbnail_size":10774,"searchStr":"nameofphoto1.jpg,!nameofphoto2.jpg,!:sStrEnd"},{"name":"\/fotografias\/mas\/nameofphoto2_a8ud6wkr.jpg","usrName":"nameofphoto2.jpg","size":20333,"type":"image\/jpeg","thumbnail":"\/fotografias\/mas\/thnameofphoto2_05d0xaqk.jpg","thumbnail_type":"image\/jpeg","thumbnail_size":13269}]
Really, what I need is just save the mane of the files: "nameofphoto1.jpg,nameofphoto2.jpg"

If it is not possible, can anybody help me with a php function to extract name of files in this field?

aadham 12/26/2020

Hi Alfonso
I hope my code would help you achieve what you want.

In Before Record Added/Updated Event:



// get uploaded files info

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



// rename files

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

{

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

$name = pathinfo($fileName, PATHINFO_FILENAME);

$clean = substr_replace($name, '', -9);

$ext = pathinfo($fileName, PATHINFO_EXTENSION);

$newFileName = "files/".$clean.".".$ext;

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

$fileArray[$i]["usrName"] = $clean.".".$ext;

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



// update values of the field that stores file names

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

}
A
alfonso authorDevClub member 12/26/2020

Thanks. I try to use the code and I also try to save in other field. What I get is an exact copy of the content
This code:

[{"name":"\/fotografias\/mas\/nameofphoto1_l0bcobz3.jpg","usrName":"nameofphoto1.jpg","size":10774,"type":"image\/jpeg","thumbnail":"\/fotografias\/mas\/thnameofphoto1_3pz4od6j.jpg","thumbnail_type":"image\/jpeg","thumbnail_size":10774,"searchStr":"nameofphoto1.jpg,!nameofphoto2.jpg,!:sStrEnd"},{"name":"\/fotografias\/mas\/nameofphoto2_a8ud6wkr.jpg","usrName":"nameofphoto2.jpg","size":20333,"type":"image\/jpeg","thumbnail":"\/fotografias\/mas\/thnameofphoto2_05d0xaqk.jpg","thumbnail_type":"image\/jpeg","thumbnail_size":13269}]
The result is the same code, not only names of images

aadham 12/26/2020



Thanks. I try to use the code and I also try to save in other field. What I get is an exact copy of the content
This code:

[{"name":"\/fotografias\/mas\/nameofphoto1_l0bcobz3.jpg","usrName":"nameofphoto1.jpg","size":10774,"type":"image\/jpeg","thumbnail":"\/fotografias\/mas\/thnameofphoto1_3pz4od6j.jpg","thumbnail_type":"image\/jpeg","thumbnail_size":10774,"searchStr":"nameofphoto1.jpg,!nameofphoto2.jpg,!:sStrEnd"},{"name":"\/fotografias\/mas\/nameofphoto2_a8ud6wkr.jpg","usrName":"nameofphoto2.jpg","size":20333,"type":"image\/jpeg","thumbnail":"\/fotografias\/mas\/thnameofphoto2_05d0xaqk.jpg","thumbnail_type":"image\/jpeg","thumbnail_size":13269}]
The result is the same code, not only names of images


Have you changed the filed name from 'My File' to the actual field name?

A
alfonso authorDevClub member 12/26/2020

Yes. I think you use this code to rename files. What I need is to save in the same field or in other field ONLY the name of the images (comma separated): nameofphoto1.jpg,nameofphoto2.jpg

aadham 12/26/2020



Yes. I think you use this code to rename files. What I need is to save in the same field or in other field ONLY the name of the images (comma separated): nameofphoto1.jpg,nameofphoto2.jpg



My apologies, I believe I have misinterpreted your question.

Do you want to save multiple images in one field but you don't want them to be stored in JSON format?

A
alfonso authorDevClub member 12/26/2020

Exact. What I want is to leave the gallery_image field with the json format, but I want in the Before Record Added/Updated Event to save in another field called gallery_image2, only the names of the images separated by commas

A
alfonso authorDevClub member 12/26/2020

I've got it:
$fileArray = my_json_decode($values["gallery_image"]);
// rename files

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

{

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

$name = pathinfo($fileName, PATHINFO_FILENAME);

$clean = substr_replace($name, '', -9);

$ext = pathinfo($fileName, PATHINFO_EXTENSION);

$newFileName = $clean.".".$ext;

$fileArray[$i]["name"] = $newFileName;
$values["service_4_image"] = $values["service_4_image"].",".$newFileName;

$fotos = $values["service_4_image"];

$values["fotos_carta"] = substr($fotos,1);

}