douhuang5331 2019-05-19 16:31
浏览 265
已采纳

如何在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   
  • 写回答

2条回答 默认 最新

  • dsh7551 2019-05-19 17:48
    关注

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)