skip to Main Content

Good morning, I’m writing some php strings in fuction.php. This code get specific details of formv(CF7) and write its in a txt file in ftp. I done all fields but I’m not able get the url of uploaded file. I have to get url and write it toghether other fields in txt file. How I can do it?

I tried with:

 $text .= $data['file-799'];

but it doesnt work
This is the code:


add_action('wpcf7_before_send_mail', 'log_cf7');


 function log_cf7($WPCF7_ContactForm) {
   $submission = WPCF7_Submission::get_instance();
   $data = $submission->get_posted_data();

   $upload_dir   = wp_upload_dir();
   echo $upload_dir['basedir'] . '/wpcf7_uploads';
  $text .= "|";
  $text .= "CSTPRODREG";
  $text .= "||||||||||||||||";
  $text .= $data['checkbox-339'];
  $text .= "||";
  $text .=  $data['your-name'];
  $text .= "|";
  $text .=  $data['text-628'];
  $text .= "||||||";
  $text .=  $data['tel-709'];
  $text .= "|||";
  $text .=  $data['your-email'];
  $text .= "|||||";
  $text .=  $data['menu-470'][0];
  $text .= "||";
  $text .=  $data['menu-749'][0];
  $text .=  $data['menu-750'][0];
  $text .=  $data['menu-751'][0];
  $text .=  $data['menu-752'][0];
  $text .=  $data['menu-753'][0];
  $text .= "||";
  $text .=  $data['text-590'];
  $text .=  $data['text-591'];
  $text .= "||";
  $text .= $formatted_date = str_replace('-', '', $data['date-855']);
  $text .= "|||";
// follow the field of form for uploaded file
  $text .= $data['file-799'];
foreach ($data as $key => $value) {
}

   $myfile = fopen($_SERVER['DOCUMENT_ROOT'] . "/myfile.txt","wb");

   fwrite($myfile, $text);
   $ftp_server="myserver";
   $ftp_username="myuser";
   $ftp_userpass="mypass";
   $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to server");
   $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
   if(ftp_put($ftp_conn,"newdata.prr",$_SERVER['DOCUMENT_ROOT'] . "/myfile.txt",FTP_ASCII))
    {
        print("yay");
    }
    else
    {
        print("f...");
    }
    fclose($myfile);
}
?>

Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    The answer often are more easy that we are thinking... Since cf7 with Contact Form CFDB7 record every file submissioned in a ftp folder I only wrote this string:

    $text .= 'www.mysito.com/percorso/uploads/cfdb7_uploads/' . $_FILES['file-799']['name'];
    

    And in this way, whit the other necessary fields, function.php write in txt file the complete url of uploaded file

    thank you all


  2. I think CF7 stores uploaded files in a temporary location and attaches them to the email as attachments. It doesn’t provide a direct URL to the uploaded file by default. To get the URL of the uploaded file, you can modify your code as follows:

    add_action('wpcf7_before_send_mail', 'log_cf7');
    
    function log_cf7($WPCF7_ContactForm) {
        $submission = WPCF7_Submission::get_instance();
        $data = $submission->get_posted_data();
    
        // Assuming the file input field name is "file-799"
        $file_field_name = 'file-799';
    
        // Retrieve the uploaded file details
        $uploaded_files = $submission->uploaded_files();
        
        if (isset($uploaded_files[$file_field_name])) {
            $file_url = $uploaded_files[$file_field_name]['url'];
    
            // FTP server details
            $ftp_server = 'ftp.example.com';
            $ftp_username = 'your_ftp_username';
            $ftp_password = 'your_ftp_password';
            $ftp_directory = '/path/to/directory/';
            $text_file = 'uploaded_files.txt';
    
            // Connect to FTP server
            $ftp_connection = ftp_connect($ftp_server);
            ftp_login($ftp_connection, $ftp_username, $ftp_password);
    
            // Change to the desired directory
            ftp_chdir($ftp_connection, $ftp_directory);
    
            // Write the file URL to the text file
            $content = $file_url . PHP_EOL;
            ftp_put($ftp_connection, $text_file, $content, FTP_APPEND);
    
            // Close FTP connection
            ftp_close($ftp_connection);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search