在html中使用php将图像添加到幻灯片中

时间:2017-01-02 22:48:54

标签: php html

我正在尝试在我当地的足球俱乐部调整电视屏幕的节目。我已经使网站工作,但现在我想让它更加用户友好。

具体来说,我想让网站正常工作,以便有人可以将图像添加到某个文件夹,幻灯片显示将使用文件夹中的所有图像。

代码的相关部分如下;请注意图像是硬编码的:

<html>
    <head>
        <script type="text/javascript" src="js/tabs.js"></script>
        <script src="js/jquery.js"></script>
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <script type="text/javascript" src="js/slider.js"></script>
    </head>
    <body>
        <div class="corner_1">
            <?php include("inc/script.php"); ?>
        </div>
        <div class="slider" id="slider">
            <ul>
                <li style="background-image:     url('http://homedir.com/images/IMG_4947.JPG'); no-repeat center center fixed; 
   background-repeat: no-repeat;
      background-size: 100% 100%;
    background-position: center top;
    background-attachment: fixed;">
                </li>
                <li style="background-image:     url('http://homedir.com/images/IMG_4939.JPG'); no-repeat center center fixed; 
   background-repeat: no-repeat;
      background-size: 100% 100%;
    background-position: center top;
    background-attachment: fixed;">
                </li>
                <li style="background-image:     url('http://homedir.com/images/IMG_4922.JPG'); no-repeat center center fixed; 
   background-repeat: no-repeat;
      background-size: 100% 100%;
    background-position: center top;
    background-attachment: fixed;">
                </li>
            </ul> 
        </div>
    </body>
    </html>

我尝试用PHP中的glob()和echo函数创建一些东西,但是无法使它工作。

1 个答案:

答案 0 :(得分:0)

您可以轻松地执行scandir()

# I am just going to use this to clean up the html, you don't have to do this...
$style = array(
    'background: no-repeat center center fixed',
    'background-repeat: no-repeat',
    'background-size: 100% 100%',
    'background-position: center top',
    'background-attachment: fixed'
);
$style = implode('; ',$style);
# I use a define('ROOT_DIR',__DIR__) in a root-based config file to get the full path
$path = '/var/www/vhosts/domain/images';
# Scan the directory
$dir  = scandir($path);
# Loop through files in directory
foreach($dir as $file) {
    # Use our root path to make sure file exists
    # (there are . and .. that will be in the array
    if(!is_file($path.'/'.$file))
        continue;
?>
        <li style="background-image: url('http://homedir.com/images/<?php echo $file ?>'); <?php echo $style ?>">
        </li>
<?php
}