如何在PHP中创建mySQL数据库及其相应文件(超链接)的文件名HTML表

如何在PHP中创建mySQL数据库及其相应文件(超链接)的文件名HTML表

问题描述:

I have a form that submits a file to the server in the folder uploads and also saves the file name(as stored in the server) as well as other file details in my database. Now what I'm trying to do, is create a HTML table that displays file details as well as the corresponding file(In hyperlink of course).

Is there any way to this? An idea I had is to link the file name to the corresponding file in my uploads/ directory but I have no idea on how to achieve that.

Here is my table uploaded_content where the form data is stored in my database

================================================
id |Description | filename                     |
------------------------------------------------
1  |Information | 3223-2323-4334-32-slajjjq.txt|
2  |Users       | 3223-2323-4344-33-slik.txt   |
================================================

And here is an example of how my uploads/ directory looks like (Notice that their names are saved in my database)

3223-2323-4334-32-slajjjq.txt
3223-2323-4344-33-slik.txt

So now I want to create a HTML table with the following columns

id |Description | filename  |File   

我有一个表单,用于将文件提交到 uploads strong>文件夹中的服务器,还有 保存文件名(存储在服务器中)以及我的数据库中的其他文件详细信息。 现在我要做的是创建一个显示文件详细信息的HTML表以及相应的文件(在超链接中) 当然)。 p>

有什么办法吗? 我的想法是将文件名链接到 uploads / strong>目录中的相应文件,但我不知道如何实现它。 p>

这是 我的表 uploaded_content strong>表单数据存储在我的数据库中 p>

  =================  =============================== 
id |描述|  filename | 
 ----------------------------------------------  -  
1 |信息|  3223-2323-4334-32-slajjjq.txt | 
2 |用户|  3223-2323-4344-33-slik.txt | 
 ====================================  ============ 
 
  code>  pre> 
 
 

以下是我的 uploads / code>目录的示例 喜欢(注意他们的名字保存在我的数据库中) p>

  3223-2323-4334-32-slajjjq.txt 
3223-2323-4344-33-slik.txt \  n 
  code>  pre> 
 
 

现在我想创建一个包含以下列的HTML表格 p>

  id | Description |  filename | File 
  code>  pre> 
  div>

It is not the best way to do it, but i just wanna show you how to do that.

Step 1 - Create mysql DB.

CREATE TABLE `uploaded_content` (
  `id` int(11) NOT NULL,
  `description` text NOT NULL,
  `filename` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Step 2 - Insert data to this 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');

Step 3 - Make config.php:

<?php
    $conn = new PDO('mysql:host=localhost;dbname=stack', 'root', '');
    $q = $conn->query("SELECT * FROM uploaded_content WHERE id");
    $f = $q->fetchAll();
?>

Step 4 - Create index.php with this code:

<?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>

When you finish that, you get html page like this:

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

</div>

  1. Do a query to get the list from the database.
  2. Use that list to build your HTML table.

Table:

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;

Data:

INSERT INTO `a_files` (`id`, `Description`, `filename`)
VALUES
    (1, 'Information', 'some-file-000-111-222.txt'),
    (2, 'Users', 'some-file-000-111-223.txt');

Code:

 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>
",
                    self::UPLOAD_DIR,
                    $row['filename'],
                    $row['Description']);
            }
        } catch (PDOException $e) {
            echo $e->getMessage() . "
";
            exit();
        }
    }
}
$lister = new FileLister;
$lister->makeList();

Output:

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