• moxymore

    (@moxymore)


    Hi,

    Let’s take a simple example of how to change the native upload dir thanks to wp_handle_upload_prefilter :

    function my_custom_upload_dir($path) {
        $custom_dir   = 'my-custom-dir'
        $path['subdir'] = $custom_dir.'/';
        $path['path']   = $path['basedir'].$custom_dir;
        $path['url']    = $path['baseurl'].$custom_dir;
        return $path;
    }
    
    add_filter('wp_handle_upload_prefilter', 'my_upload_prefilter');
    add_filter('wp_handle_upload', 'my_handle_upload');
    function my_upload_prefilter( $file ) {
        add_filter('upload_dir', 'my_custom_upload_dir');
        return $file;
    }
    
    function my_handle_upload( $fileinfo ) {
        remove_filter('upload_dir', 'my_custom_upload_dir');
        return $fileinfo;
    }

    This code is perfectly working as expected. But what I expected to do was to put uploaded files outside the native wordpress uploads location, in the server root.

    I thought this process was going to be easy : with some logic, I thought that modifying $path[‘url’] and $path[‘path’] in my previous code will do the job, but it doesn’t :

    $path['path']   = $_SERVER["DOCUMENT_ROOT"].$custom_dir;
    $path['url']    = get_site_url().$custom_dir;

    But with this modification, a strange behavious happens : uploaded files are physically properly located in the server’s root directory (as expected), but url isn’t properly defined! In facts, the entry of _wp_attached_file of the wp_postmeta table is strictly equal to the $path[‘path’] string, when it should theorically represent the relative path of $path[‘url’].

    What is strange behind this is that modifying $path[‘url’] has no effect. I tried too to modify $path[‘baseurl’], but same issue : no effect.

    I even tried to declare UPLOADS constant in my php script, since it isn’t defined there, and by doing this I can modify the native ‘wp-content/uploads” base dir upload.

    Do you guys have some ideas regarding my problem? It’s kinda strange…

    PS : I’m on localhost, and I really wonder if my windows operating system with backslash separator isn’t conflicting with the generated servers which need antislashes.

    Regards.

    • This topic was modified 6 years ago by moxymore.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The meta data keyed “_wp_attached_file” is supposed to be relative to the uploads folder. That portion would be the same regardless of whether we are talking about a path or URL. The file’s URL doesn’t really appear anywhere in the DB (except as the attachment’s GUID, which should not be relied upon, that’s not what it’s for). The URL is constructed dynamically based upon the upload location and this meta value.

    You can move the uploads folder into any path (even of your own creation) within the WP ABSPATH structure by defining the desired path as UPLOADS relative to ABSPATH in wp-config.php, according to the Codex.

    It’s not clear where your WP installation is relative to server root, or if you mean real root or public docs root. It’s inadvisable to store files in the installation folder and you cannot store uploads outside of the installation structure, so I have to conclude your plan is ill advised. I’m not saying it’s not possible, only that it’s beyond what WP is intended to do. Maybe it could be accomplished with symlinks or something, IDK.

Viewing 1 replies (of 1 total)
  • The topic ‘Strange behaviour when modifying upload_dir ($path)’ is closed to new replies.