Uploading .dta and do files on wordpress - wordpress

I am currently building a website for sharing statistical files. I am using wordpress CMS to build it. However, when I try to upload files with extension .dta and do files, it raises this error:
cr-ethdat1997-version3.do: Sorry, this file type is not permitted for security reasons.
How can I resolve it?

If you don't want WordPress to check what file types you are uploading, you can add a constant in the wp-config.php file:
define( 'ALLOW_UNFILTERED_UPLOADS', true );
Otherwise, if you want file type checking, you could add certain mime types with the following filter:
function my_custom_mime_types( $mimes ) {
// New allowed mime types.
$mimes['svg'] = 'image/svg+xml';
$mimes['svgz'] = 'image/svg+xml';
$mimes['doc'] = 'application/msword';
return $mimes;
}
add_filter( 'upload_mimes', 'my_custom_mime_types' );
You would need to find the MIME types for the files you'd want to upload (.do, .dta)
Take a look at the Codex for more details: https://developer.wordpress.org/reference/hooks/upload_mimes/

Related

WordPress upload file

I would like to upload a file like as (.apk) but I can't. I receive this message.
Sorry, this file type is not allowed for security reasons.
I show solutions as to settings on multisite, but I don't have this choice on my version. In which way I could enable the settings of multisite?
Is there any way to upload files like apk,( may a plugin)?
Thank you in advance!
Open your functions.php file and add the following code inside it.
add_filter('upload_mimes', 'allow_custom_mimes');
function allow_custom_mimes ( $existing_mimes = array() ) {
// with mime type ‘application/vnd.android.package-archive
$existing_mimes['apk'] = 'application/vnd.android.package-archive';
return $existing_mimes;
}
Restart your server after adding code and check.

How To Display Uploaded Image Correctly?

I have Wordpress Plugin in which I try To display an uploaded image, but I get url like this:
http://localhost/opt/lampp/htdocs/wordpress2/wp-content/plugins/fileuploadplugin/uploads/website-development-banner.jpg
instead of this:
http://localhost/wordpress2/wp-content/plugins/fileuploadplugin/uploads/website-development-banner.jpg
My code is:
<?php
// Configuration - Your Options
$allowed_filetypes = array('.csv','.jpg','.gif','.png','.jpeg'); // These will be the types of file that will pass the validation.
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
define( 'WP_PLUGIN_DIR', $_SERVER['DOCUMENT_ROOT'] . '/wordpress2/wp-content/plugins' );
//$targetpath=WP_PLUGIN_DIR.'/testplugin/documents/'.basename($_FILES["photo"]["name"]);
$upload_path = WP_PLUGIN_DIR.'/fileuploadplugin/uploads/'; // The place the files will be uploaded to (currently a 'files' directory).
//echo $upload_path;exit;
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
//$targetpath='/uploads'.basename($filename);
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file here'; // It worked.
else
echo 'There was an error during the file upload. Please try again.'; // It failed :(.
?>
My Problem is how to remove /opt/lampp/htdocs from $uploadpath to get uploaded image to display correctly?
The problem is that you are using the same path for both uploading and displaying the image.
Path to Upload the image
In your upload code above, you need to use the file system path, as you are doing. The path in your code is:
$upload_path = WP_PLUGIN_DIR.'/fileuploadplugin/uploads/'; // The place the files will be uploaded to (currently a 'files' directory).
This will have a value something like
/home/user/var/www/wp-content/plugins/fileuploadplugin/uploads/
URL to Display the image
The file system path obviously won't work to display the image, so you need to use a URL or a path relative to your website, e.g.
http://www.example.com/wp-content/plugins/fileuploadplugin/uploads/uploadedimage.jpg
To get the URL for your plugins folder, you need to use:
$upload_url = WP_PLUGIN_URL.'/fileuploadplugin/uploads/';
And then you can display the image using that path, e.g.
<img src="/<?php echo $upload_url.$filename; ?>" />
Note:
I have kept WP_PLUGIN_DIR and used WP_PLUGIN_URL in the code above to make it easier for you to see the changes required. However these should not be used directly. Instead you should use the functions provided by WP to get these values: ref Wordpress Codex: Determining Plugin and Content Directories.

Uploading .kml files to WordPress

