1084

How does one get the new file name after upload?

Comments for “How does one get the new file name after upload?”
 

Posted by frocco on Monday 6th June 2022 at 20:05 GMT

    $config['new_file_name'] = true;
        $this->upload_file($config);

frocco

User Level: Guest

Date Joined: 7/10/2021

Posted by djnordeen on Tuesday 7th June 2022 at 14:38 GMT

Can you post your code for file upload.

Dan
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by frocco on Tuesday 7th June 2022 at 15:03 GMT

 $this->validation_helper->set_rules('myfile', 'file',  'required|validate_file|allowed_types[txt,csv,pdf]|max_size[20000]');
        $result = $this->validation_helper->run();

        if ($result == true) {
            // Upload file
            $config['destination'] = $this->upload_path;
            $config['new_file_name'] = true;
            $this->upload_file($config);


After the call to upload_file, how do you get the new_file_name?

This comment was edited by frocco on Tuesday 7th June 2022 at 15:04 GMT

frocco

User Level: Guest

Date Joined: 7/10/2021

Posted by mjim on Tuesday 7th June 2022 at 21:43 GMT

Hi Frocco

Assign a variable to the upload_file method and then use the json method to view what is returned.
Then you'll see how to view the new filename.

$myupload = $this->upload_file($config);
json($myupload, true);
Level One Member

mjim

User Level: Level One Member

Date Joined: 6/01/2022

Posted by frocco on Tuesday 7th June 2022 at 22:11 GMT

thanks for the suggestion, just tried it and it returns null

frocco

User Level: Guest

Date Joined: 7/10/2021

Posted by mjim on Tuesday 7th June 2022 at 22:25 GMT

Yes, I tested and you are right and I believe I ran into the same issue before.

In the upload_file method in Trongate.php it doesn't return anything.

So you could add return $new_file_name; but it will be overwritten later on future updates.

Perhaps try copying that method into your controller file and then add the return $new_file_name; at the end.

        } else {
            die('ERROR: Unable to find target file destination: '.$destination);
        }
        return $new_file_name;
Level One Member

mjim

User Level: Level One Member

Date Joined: 6/01/2022

Posted by frocco on Tuesday 7th June 2022 at 22:47 GMT

Correct, I ended up doing as you suggested and copied the upload function to my controller and returning the file name.
Problem solved, but I think Trongate.php needs to be enhanced.

frocco

User Level: Guest

Date Joined: 7/10/2021

Posted by Davcon on Friday 10th June 2022 at 17:45 GMT

When you upload a file, in PHP, you create a $_FILES superglobal.

You can view this by going to your submit endpoint and adding:

json($_FILES, true);


This will give you a summary of some key information to do with your uploaded file. For example,

{
    "picture": {
        "name": "app_store.png",
        "full_path": "app_store.png",
        "type": "image\/png",
        "tmp_name": "\/Applications\/XAMPP\/xamppfiles\/temp\/phpoC4T0B",
        "error": 0,
        "size": 206314
    }
}


So, the code for getting the file name is:

$filename = $_FILES['picture']['name'];


WEAKNESSES & POSSIBLE IMPROVEMENTS

I think there are a couple of improvements that could be made with the system we have. Specifically,

* Clean the filename to remove potentially problematic characters like spaces.

* Return the filename after the upload has happened.


We have a wave of framework updates coming very soon (all non-breaking changes). I'll try my best to add those tweaks shortly.

This comment was edited by Davcon on Friday 10th June 2022 at 17:46 GMT

Founding Member

Davcon

User Level: Founding Member

Date Joined: 3/11/2018

Posted by frocco on Saturday 11th June 2022 at 21:06 GMT

Thanks Dave, looking forward to them.

frocco

User Level: Guest

Date Joined: 7/10/2021

Posted by TestBuyer on Sunday 12th June 2022 at 23:37 GMT

Cheers dude.

TestBuyer

User Level: Guest

Date Joined: 11/06/2022

×