admin管理员组

文章数量:1312916

So, i'm trying to find out a way to use two separate upload folders, being the default one wp-content/uploads for general media uploads, and another one say wp-content/custom for one specific type of attachments (PDF files attached to one specific post_type).

It is important to keep'em separated both for organization and data security as the PDF files will hold somewhat sensitive data that should only be acessible by two custom user roles, while general media is, well, general.

I'm a little embarrassed to show you the code i got working, because it's lousy, but here it goes:

    function custom_post_type_metabox_save_function($post_id) {

    global $post;

    // Verify auto-save, nonces, permissions and so on then:

    update_post_meta($post_id, "meta_key1", $_POST["value1"]);
    update_post_meta($post_id, "meta_key2", $_POST["value2"]);

// this is where it gets uply. I change the 'upload_path' to my desired one for this post type
    update_option('upload_path','wp-content/custom-upload-dir');

// then upload the file to it
wp_upload_bits($_FILES["pdfexame"]["name"], null, file_get_contents($_FILES["pdfexame"]["tmp_name"]));

// and then change it back to default... :$
    update_option('upload_path','');

}
add_action('save_post','custom_post_type_metabox_save_function');

I'd really rather just have 2 upload files being one for this post-format and another for the rest. Is there a cleaner way to go about it?

So, i'm trying to find out a way to use two separate upload folders, being the default one wp-content/uploads for general media uploads, and another one say wp-content/custom for one specific type of attachments (PDF files attached to one specific post_type).

It is important to keep'em separated both for organization and data security as the PDF files will hold somewhat sensitive data that should only be acessible by two custom user roles, while general media is, well, general.

I'm a little embarrassed to show you the code i got working, because it's lousy, but here it goes:

    function custom_post_type_metabox_save_function($post_id) {

    global $post;

    // Verify auto-save, nonces, permissions and so on then:

    update_post_meta($post_id, "meta_key1", $_POST["value1"]);
    update_post_meta($post_id, "meta_key2", $_POST["value2"]);

// this is where it gets uply. I change the 'upload_path' to my desired one for this post type
    update_option('upload_path','wp-content/custom-upload-dir');

// then upload the file to it
wp_upload_bits($_FILES["pdfexame"]["name"], null, file_get_contents($_FILES["pdfexame"]["tmp_name"]));

// and then change it back to default... :$
    update_option('upload_path','');

}
add_action('save_post','custom_post_type_metabox_save_function');

I'd really rather just have 2 upload files being one for this post-format and another for the rest. Is there a cleaner way to go about it?

Share Improve this question asked Aug 26, 2011 at 0:02 moraleidamoraleida 2,2552 gold badges19 silver badges32 bronze badges 1
  • I've been struggling with this issue for quite a long time and I have found a working solution here : wordpress.stackexchange/questions/193089/… Hopping it can help some folks in the future – leh Commented Dec 23, 2020 at 16:51
Add a comment  | 

1 Answer 1

Reset to default 4

I ended up solving it by completely bypassing the wp upload system, so this is how it looks now:

/*
 * Define new upload paths
 */

$uploadfolder =  WP_CONTENT_DIR . '/exames'; // Determine the server path to upload files
$uploadurl = content_url() . '/exames/'; // Determine the absolute url to upload files
define(RM_UPLOADDIR, $uploadfolder);
define(RM_UPLOADURL, $uploadurl);

    function custom_post_type_metabox_save_function($post_id) {

        global $post;

        // Verify auto-save, nonces, permissions and so on then:

        update_post_meta($post_id, "meta_key1", $_POST["value1"]);
        update_post_meta($post_id, "meta_key2", $_POST["value2"]);
        update_post_meta($post_id, "meta_key3", $_POST["value3"]);

    $destination =  RM_UPLOADDIR; // Determine the path to upload files
    $filename = $_FILES["file"]["name"]; // Get the uploaded file name

    // This separates the extension from the rest of the file name
    $filename = strtolower($filename) ; 
    $exts = split("[/\\.]", $filename) ; 
    $n = count($exts)-1; 
    $exts = $exts[$n];

    $newname = time() . rand(); // Create a new name
    $filepath = $destination . '/' . $newname.'.'.$exts; // Get the complete file path
    $filename = $newname.'.'.$exts; // Get the new name with the extension

    // Now, if the upload was successful we save a post meta with the filename, if not, save nothing
    if (move_uploaded_file($_FILES["pdfexame"]["tmp_name"], $filepath)) {
            update_post_meta($post_id, "rm_martins_exame_url", $filename); 
        }

  }
    add_action('save_post','custom_post_type_metabox_save_function');

It's well less ugly than what i had before, but still would be much better if this could be done using the upload_dir filter.

本文标签: Use a separate upload folder for custom post attachment upload