Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate filename with creation date

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

like image 827
denully Avatar asked Sep 06 '12 10:09

denully


People also ask

How do I put the date in a FileName?

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.

How do you create a FileName with a date and time in python?

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.

How do I create a FileName from a date in Linux?

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.


1 Answers

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.

like image 175
Fluffeh Avatar answered Sep 29 '22 11:09

Fluffeh