Support » Plugin: Contact Form 7 » [Plugin: Contact Form 7] upload file to wordpress folder before send email

  • Resolved emile13



    i’m using the function
    add_action( ‘wpcf7_before_send_mail’, ‘contactform7_before_send_mail’ );

    to create a post in wordpress with the data of contact form 7

    i whant to upload a file to my wp-content/uploads/ but when i do this

    $file= $cf7->posted_data[“file”];
    i have just the name of the file and not the original directory (ex :c:\Users\documents\myfile.jpg

    do you have an idee how can i proccess?


Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Takayuki Miyoshi


    The $cf7->posted_data parameter only has file names. Use $cf7->uploaded_files instead, as it is an array of {field name => file path}. So when the field name is ‘file’, you can get the file path by $cf7->uploaded_files["file"].

    thanks so much you save my day

    @ emile13


    Is this correct? Your code is sending the email and uploaded file notification to the email recipient but also the file is being uploaded to a permanent directory?

    Can you please explain the process? I would like to set this up for myself.

    Thank you

    yes it’s the idea

    i use it for concerts events

    a visitor add an events and i create a post to draft and i have just to publish it after

    save time, save money

    Are uploaded images going into a static folder though? If so, how do you have that set up?

    yes it’s the idea upload the images in uploads directory, create thumbail and set as featured image

    you can look here the final code

    I use this code to move my uploaded files into the uploads folder before cf7 gets chance to delete them. I also add a random string to the start of the filename so that there is almost no risk of duplicated file names.

    function update_event(){
        $random = date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000)).rand(0,10000);
        $eventImgClean = "/".$random.str_replace("/home/website/public_html/wp-content/uploads/wpcf7_uploads/","",$cf7->uploaded_files['event-image']);
        $eventImg = $cf7->uploaded_files['event-image'];
        if (strlen($eventImg)<1)  {
          $eventImg = FALSE;
        else {
          //make sure the image is below 1Mbyte
          if (filesize($eventImg) < 1048576){
            copy($eventImg, "wp-content/uploads/".$eventImgClean);
            $evImg = "/wp-content/uploads/".$eventImgClean;
            $filesize1_ok = TRUE;
          } else {
            $filesize1_ok = FALSE;
        //do extra stuff here
    add_action('wpcf7_before_send_mail', 'update_event');

    Great shot!
    Gonna try it asap.


Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Plugin: Contact Form 7] upload file to wordpress folder before send email’ is closed to new replies.