上传图片或txt并自动添加到网页中 [英] Upload an image or a txt and add it into a web page automatically

查看:24
本文介绍了上传图片或txt并自动添加到网页中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上传器,可以让我将文件上传到服务器.

I have an uploader that make me upload files to the server.

我希望当我上传图片时,该图片会自动显示在网站页面中.例如,如果我有一个乐队的网站,如果我从管理员密码保护的页面上传下一场音乐会的海报,并且自动在页面事件"中我可以看到该海报.

I would like that when I upload an image, this image is automatically visible in a page of the site. Eg, if I have a site of a music band, if I upload from an administrator password protected page the poster of the next concert, and automatically in the page "events" I can see that poster.

同样,如果我上传(从另一个上传者)该音乐会的文本,该文本会出现在活动页面的音乐会描述"div 中.

In the same way if I upload (from another uploader) the text for that concert, this text appears in the event page in a "description of the concert" div.

有可能吗?谢谢!

推荐答案

如果您需要这样做,那么使用数据库并将上传的项目存储在其列中是更好的方法..

If you need to do that, then using the database and store the uploaded items in its coloumn is the better way..

但是要回答你的问题

你可以这样做.

第 1 步:

读取目录中的所有文件

$dir    = 'up/';
$files = scandir($dir);

第 2 步:

做一个 foreach 并将其打印在您的文件中(如果您需要显示图像,这是可能的,并且您可以在那里找到链接)

Do a foreach and print it in your file (If you need to display an image it can be possible and you can have links over there )

$i = 1;
foreach ($files as $key) 
{
    echo $i.' - '.$key.'<hr>';
    echo "<a href='up/".$key."'><img src ='up/".$key."'>".$key."</a>";
    $i++;
}

所以你可以有这样的东西..

So you can have something like this..

<?php
$dir    = 'up/';
$files = scandir($dir);
echo '<h1>List of Uploaded Files</h1> <br><hr>';
$i = 1;
foreach ($files as $key) 
{
    echo $i.' - '.$key.'<hr>';
    echo "<a href='up/".$key."'><img src ='up/".$key."'>".$key."</a>";
    $i++;
}
echo 'End of Files';

?>

注意:

我使用目录名称作为 up 您可以使用任何您自己的名称.

I am using the directory name as up you can have it any name of your own.

如果您将此文件设为 list.php,那么它应该有一个名为 up 的子文件夹,并且您应该在其中包含文件.您可以根据需要包含图像或文本文件.

If you have this file as list.php then it should have a sub folder named as up and you should have files inside it.. You can have image or text file as you need.

更新:

如果你只是想列出文件,那么你只需要回显它

If you just want to list the files, then you just need to echo it

<?php
$dir    = 'up/';
$files = scandir($dir);
echo '<h1>List of Uploaded Files</h1> <br><hr>';
$i = 1;
foreach ($files as $key) 
{
    echo $i.' - '.$key.'<hr>';
    $i++;
}
echo 'End of Files';

?>

这是评估链接

更新 2:

由于 OP 需要有一个锚标记并希望删除目录级别,这是更新后的代码

As the OP needs to have an anchor tag and wants to remove the directory level Here's the updated code

<?php
$dir    = 'up/';
$files = scandir($dir);
echo '<h1>List of Uploaded Files</h1> <br><hr>';
$i = 1;
foreach ($files as $key) 
{
    if ($i>3) 
    {
    $j = $i-3;
    echo $j."<a href='up/".$key."'>".$key."</a><hr>";
    }
    $i++;

}
echo 'End of Files';

?>

这是评估

这篇关于上传图片或txt并自动添加到网页中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