简体   繁体   English

如何创建已保存文件名的列表?

[英]How to create a list of saved file names?

I'm working on page where you can generate word document with items for customers. 我正在研究页面,您可以在其中生成带有客户项目的Word文档。 There is a tab called "Latest generated documents". 有一个名为“最新生成的文档”的选项卡。 How to save each generated document name and add it to list? 如何保存每个生成的文档名称并将其添加到列表? Should I use array? 我应该使用数组吗?

To define file name I'm using this code: 要定义文件名,我使用以下代码:

$filename = "".$name." ".$surname."_report.docx";

Name and surname depends on what customer I choose when I'm creating a document. 名称和姓氏取决于我在创建文档时选择的客户。 The file is saved to folder "documents" but each time file is overwritten with new. 该文件保存到文件夹“ documents”,但是每次文件都被新文件覆盖。

If you don't overwrite the files you could scan your directory and add each found file into an array, an other approach would be to store the filenames in a database. 如果不覆盖文件,则可以扫描目录并将找到的每个文件添加到数组中,另一种方法是将文件名存储在数据库中。

$data = scandir($path_to_directory);
foreach($data as $file) {
   // display filename
}

1) Create a document for your user 1)为您的用户创建一个文档

2) Insert the name of the file in your database and perhaps also the time the file was created/inserted 2)在您的数据库中插入文件的名称,也许还要插入文件的创建/插入时间

3) When you should display latest generated documents you query your database for these filenames sorted on created DESC 3)当您应该显示最新生成的文档时,请在数据库中查询按创建的DESC排序的这些文件名

like this for example:

$documents = array();
$query = "SELECT * FROM tbl_document ORDER BY doc_created DESC";
if(($result = mysql_query($query))) {
    while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
        $documents[] = $row;
     }
     mysql_free_result($result);
 }

Now you have an array with the latest documents that you can display to the user, I think you also want to remove records from the database depending on the value of doc_created. 现在,您有了一个可以显示给用户的带有最新文档的数组,我想您还想根据doc_created的值从数据库中删除记录。

 // the following query will remove documents older than 1 hour from the database
 mysql_query("DELETE FROM tbl_document WHERE 
              UNIX_TIMESTAMP(doc_created) < ".(time()-(60*60)));

The above assumes you have created a table that looks something like this: 以上假设您已经创建了一个看起来像这样的表:

 CREATE TABLE tbl_document (
    doc_id int(11) unsigned NOT NULL PRIMARY_KEY AUTO_INCREMENT,
    doc_filename VARCHAR(260) NOT NULL,
    doc_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM