Use a separate upload folder for custom post attachment upload

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?

1
1

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 . "https://wordpress.stackexchange.com/" . $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.

Leave a Comment