Strange behaviour when modifying upload_dir ($path)
-
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.
- The topic ‘Strange behaviour when modifying upload_dir ($path)’ is closed to new replies.