如何使用 FPDF 将唯一文件名保存到目录?
How to save unique filename to directory with FPDF?
在使用 FPDF 库生成 PDF 后,我想将每个新生成的具有唯一文件名的 PDF 文件保存到 "receipts" 目录中...就像现在一样,PDF 每次都被覆盖。我可以在 PDF 文件名中附加时间戳吗?示例 --->( /receipt_month-day-year-hour-seconds.pdf )
需要绝对的唯一性,但不是特别关键。
$pdf->Output('receipts/receipt.pdf', 'F');
确保文件名唯一的一种简单(但并非万无一失)的方法是向文件名添加微时间戳。 Microtime 包括千分之一秒,因此除非您的网站有大量流量,否则可能会起作用:
$pdf->Output('receipts/receipt-' . microtime(true) . '.pdf', 'F');
如果您希望时间戳像 receipt_12-26-2017.pdf,那么:
$pdf->Output('receipts/receipt_' . date("m-d-Y") . '.pdf', 'F');
如果你真的想确保每个目录的文件名是唯一的,你可以这样做:
<?php
function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
{
static $_filedata = array();
if ($fp = @opendir($source_dir))
{
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($_recursion === FALSE)
{
$_filedata = array();
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}
while (FALSE !== ($file = readdir($fp)))
{
if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
{
get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
}
elseif (strncmp($file, '.', 1) !== 0)
{
$_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
}
}
return $_filedata;
}
else
{
return FALSE;
}
}
function force_unique_filename( $dir_list, $file_name, $x = 2 )
{
/**
* Dir list may be an array of file names, or in the case of
* cURL, the list may be supplied as a string. If an array, we
* just convert the array to a string so it is checked as a string.
*/
if( is_array( $dir_list ) )
{
$dir_list = implode( ' ', $dir_list );
}
while( strpos( $dir_list, $file_name ) !== FALSE )
{
// Use pathinfo to break apart the filename
$info = pathinfo( $file_name );
// Get the file extension of the file
$ext = '.' . $info['extension'];
// Get the name of the file without extension
$file_name = basename( $file_name, $ext );
// Remove the filename suffix before adding a new one
$pattern = '/\(\d+\)/';
$replacement = '';
$file_name = preg_replace( $pattern, $replacement, $file_name );
// Add new filename suffix
$file_name .= '(' . (string) $x . ')' . $ext;
// Increment the number we are using in a filename suffix "($x)"
$x++;
}
return $file_name;
}
// -----------------------------------------------------------------------
// This directory should be an absolute path...
$source_dir = './receipts';
// The desired filename
$filename = 'receipt_' . date("m-d-Y") . '.pdf';
// Get all of the filenames in this directory
$filenames = get_filenames( $source_dir, FALSE, FALSE );
// Get the unique filename
$unique_filename = force_unique_filename( $filenames, $filename );
$pdf->Output('receipts/' . $unique_filename, 'F');
在使用 FPDF 库生成 PDF 后,我想将每个新生成的具有唯一文件名的 PDF 文件保存到 "receipts" 目录中...就像现在一样,PDF 每次都被覆盖。我可以在 PDF 文件名中附加时间戳吗?示例 --->( /receipt_month-day-year-hour-seconds.pdf )
需要绝对的唯一性,但不是特别关键。
$pdf->Output('receipts/receipt.pdf', 'F');
确保文件名唯一的一种简单(但并非万无一失)的方法是向文件名添加微时间戳。 Microtime 包括千分之一秒,因此除非您的网站有大量流量,否则可能会起作用:
$pdf->Output('receipts/receipt-' . microtime(true) . '.pdf', 'F');
如果您希望时间戳像 receipt_12-26-2017.pdf,那么:
$pdf->Output('receipts/receipt_' . date("m-d-Y") . '.pdf', 'F');
如果你真的想确保每个目录的文件名是唯一的,你可以这样做:
<?php
function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
{
static $_filedata = array();
if ($fp = @opendir($source_dir))
{
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($_recursion === FALSE)
{
$_filedata = array();
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}
while (FALSE !== ($file = readdir($fp)))
{
if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
{
get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
}
elseif (strncmp($file, '.', 1) !== 0)
{
$_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
}
}
return $_filedata;
}
else
{
return FALSE;
}
}
function force_unique_filename( $dir_list, $file_name, $x = 2 )
{
/**
* Dir list may be an array of file names, or in the case of
* cURL, the list may be supplied as a string. If an array, we
* just convert the array to a string so it is checked as a string.
*/
if( is_array( $dir_list ) )
{
$dir_list = implode( ' ', $dir_list );
}
while( strpos( $dir_list, $file_name ) !== FALSE )
{
// Use pathinfo to break apart the filename
$info = pathinfo( $file_name );
// Get the file extension of the file
$ext = '.' . $info['extension'];
// Get the name of the file without extension
$file_name = basename( $file_name, $ext );
// Remove the filename suffix before adding a new one
$pattern = '/\(\d+\)/';
$replacement = '';
$file_name = preg_replace( $pattern, $replacement, $file_name );
// Add new filename suffix
$file_name .= '(' . (string) $x . ')' . $ext;
// Increment the number we are using in a filename suffix "($x)"
$x++;
}
return $file_name;
}
// -----------------------------------------------------------------------
// This directory should be an absolute path...
$source_dir = './receipts';
// The desired filename
$filename = 'receipt_' . date("m-d-Y") . '.pdf';
// Get all of the filenames in this directory
$filenames = get_filenames( $source_dir, FALSE, FALSE );
// Get the unique filename
$unique_filename = force_unique_filename( $filenames, $filename );
$pdf->Output('receipts/' . $unique_filename, 'F');