This topic is locked
[SOLVED]

 Multiple attachments from one field

2/21/2020 2:43:46 PM
PHPRunner General questions
abidcastaneda author

Hi Sergey,
I have a table in my project called pdf that has a varchar(2000) field so I can upload files, I want to send an e - mail from my application that has those files included as an attachment or include links to those files so they can be downloaded.
Problem 1: I have used:
$attachments = array

(
array('path' => getabspath('files/'.$values['pdf'].'')
)
But I get an error that the file is not accessbile.
I have changed the "getbspath" to the actual url of the file folder:
array('path' => getabspath('https://mydomain.com/files/'.$values['pdf'].'';)
But I still get the same thing,
Now, there are multiple files saved on the same field, so the result of $value['pdf'] is:

'[{"name":"files\/SMM190614KA9_Factura_A13_20200106_0dh45m0m.pdf","usrName":"SMM190614KA9_Factura_A13_20200106.pdf","size":18830,"type":"application\/pdf","searchStr":"SMM190614KA9_Factura_A13_20200106.pdf,!SMM190614KA9_Factura_A11_20200106.pdf,!SMM190614KA9_Factura_A12_20200106.pdf,!:sStrEnd"},{"name":"files\/SMM190614KA9_Factura_A11_20200106_x40w92lv.pdf","usrName":"SMM190614KA9_Factura_A11_20200106.pdf","size":18796,"type":"application\/pdf"},{"name":"files\/SMM190614KA9_Factura_A12_20200106_jq1kxy5t.pdf","usrName":"SMM190614KA9_Factura_A12_20200106.pdf","size":18732,"type":"application\/pdf"}]'
So, I'm guessing that the application is trying to retrieve something like this:
https://mydomain.com/files/'[{"name":"files\/SMM190614KA9_Factura_A13_20200106_0dh45m0m.pdf","usrName":"SMM190614KA9_Factura_A13_20200106.pdf","size":18830,"type":"application\/pdf","searchStr":"SMM190614KA9_Factura_A13_20200106.pdf,!SMM190614KA9_Factura_A11_20200106.pdf,!SMM190614KA9_Factura_A12_20200106.pdf,!:sStrEnd"},{"name":"files\/SMM190614KA9_Factura_A11_20200106_x40w92lv.pdf","usrName":"SMM190614KA9_Factura_A11_20200106.pdf","size":18796,"type":"application\/pdf"},{"name":"files\/SMM190614KA9_Factura_A12_20200106_jq1kxy5t.pdf","usrName":"SMM190614KA9_Factura_A12_20200106.pdf","size":18732,"type":"application\/pdf"}]';
In which case the file will not exist.
I would like to send all the files in that field as an attachment, or, create a link for each one of those files so the user can download them via e - mail.
If I go to https://mydomain.com/files/SMM190614KA9_Factura_A13_20200106_0dh45m0m.pdf, I can actually download the file, which I know now that SMM190614KA9_Factura_A13_20200106_0dh45m0m.pdf is the file name as it's actually stored in the application's file folder, but how can I get those names from all the files that are stored in that particular field?

abidcastaneda author 2/21/2020

I have also set the pdf field as a longblob to store the files in the database, but this only alows me to store 1 file per field, and I do have to store anywhere from 1 to 6 files in the same field, if not, I would have to set up 6 fileds to store files and most of the time I would upload 1 - 3 files and the rest of the fields would be blank, which, kind of confuses my users.
Oh, and I have already looked through the forum, but couldn't find anything to do this.
Thanks in advanced.

W
WilliamBDevClub member 2/21/2020

Look at this. It may help you access the files.
https://xlinesoft.com/phprunner/docs/rename_uploaded_file.htm

abidcastaneda author 2/27/2020

Thanks, I have gone through the link that you posted, but apparently I don't know how to use it, I've added this on the after record updated event and on the javascript onload event:
$fileArray = my_json_decode($values["pdf"]);

$values["pdfiles"] = my_json_encode($fileArray);
But nothing happens.
I've also added this on the after record updated event:
$fileArray = my_json_decode($values["pdf"]);
$data = array();

$keyvalues = array();

$data["pdfiles"] = $fileArray;

$keyvalues["id"] = $values["id"];

DB::Update("facturas", $data, $keyvalues );
And nothing, my DB doesn't get updated with the file names.
P.S. pdf = field where the files get uploaded (varchar2000)

pdfiles = field where the file names are supposed to be

W
WilliamBDevClub member 2/27/2020



Thanks, I have gone through the link that you posted, but apparently I don't know how to use it, I've added this on the after record updated event and on the javascript onload event:
$fileArray = my_json_decode($values["pdf"]);

$values["pdfiles"] = my_json_encode($fileArray);
But nothing happens.
I've also added this on the after record updated event:
$fileArray = my_json_decode($values["pdf"]);
$data = array();

$keyvalues = array();

$data["pdfiles"] = $fileArray;

$keyvalues["id"] = $values["id"];

DB::Update("facturas", $data, $keyvalues );
And nothing, my DB doesn't get updated with the file names.
P.S. pdf = field where the files get uploaded (varchar2000)

pdfiles = field where the file names are supposed to be


You will need to loop through the values stored in that field ($values["pdf"]). I don't see you doing that in your code. There are examples in the link to access the files. I also don't see anything there for you to email as you stated in your original post. It is kind of confusing.
Here is a different link that shows how to access the files, attach and email.

https://xlinesoft.com/phprunner/docs/send_an_email_with_attachment.htm

abidcastaneda author 2/28/2020

Awesome, thanks, you where right, I was not looping through the values, here's the code I finally used:
$record = $button->getCurrentRecord();

$result["pdf"]=$record["pdf"];

$result["factura"]=$record["factura"];

$fileArray = my_json_decode($result["pdf"]);

$attachments = array();

foreach($fileArray as $f)

{

$attachments[] = array("path" => $f["name"]);

}
//email

$email_msg = "";

$email_msg.= "";

$email_msg.= " Good afternoon,\n";

$email_msg.= "\n";

$email_msg.= " Attached please find the required documents for the importation of --------.\n";

$email_msg.= "\n";

$email_msg.= " Attached are:\n";

$email_msg.= " a) Bill of Ladings.\n";

$email_msg.= " <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=90445&image=1&table=forumreplies' class='bbc_emoticon' alt='B)' /> Invoices A11, A12 and A13.\n";

$email_msg.= " c) Certificate of Origin.\n";

$email_msg.= "\n";

$email_msg.= " As always, we appreciate your support.\n";

$email_msg.= "\n";

$email_msg.= " Best regards.\n";

$email_msg.= "\n";

$email_msg.= " RC..\n";
//send email

$email = "something@mail.com";

$subject = "Facturas: ".$result["factura"];

runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $email_msg, 'attachments' => $attachments));
Now I have to gather the attachments from several fiels, the fields I have with files are: invoices, bol, and certorg, I have to grab the files from those fields to send them as an attachment, I'll let you guys know if I figured out and how I did it, of course, any and all help is appreciated.

