Quantcast
Channel: WordPress.org Forums » [Contact Form 7] Support
Viewing all articles
Browse latest Browse all 49500

normadize on "[Plugin: Contact Form 7] Uploaded files are deleted even on successful email (?)"

$
0
0

For anyone interested, if you want your uploaded files to be accessible after upload via a URL, and also to have that URL in the email sent by WPCF7, then here's a solution I used in my plugin.

If you don't use classes, then replace the array with just the function name. Note that this is for one file only, use a foreach() loop if your form has multiple upload files.

You may also want to change the chmod() permissions to suit your server. In my case, SuPHP requires 644 or else it denies access.

class MyPlugin
{
    /**
     * Relative to the WP uploads dir.
     */
    public $dir_uploaded = 'foo';

    function __construct ()
    {
        add_action('wpcf7_before_send_mail', array($this, 'WPCF7_BeforeSendMail'));
    }

    /**
     * Fetch uploaded files and modify email body to point to uploaded file url.
     * @param WPCF7_ContactForm $cf7
     */
    public function WPCF7_BeforeSendMail (WPCF7_ContactForm $cf7)
    {
        if (empty($cf7->uploaded_files['fileupload']))
            return;
        $dest = wp_upload_dir();
        $dest_dir = $dest['basedir'].$this->dir_uploaded;
        $dest_url = $dest['baseurl'].$this->dir_uploaded;
        wp_mkdir_p($dest_dir);
        $dest_file = wp_unique_filename($dest_dir, basename($cf7->uploaded_files['fileupload']));
        $dest_path = "$dest_dir/$dest_file";
        @rename($cf7->uploaded_files['fileupload'], $dest_path);
        // avoid SuPHP denying access since WPCF7 hardcodes 0400 (...)
        chmod($dest_path, 0644);
        $cf7->mail['body'] = "Uploaded: $dest_url/$dest_file\n\n" . $cf7->mail['body'];
    }
}

Cheers.


Viewing all articles
Browse latest Browse all 49500

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>