PHP rename file after it has been attached with date command

the mail() function

Moderators: egami, macek, gesf

Post Reply
ivanistheking
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Jan 11, 2020 12:23 am

Sat Jan 11, 2020 12:34 am

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
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 904
Joined: Mon Feb 22, 2016 5:52 pm

Sat Jan 11, 2020 1:50 am

If you are using a date stamp in several places, create a variable with it otherwise you run the risk of it changing:

Code: Select all

$date_stamp = date('YmdHis');
file_put_contents(ABSPATH . $date_stamp . ".txt", $output);
ivanistheking
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Jan 11, 2020 12:23 am

Sat Jan 11, 2020 9:21 am

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
ivanistheking
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Jan 11, 2020 12:23 am

Sat Jan 11, 2020 9:33 am

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.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 904
Joined: Mon Feb 22, 2016 5:52 pm

Sat Jan 11, 2020 10:30 am

my code works now
Pleased to be of help, well done!
Post Reply