abidcastaneda author 2/28/2020

BLAMO!!!! I SOLVED IT!!!
I used array_merge:
$record = $button->getCurrentRecord();

$result["pdf"]=$record["pdf"];

$result["factura"]=$record["factura"];

$result["xml"]=$record["xml"];
$fileArray = my_json_decode($result["pdf"]);

$attachments = array();

foreach($fileArray as $f)

{

$attachments[] = array("path" => $f["name"]);

}
$fileArray2 = my_json_decode($result["xml"]);

$attachments2 = array();

foreach($fileArray2 as $f2)

{

$attachments2[] = array("path" => $f2["name"]);

}
//email

$email_msg = "";

$email_msg.= "";

$email_msg.= " Good afternoon,\n";

$email_msg.= "\n";

$email_msg.= " Attached please find the required documents for the importation of .\n";

$email_msg.= "\n";

$email_msg.= " Attached are:\n";

$email_msg.= " a) Bill of Ladings.\n";

$email_msg.= " <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=90446&image=1&table=forumreplies' class='bbc_emoticon' alt='B)' /> Invoices A11, A12 and A13.\n";

$email_msg.= " c) Certificate of Origin.\n";

$email_msg.= "\n";

$email_msg.= " As always, we appreciate your support.\n";

$email_msg.= "\n";

$email_msg.= " Best regards.\n";

$email_msg.= "\n";

$email_msg.= " RC.\n";
//send email

$email = "email@email.com";

$subject = "Facturas: ".$result["factura"];

runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $email_msg, 'attachments' => array_merge($attachments, $attachments2)));