create a plugin that allow file format .exe upload

Home » Snippets » create a plugin that allow file format .exe upload
0

Created with:

Visibility: 

public

Creator: Solomon

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/*
Plugin Name: File Upload Plugin
Description: Allows uploading .exe files.
Version: 1.0
Author: Your Name
*/

// Enqueue necessary scripts and styles for the file upload form
function wpturbo_enqueue_scripts() {
    wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'wpturbo_enqueue_scripts');

// Add the .exe file extension to the allowed file types
function wpturbo_add_exe_to_allowed_types($mime_types) {
    $mime_types['exe'] = 'application/exe';
    return $mime_types;
}
add_filter('upload_mimes', 'wpturbo_add_exe_to_allowed_types');

// Display a custom error message for .exe file uploads
function wpturbo_custom_upload_error_message($error) {
    if(strpos($error, 'Sorry, this file type is not permitted for security reasons.') !== false) {
        $error = 'Sorry, only .exe files are allowed to be uploaded.';
    }
    return $error;
}
add_filter('upload_size_error_message', 'wpturbo_custom_upload_error_message');

// Validate the file extension before uploading
function wpturbo_validate_file_extension($file) {
    $allowed_extensions = array('exe');
    $file_extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
    if(!in_array($file_extension, $allowed_extensions)) {
        $file['error'] = 'Sorry, only .exe files are allowed to be uploaded.';
    }
    return $file;
}
add_filter('wp_handle_upload_prefilter', 'wpturbo_validate_file_extension');
				

Explanation:

This code creates a WordPress plugin that allows uploading .exe files. It adds the .exe file extension to the allowed file types using the upload_mimes filter. Additionally, it enqueues the necessary scripts and styles for the file upload form using the wp_enqueue_scripts action.

To provide a custom error message for .exe file uploads, the code uses the upload_size_error_message filter to check if the default error message contains the string "Sorry, this file type is not permitted for security reasons." If it does, the code replaces it with a custom error message.

To validate the file extension before uploading, the code uses the wp_handle_upload_prefilter filter. It checks if the uploaded file's extension is in the list of allowed extensions (in this case, only .exe files are allowed). If the file extension is not allowed, the code sets an error message and prevents the file from being uploaded.

Please note that allowing .exe file uploads can be a security risk, as these files can potentially contain harmful code. It's important to implement additional security measures to ensure that only trusted users can upload .exe files and that the uploaded files are thoroughly scanned for malware.

Register an account to save your snippets or go Pro to get more features.