Is it possible to set upload path outside public_html directory?

How to set path to outside public_html directory for forminator uploads. I want to set a private public_html path for uploads from forminator.

  • Dimitris
    • Support Star

    Hello there Mohamed,
    hope you're doing well today! :slight_smile:

    This is escalated to our Second Level Support team for further investigation.
    Me or another colleague of mine will keep you posted here as soon as there's any development on this. Your patience here is highly appreciated!

    Warm regards,
    Dimitris

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hi Mohamed ,

    You can do this with a mu-plugin but you'll have to change some parts of the code to adjust the folder that you like.

    How to install:
    Always make sure to keep a backup of your site before changing/adding custom code.

    1] Navigate to your /wp-content/ directory and create a new one named mu-plugins if it doesn't exist.
    2] Inside the mu-plugins folder create a file named frmt-my-upload-path.php
    3] Edit the file and copy / paste this code snippet inside.

    <?php
    
    /**
     * Plugin Name: Forminator Pro - Change Upload Path
     * Plugin URI: https://premium.wpmudev.org/
     * Description: mu-plugin for changing the Forminator upload dir to /uploads/ID.
     * Version: 1.0.0
     * Author: Konstantinos Xenos @ WPMUDEV
     * Author URI: https://premium.wpmudev.org/
     * License: GPLv2 or later
     */
    
    class Change_Forminator_Upload_Dir {
    
    	/**
    	 * New dir var.
    	 *
    	 * @var $new_dir_id
    	 */
    	private $new_dir_id = 0;
    
    	/**
    	 * Constructor.
    	 */
    	public function __construct() {
    		add_action( 'forminator_custom_form_before_save_entry', array( $this, 'my_form_change_upload_dir' ) );
    		add_action( 'forminator_custom_form_after_save_entry', array( $this, 'my_form_restore_upload_dir' ) );
    	}
    
    	/**
    	 * Hook into Forminator and apply the upload_dir filter.
    	 *
    	 * @param int $form_id The form ID.
    	 */
    	public function my_form_change_upload_dir( $form_id ) {
    		$this->new_dir_id = $form_id;
    		add_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
    	}
    
    	/**
    	 * Hook into the upload_dir filter and change the path.
    	 *
    	 * @param array $param The upload dir parameters array.
    	 */
    	public function my_form_ad_formid_to_upload_dir( $param ) {
    		// change this into the path you want the uploaded files to go
    		$new_path = '';
    
    		// change this into the url that those files would have
    		$new_url = '';
    
    		$param['path'] = $new_path . $this->new_dir_id . '/' . date( 'Y' ) . '/' . date( 'm' );
    
    		$param['url']  = $new_url . $new_path;
    
    		return $param;
    	}
    
    	/**
    	 * Hook into Forminator and remove the upload_dir filter.
    	 *
    	 * @param int $form_id The form ID.
    	 */
    	public function my_form_restore_upload_dir( $form_id ) {
    		$this->new_dir_id = 0;
    		remove_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
    	}
    }
    
    new Change_Forminator_Upload_Dir();

    4] Save and close the file.
    5] The final path should look like /wp-content/mu-plugins/frmt-my-upload-path.php

    In the code above you should edit these lines:

    // change this into the path you want the uploaded files to go
    		$new_path = '';
    
    		// change this into the url that those files would have
    		$new_url = '';

    the $new_path should contain the path on your server that you want the files to be uploaded at for example: if you set "/var/www/my_uploads/" the final path would be "/var/www/my_uploads/7/2019/05/file.jpg", in essense the folder that you wanted followed by the form id and the current year / month.

    You should also change the $new_url if you want to be able to download those files when viewing the Submissions in the admin area.

    Tell me if you need any further help with this.

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.