Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

File Object / PHP

Questions related to using nuBuilder Forte.
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

File Object / PHP

Unread post by toms »

Hi,

On a Form there is a file object:

ID: file_test
Input Type: File

Then I'd like to process a selected file with PHP code in the "After Save" event (to save it to the file system and also send it as attachment by email)

Unfortunately, I don't manage to get a reference to that file object so I can process it further.

First attempt:

Code: Select all

$F = "#file_test#";
nuDebug("F: ".$F);  //  <--- variable is empty
Second attempt:

Code: Select all

$F = nuHash()['file_test'];
nuDebug("F: ".$F);  //  <--- variable is empty
// The further processing would look like this (maybe not relevant for my question above)

Code: Select all

$J = json_decode($F);
    nuDebug("J".$J);

    $name     = $J[0]->{'name'};
    $type     = $J[0]->{'type'};
    $file     = $J[0]->{'file'};

    nuDebug("name: ".$name );
    nuDebug("type: ".$type );
    nuDebug("file: ".$file );
     
    $contents = file_get_contents(sys_get_temp_dir().$name);                    //obtain the contents of the file
    $contents = addslashes($contents);

    nuDebug("contents: ".$contents );

// and more to do...
$filelist = array();
array_push($filelist, ......);
nuSendEmail(....);

What's the correct way of doing it? I'm not looking for a complete solution just an idea to approach it.

If possible, I don't want to save the file data (as JSON) in a field nor in the db.
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: File Object // PHP

Unread post by admin »

toms,

You were right, there was a bug.

I have put the fix on Github.

let me know if it works.

You said...
If possible, I don't want to save the file data (as JSON) in a field nor in the db.
Just give the Input:File Object a name that doesn't match any field name in the current Form's table.


BTW

It would be nice if you could share with the rest of us, the PHP you come up with, to attach a file to an email and then send it.


Steven
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: File Object // PHP

Unread post by toms »

Steven,

Thanks to the fix I get now the file object's value. I got the file uploading and email sending work, it just needs some fine tuning before I share the code (or parts of it)
One thing I noted is that nuSendEmail() deletes my file by calling unlink(). While in some cases it makes sense to delete the source file, my file needs to be stored on the server.

How about deleting the file only if it is in a temporary folder?

function nuSendEmail():

// Existing code:

Code: Select all

foreach($filelist as $filename => $filesource) {@
    unlink($filesource);
}
// Suggestion: Delete only if file is in temp folder

Code: Select all

$tmp = sys_get_temp_dir();
foreach($filelist as $filename => $filesource) {
    if (strncmp($filesource, $tmp, strlen($tmp)) === 0) {
        unlink($filesource);
    }
}
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: File Object / PHP

Unread post by admin »

toms,

You said.
my file needs to be stored on the server.
Can I ask why and where this file comes from?

Steven
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: File Object / PHP

Unread post by toms »

Steven,

You'll find the details in the first post of this thread.
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: File Object / PHP

Unread post by admin »

toms,

I'm really sorry, but I'm don't understand.

Steven
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: File Object / PHP

Unread post by admin »

toms,

I guess my questions are...

Why does the file need to stay on the system.
Why can't it regenerate it everytime you send an email?

Steven
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: File Object / PHP

Unread post by toms »

The process goes like this:

- A user creates a new record and fills in the fields on the form
- Using the file object, a ms word file that is different every time and created be the user is selected
- Then the form is saved.
- A PHP script then uploads the file to the server and an email is sent with that attached word file.

The file needs to stay on the server because the user will not keep a local copy of it and sometimes the file will be retrieved again from the server.
To cut a long story short, nuSendEmail() must not delete the files on the server.
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: File Object / PHP

Unread post by admin »

toms,

Am I correct in saying that the file can be recreated from nuBuilder at any time?

Steven
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: File Object / PHP

Unread post by toms »

Not quite sure what you mean. The file is not stored in the db, just on the server. So nuBuilder can't just recreate them.
I thinks I should just create my own sendmail function that doesn't delete the uploaded files.
Locked