php 使用创建日期生成文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12298048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:17:56  来源:igfitidea点击:

Generate filename with creation date

phpdatetimefilenamesdompdf

提问by denully

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 )

我正在使用 dompdf 创建一个 PDF 文件并将其邮寄到我的邮件中,同时在服务器上保存一个 .txt 版本。保存文件正常工作,但我在使用唯一名称保存文件时遇到了一些麻烦。在这种情况下,我想要类似 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.

或者甚至更好,如果它可以将文本字段“refnr”中的名称作为名称。

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

我试图玩,$name='myfile_'.date('m-d-Y_hia)';但不能让它工作,它每次都在那条线上出错。所以现在我在这里寻求聪明人的指导:)

回答by Fluffeh

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 wanta suffix on the file:

正如 Jan1337z 指出的那样,您可能需要在文件上添加后缀:

$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.

没有后缀不应停止正在创建的文件 - 但拥有它可能有助于使其易于使用。