Contact Form 7
[resolved] upload file to wordpress folder before send email (9 posts)

  1. emile13
    Posted 4 years ago #


    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?



  2. Takayuki Miyoshi
    Plugin Author

    Posted 4 years ago #

    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"].

  3. emile13
    Posted 4 years ago #

    thanks so much you save my day

  4. mrjak01
    Posted 4 years ago #

    @ 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

  5. emile13
    Posted 4 years ago #

    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

  6. mrjak01
    Posted 4 years ago #


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

  7. emile13
    Posted 4 years ago #

    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


  8. jsims281
    Posted 4 years ago #

    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');
  9. atomik
    Posted 4 years ago #

    Great shot!
    Gonna try it asap.


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Contact Form 7
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic