I have developed a plugin and in my plugin there is a custom media uploader. When i upload any file from my plugin uploader that file is uploaded and saved in WordPress default uploads/
folder.
But I want that when i upload file from my plugin uploader those files should be uploaded in a new folder inside Wordpress custom uploads
folder so I search from google and found this code:
<?php
function upload_dir($dirs)
{
$dirs['subdir'] = '/my-uploads';
$dirs['path'] = $dirs['basedir'] . '/my-uploads';
$dirs['url'] = $dirs['baseurl'] . '/my-uploads';
return $dirs;
}
add_filter('upload_dir', 'upload_dir');
?>
Now all files that are uploaded from (posts, pages etc) are also stored in that plugin folder.
I want that only those files which are uploaded from my plugin uploader should be saved in that my-folder
, and rest of the files should be saved in WordPress default uploads
folder...
Click the [Windows] button > choose "File Explorer." From the left side panel, right-click "Documents" > choose "Properties." Click [Apply] > Click [No] when prompted to automatically move all files to the new location > Click [OK].
Right before the uploading code, use
add_filter('upload_dir', 'upload_dir');
Then after your uploading code, use:
remove_filter('upload_dir', 'upload_dir');
That way, the filter only works for your plugin uploads and not on other places.
You might need to handle the file processing yourself using plain PHP. When you process the form, try this:
$uploaddir = '/path/to/plugin/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if( move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
echo 'success!';
else
echo 'fail!';
and use print_r($_FILES);
to debug.
Be very, very careful though - you're potentially creating a security risk. see Secure PHP File Upload Script for more info on that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With