I'm using dompdf to create and mail a PDF file to my mail and at the same time, save a .txt version on the server. Saving the file is working as it should, but im having a bit of trouble getting it to save it with a unique name. In this case i wanted something like date-time.txt ( 06-09-2012_11:43.txt )
or even better, if it could have the name from the text field "refnr" as the name.
<label for="refnr"><b>Referensnummer:</b></label>
<input type="text" name="refnr" id="refnr" class="input" />
The code looks like this:
$html = '/html.php';
$filename = $dir.'/Admin/files/"date here".txt';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper('a4', 'portrait');
$dompdf->render();
file_put_contents($filename, $dompdf->output());
I tried to play around with $name='myfile_'.date('m-d-Y_hia)';
but couldn't make that work, it just gave an error on that line every time.
So now im here to seek the guidance from you smart people :)
I'd use YYYY-MM-DD HHmmss for filenames, unless there is a particular need for timezones or a possible need to parse them into ISO dates; in those cases an ISO date would probably be preferrable.
First, import the module and then get the current time with datetime. now() object. Now convert it into a string and then create a file with the file object like a regular file is created using file handling concepts in python.
You should use double quotes and need to evaluate date +"%F" using command substitution. Double quote helps you create a single file where some options of date command would include a space. For example, touch test_$(date) will create multiple files, where as touch "test_$(date)" won't.
You put the )
before you closed the string format code:
$name='myfile_'.date('m-d-Y_hia');
Should work fine.
As Jan1337z points out, you probably want a suffix on the file:
$name='myfile_'.date('m-d-Y_hia').'.txt';
Not having a suffix should't stop the file being created - but having it will probably help make it easily usable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With