我有一个表单,它向文件夹上传中的服务器提交文件,并将文件名(存储在服务器中)以及其他文件详细信息保存在我的数据库中。现在我要做的是创建一个HTML表,该表显示文件细节以及相应的文件(当然是超链接)。
有办法吗?我的一个想法是将文件名链接到我的上载/目录中的相应文件,但我不知道如何做到这一点。
这是我的表uploaded_content,表单数据存储在我的数据库中
================================================
id |Description | filename |
------------------------------------------------
1 |Information | 3223-2323-4334-32-slajjjq.txt|
2 |Users | 3223-2323-4344-33-slik.txt |
================================================下面是我的uploads/目录的一个示例(请注意,它们的名称保存在我的数据库中)
3223-2323-4334-32-slajjjq.txt
3223-2323-4344-33-slik.txt因此,现在我想创建一个HTML表,其中包含以下列
id |Description | filename |File 发布于 2019-05-19 17:48:35
这不是最好的方法,但我只想教你怎么做。
步骤1-创建mysql。
CREATE TABLE `uploaded_content` (
`id` int(11) NOT NULL,
`description` text NOT NULL,
`filename` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;步骤2-向此DB插入数据。
INSERT INTO `uploaded_content` (`id`, `description`, `filename`) VALUES
(1, 'Information', '3223-2323-4334-32-slajjjq.txt'),
(2, 'Users', '3223-2323-4344-33-slik.txt');步骤3-使config.php:
<?php
$conn = new PDO('mysql:host=localhost;dbname=stack', 'root', '');
$q = $conn->query("SELECT * FROM uploaded_content WHERE id");
$f = $q->fetchAll();
?>步骤4-使用以下代码创建index.php:
<?php
include('config.php');
?>
<table>
<tr>
<th>ID</th>
<th>Description</th>
<th>File</th>
<th>Download link</th>
</tr>
<?php
foreach ($f as $g) :?>
<tr>
<td> <?php echo $g['id']; ?></td>
<td> <?php echo $g['description']; ?></td>
<td> <?php echo $g['filename']; ?></td>
<td><a href="https://example.com/uploads/<?php echo $g['filename']; ?>">Download</a></td>
</tr>
<?php endforeach;?>
</table>当您完成该操作时,您将得到这样的html页面:
<table>
<tr>
<th>Id</th>
<th>Description</th>
<th>File</th>
<th>Download link</th>
</tr>
<tr>
<td>1</td>
<td>Information</td>
<td>3223-2323-4334-32-slajjjq.txt</td>
<td><a href="https://example.com/uploads/3223-2323-4334-32-slajjjq.txt">Download</a></td>
</tr>
<tr>
<td>2</td>
<td>Users</td>
<td>3223-2323-4344-33-slik.txt</td>
<td><a href="https://example.com/uploads/3223-2323-4344-33-slik.txt">Download</a></td>
</tr>
</table>
发布于 2019-05-19 17:25:22
表:
CREATE TABLE `a_files` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Description` varchar(255) DEFAULT NULL,
`filename` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;数据:
INSERT INTO `a_files` (`id`, `Description`, `filename`)
VALUES
(1, 'Information', 'some-file-000-111-222.txt'),
(2, 'Users', 'some-file-000-111-223.txt');代码:
class FileLister {
const UPLOAD_DIR = '/uploads';
public function makeList()
{
$db = [
'host' => '127.0.0.1',
'user' => 'app',
'password' => 'aaaa',
'database' => 'sss',
];
$dsn = "mysql:dbname={$db['database']};host={$db['host']}";
try {
$pdo = new PDO($dsn, $db['user'], $db['password']);
$sql = "SELECT * FROM `a_files`";
$rs = $pdo->query($sql);
foreach($rs as $row) {
// echo print_r($row, true) . PHP_EOL;
echo sprintf("<A href=\"https://sample.com%s/%s\">%s</A>\n",
self::UPLOAD_DIR,
$row['filename'],
$row['Description']);
}
} catch (PDOException $e) {
echo $e->getMessage() . "\n";
exit();
}
}
}
$lister = new FileLister;
$lister->makeList();输出:
<A href="https://sample.com/uploads/some-file-000-111-222.txt">Information</A>
<A href="https://sample.com/uploads/some-file-000-111-223.txt">Users</A>https://stackoverflow.com/questions/56209830
复制相似问题