PHP rename file after it has been attached with date command

A

Anonymous

Guest
Hi there,

I need help with renaming file after it has been attached using wordpess contact form 7.

Here is what is already working:

....
file_put_contents(ABSPATH. "import.txt", $output);
$submission = WPCF7_Submission::get_instance();
$submission->add_uploaded_file('myfile', ABSPATH. '/import.txt');
endif;

This normally works and I receive attached file called "import.txt"

What I want is to attach unique name with date command.

I got file saved with date command, but I don't know how to attach it, please see below:

....
file_put_contents(ABSPATH. date('YmdHis') . ".txt", $output);
$submission = WPCF7_Submission::get_instance();
$submission->add_uploaded_file('myfile', ABSPATH. '/import.txt');
endif;
}

Above generates file name with date like for example: 20202201093200.txt which is exactly what I want.
Thing is I don't know how to attach that file to an email.

Could someone see if it's possible to attach it?

Thank you in advance.

Regards,
Ivan
 
If you are using a date stamp in several places, create a variable with it otherwise you run the risk of it changing:
Code:
$date_stamp = date('YmdHis');
file_put_contents(ABSPATH . $date_stamp . ".txt", $output);
 
Hi hyper,

Thank you for your reply.
I followed your advice and I now have $date_stamp but I still don't know how to attache file, If I try:

$date_stamp = date('YmdHis');
file_put_contents(ABSPATH. $date_stamp . ".txt", $output);
$submission = WPCF7_Submission::get_instance();
$submission->add_uploaded_file('myfile', ABSPATH. '/$date_stamp.txt');

Above code generates txt file with date stamp, but I receive email without attachment.
I guess I don't know how to "call" for the file - last line of code.

Regards,
Ivan
 
Thanks to hyper, my code works now and looks like this:

$date_stamp = date('YmdHis');
file_put_contents(ABSPATH. $date_stamp . ".txt", $output);
$submission = WPCF7_Submission::get_instance();
$submission->add_uploaded_file('myfile', ABSPATH. $date_stamp . ".txt");

Above attaches file in date stamp .txt which is exactly what I wanted.
 
Back
Top