Custom Fields
Re-Envisioned
Support General Change Destination Of Custom File Upload Field Type

This topic contains 12 replies, has 4 voices, and was last updated by  Anh Tran 3 months, 2 weeks ago.

  • Creator
    Topic
  • #11274
    Resolved
    Lucjan Zaborowski
    Participant

    Hello Folks

    I have a bit different requirement. We would like to upload files on specific destination like in my root (/downloads) . Currently its uploading on my uploads directory. I want to upload in different folder.

    Full Scenario:

    We are uploading two files(PDF) for all posts. 1 for public download and 1 for gated like people will fill the form and would be able to download.

    so While adding a post we have two file type options for upload the files. now we want to upload the file on ROOT_PATH/downloads and ROOT_PATH/downloads/gated

    Please help me with this. Thanks in advance.

Viewing 10 replies - 1 through 10 (of 12 total)
  • Author
    Replies
  • #11299

    Lucjan Zaborowski
    Participant

    Any update On the same

    #11304

    Anh Tran
    Keymaster

    Hi Lucjan,

    Sorry for the delay. This feature was asked before in the forum and we haven’t done it yet. We’ll let you know when it’s ready.

    Thanks,
    Anh

    #13143

    Lucjan Zaborowski
    Participant

    Any update on this..

    #13156

    Anh Tran
    Keymaster

    Hi Lucjan,

    We’ve just finished the feature and the code is available on Github. Please download and try it.

    In a couple of days, I’ll release a new version of Meta Box and write a tutorial about that.

    To upload to custom folder, please use file field and set a new attribute upload_dir to the path of the new folder, like this:

    array(
        'type' => 'file',
        'id' => 'f',
        'name' => 'File',
        'upload_dir' => '/path/to/your/folder/', // You can use ABSPATH . '/downloads/'
    )
    #13166

    david.h
    Participant

    Hi Anh,

    Could this be used to restrict users to accessing their own upload directory/files in the Media Library, an often requested feature in many forums.

    The concept would appear to integrate nicely with all the Metabox upload fields, for example:

    https://docs.metabox.io/fields/image-upload/

    where the array attribute ‘upload_dir’ uses/creates a “home” folder which could be a user’s wordpress ID or an secure, unique algorithmic hash.

    Thanks,
    David

    #13171

    Anh Tran
    Keymaster

    Hi David,

    There is a plugin that help you to do that.

    Or your can use a snippet.

    #13173

    david.h
    Participant

    Hi Anh,

    Thanks for the pointers, I have come across those before, although I’m interested in logical and structural separation.

    Would this pseudocode work, and could Metabox handle being returned an error?

    
    array(
        'upload_dir' => fnCreateOrReturnFolder
    )
    
    fnCreateReturnFolder {
        $userdir = get_current_user_id()
        if $userdir != 0 {
            $userdirpath = '/path/to/users/folders/' . $userdir
            if ( !file_exists($userdirpath) ) {
                if ( wp_mkdir_p( $userdirpath ) ) {
                    return $userdirpath
                }
            }
        }
        return 'error'
    }
    
    
    #13181

    Anh Tran
    Keymaster

    I think it works. The upload_dir accepts a string, which is a full path to the upload folder. So you might set it to something like 'upload_dir' => your_function(), e.g., the result of the function.

    #13204

    david.h
    Participant

    Thanks Anh,

    I had shown your suggestion in my snippet:

    ‘upload_dir’ => fnCreateOrReturnFolder

    Although I’m interested in how Metabox would handle an error creating or retrieving the folder, whereupon in the snippet I have set it to return ‘error’ instead of the folder path?

    #13257

    Anh Tran
    Keymaster

    Hi David,

    If the folder is not successfully created, or there’s any error when uploading files to that folder (for various reasons such as exceed max file size upload, file type is not allowed, folder permission, etc.), then the file simply not uploaded and saved.

    At the moment, we don’t have an error reporting. So, if you don’t see any value saved, you just know something wrong.

Viewing 10 replies - 1 through 10 (of 12 total)

You must be logged in to reply to this topic.