1. Fabrik 3.8.1 has been released. It is mostly bug fixes and feature enhancements, but does include two new plugins (push notifications, and the sequence element). As usual we strongly recommend testing the new release on a sandbox if your application is mission critical, and always do an Akeeba backup before updating.

Custom File/Foler Name

Discussion in 'Standard Support' started by joshblevins, Jun 2, 2018.

  1. joshblevins

    joshblevins Member

    Level: Standard
    I am attempting to make a custom folder and file name for a file upload.

    I would like to accomplish this type of file name.

    'EMPLOYEE ACTIVE/'.$last_int.'/'.$last_name.' '.$first_name.'/{thistable___state} {thistable___certification_type} {thistable___expiration}' ;

    The code I have tried in the rename file name is below.. However my understanding that is for the file name and not the location of the file.

    $last_name= '{employees___last_name}';
    $first_name= '{employees___first_name}';
    $last_int = $last_name[0];
    $last_int = strtoupper($last_int);
    $last_name= strtoupper($last_name);
    $first_name= strtoupper($first_name);
    $path= 'EMPLOYEE ACTIVE/'.$last_int.'/'.$last_name.' '.$first_name.'/{thistable___state} {thistable___certification_type} {thistable___expiration}' ;
    // use Joomla file utils
    jimport( 'joomla.filesystem.file' );
    // get the original file and split it into parts
    $old = ($formModel->formData['file']);
    $oldParts = pathinfo($old);
    // in this example we're using the id as the new filename
    $sid = $formModel->formData['id'];
    //create the new file (path and name)
    $new = $oldParts['dirname'].'/'.$sid.'.'.$oldParts['extension'];
    // update the file itself
    JFile::move(JPATH_SITE.$old, JPATH_SITE.$new);
    // update the data
    $formModel->updateFormData('file', $new, true);
    // update the db
    $object = new stdClass();
    // Must be a valid primary key value.
    $object->id = $sid;
    // new path + name
    $object->file = $new;
    // Update the data in the db
    $result = JFactory::getDbo()->updateObject('ep_submission', $object, 'id');
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    Nope, that won't work. As the tooltip on the rename option says, that code can only change the filename, not the path, and should just return the name. Also, it runs before we've moved the file from the web server's temporary upload folder to the element's configured folder, so if you try and move it, our code is then going to fail. Also, as it runs before the form data is written to the database, your updateObject() will fail as there's no object to update yet.

    Currently there's really no way to do this from the file element itself.

    You might be able to do it in a PHP form submission plugin, running onAfterProcess, which checks $_FILES[] (or $app->input-files) to see if something was uploaded, then do something similar to what your code is doing - move it, then update the row in the database. But i can't really help much with that on a standard sub, that's custom coding work.

    -- hugh
     

Share This Page