Generate filename with creation date

denully picture denully · Sep 6, 2012 · Viewed 43.5k times · Source

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 :)

Answer

Fluffeh picture Fluffeh · Sep 6, 2012

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.