I'm trying to upload .kml files to WordPress. I had this working at one point but the latest WordPress update seems to have broken it.
I'm using this function
function my_myme_types($mime_types){
$mime_types['kml'] = 'application/vnd.google-earth.kml+xml'; //Adding kml extension
$mime_types['kmz'] = 'application/vnd.google-earth.kmz'; //Adding kmz files
return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);
I get this error when uploading
"Sorry, this file type is not permitted for security reasons."
For KML/KMZ files to be properly supported, you'll have to use text/xml and application/zip instead, because WordPress compares the declared MIME type to the 'real' detected MIME type (see function wp_check_filetype_and_ext in wp-includes/functions.php for more details)
function add_upload_mimes($mimes) {
$mimes['kml'] = 'text/xml';
$mimes['kmz'] = 'application/zip';
return $mimes;
}
add_filter('upload_mimes', 'add_upload_mimes');
Update (2019-02-28) : kml is detected as text/xml and not application/xml, changing the code accordingly should resolve the issue described in the comments below.
This may be related as of after the latest wordpress update:
Trying to upload xml file and getting the same error message, updating upload_mimes WordPress filter hook doesn't work as well as using some file uploading or mime type managing plugins which essentially using the same filter hook.
Solution: update wp-config.php and add in the following line
define( 'ALLOW_UNFILTERED_UPLOADS', true );
and then remove this line after uploading the files to avoid potential security risk

wordpress media upload to rename files appropriately if file exists

I'm creating a plugin in wordpress that uses the wp media uploader to upload files to the site. Problem is that if a file exists with the same name, the name of the file being currently uploaded is appended with a number at the end.
This is a problem if I upload file001.pdf and then the next file is renamed to file0012.pdf instead of file001-2.pdf
It's a problem because then the user may think that is file 12 and not version 2 of file 1.
How can i change that so if there's already a file in the system with the same name, the file being uploaded gets the right rename?
EDIT
So I found out there's a function in wp-includes/functions.php called wp_unique_filename which will check for unique file names and increment until the name is unique. I just need to find a way now to customize that function on the plugin directory.
WordPress provides one hook wp_handle_upload_prefilter as below
function handle_uploadedimage($arr) {
$random_number = md5(rand(10000,99999));
$ext = pathinfo($arr['name'], PATHINFO_EXTENSION);
$arr['name'] = $random_number .'.'.$ext;
return $arr;
}
add_filter('wp_handle_upload_prefilter', 'handle_uploadedimage', 1, 1);

WordPress plugin localization, load_plugin_textdomain doesn't work

Ok i built plugin for contact form, I wanna add translation for it. In my main plugin files i add this code
function ap_action_init() {
// Localization
load_plugin_textdomain('prijava_forma', false, dirname(plugin_basename(__FILE__))."/languages";
}
// Add actions
add_action('init', 'ap_action_init');
in my file where contact form is written i have this
_e( 'Prva','prijava_forma' );
In my language folder I added the .mo and .po files created with Poedit.
Also I defined WPLANG in config.php and changed the language in the dashboard.
But i get no translation. Where could be problem, i am new to this?
There are many possible causes:
.mo file not readable or not found at all (.po file is not used by WordPress by the way)
The string you are expecting is not translated yet
Wrong .mo file name, valid names are ar.mo, fr_FR.mo..., invalid ones are br_BR.mo, arabic.po, AR.mo, ar_AR.mo... So make sure you get this one right.
for plugins the name will be the concatenation for the text domain, a dash and the locale: myplugin-ru_RU.mo
Check what load_plugin_textdomain() returned, if the .mo file was loaded, it should return true, in which case the next step would be to check that you are not missing the textdomain parameter in your __(), _e() and similar functions.
More on WordPress localization
It may be also caused on hook where the functions is attached and where are the po/mo files.
On the Init Hooks, load_plugin_textdomain() returned true but string were not translated.
I changed the action to plugins_loaded as my po/mo were in a folder inside a Custom plugins.
Also make sure that your string stands for itself. Do not append anything to the string, do this after the gettext function instead.
Wrong:
return __('Please translate me'.'(666)','your-textdomain');
Right:
return __('Please translate me','your-textdomain').'(666)';

Resources