如何使用 dompdf 为我的 PDF 文件生成动态文件名?
我正在使用 dompdf ,它似乎运行得很好。
我将生成的 pdf 文件作为电子邮件附件获取。我想知道如何在文件名后附加一个唯一的数字,以便每个接收 pdf 的用户都不会具有完全相同的文件名。
我目前获取文件的名称并获取已成功分配给它的任何名称,例如 myfile.pdf。
作为测试,我还可以在文件名后附加一个随机数,如下所示:
$message = Swift_Message::newInstance()
->setSubject('My Subject Text') // Message subject
->setTo(array('[email protected]' => 'MyName')) // Array of people to send to
->setFrom(array('[email protected]' => 'SenderName')) // From:
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'myfile'.rand(10,1000).'.pdf', 'application/pdf')); // Attach the generated PDF from earlier
这对于发送带有文件名和随机数的 pdf 非常有用。
但我怎样才能用更有意义的东西来做到这一点呢?
就像从特定数字开始并为提交的每个文件自动递增一样?
或者使用基于(或来自)会话的数字?
什么是有意义的最佳实践?
我希望每个文件名都是 myfile_UNIQUENUMBER.pdf (我认为某种类型的自动增量或会话编号是最好的)
有什么想法?
I am using dompdf which seems to be going really well.
I get my generated pdf file as an email attachment. I wanted to know how to append a unique number to the file name so that every users that receives the pdf does not have the same exact file name.
I currently get name the file and get whatever I name have assigned it successfully, such as myfile.pdf.
As a test, I can also get a random number appended to the filename like this:
$message = Swift_Message::newInstance()
->setSubject('My Subject Text') // Message subject
->setTo(array('[email protected]' => 'MyName')) // Array of people to send to
->setFrom(array('[email protected]' => 'SenderName')) // From:
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'myfile'.rand(10,1000).'.pdf', 'application/pdf')); // Attach the generated PDF from earlier
That works great for sending a pdf with the filename and random number.
But how can I do this with something that makes more sense?
Like starting with a specific number and auto-incrementing for every file that gets submitted?
Or using numbers based on (or from) the SESSION?
What is the best practice that would make sense?
I would like each file name to be myfile_UNIQUENUMBER.pdf (I think some type of auto-increment or SESSION number would be best)
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当将日期与
d/m/year
和时间一起使用时,您可以使用日期作为唯一的数字,并使用您选择的名称,这样每个文件名都会有不同的数字。尝试以下代码:You may use the date as unique number when use it with
d/m/year
AND TIME with a name you choose so every filename will have different number..try this code :