Support Forum
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.
Any update On the same
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
Any update on this..
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/'
)
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
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'
}
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.
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?
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.
Hi Anh,
Great feature!
I would like to use it in a multisite installation. Is there a way to upload it to an upload folder within that subsite, without hardcoding the sitenumber in the path?
For multisite installation the regualar path is ../wp-contents/uploads/sites/<sitenumber>/<foldername>.
Is it possible to specify as path ../wp-contents/uploads/<foldername>, and that automatically the /sites/ and sitenumber are added?
It would make setting up a new subsite so much easier...
Hi,
To avoid the complexity, I avoided using predefined variables for things like site number in multisite network. But you can always do that with a little code, like this:
function prefix_get_site_upload_dir() {
return WP_CONTENT_DIR . '/uploads/sites/' . get_current_blog_id() . '/my_uploads/';
}
And in the field settings, set:
'upload_dir' => prefix_get_site_upload_dir(),