首页 > 解决方案 > 使用php显示文件夹中的图像

问题描述

我在文件夹中有一些图像,在文件中有图像的名称。我想根据单击的按钮显示图像。

    <!DOCTYPE html>
    <html>
    <head>
        <title>Your Album</title>
    </head>
    <body>
        <form method="post">
            <input type="submit" name="first" value="FIRST">    
            <input type="submit" name="previous" value="PREVIOUS">
            <input type="submit" name="next" value="NEXT">
            <input type="submit" name="last" value="LAST">
            <input type="submit" name="dele" value="DELETE">
        </form>
        <br>

    <?php
    $fp = fopen("imgname.csv","r");
    $line = fread($fp,filesize("imgname.csv"));
    $item = explode("\n", $line);
    $count;
    $sizecsv = sizeof($item);   
    if(isset($_POST['next'])) {
        $count++;
        echo "<img src='images/$item[$count]' width='250px'>";
        echo "<br>","Image No: ",$count+1;
    }
    if(isset($_POST['previous'])) {
        $count--;
        echo "<img src='images/$item[$count]' width='250px'>";
        echo "<br>","Image No: ",$count+1;
    }
    if(isset($_POST['first'])) {
        $count = 0;
        echo "<img src='images/$item[$count]' width='250px'>";
        echo "<br>","Image No: ",$count+1;
    }
    if(isset($_POST['last'])) {
        $count = $item[$sizecsv-1];
        echo "<img src='images/$item[$count]' width='250px'>";
        echo "<br>","Image No: ",$count+1;      
    }

?>
</body>
</html>

这是我的代码。我将图像名称存储在一个数组中,并尝试根据单击的按钮来操作索引。这适用于“FIRST”按钮,但不适用于其他任何东西。我不明白这个问题。是否每次单击按钮时页面都会重新加载并且 $count 的值会丢失。请给出解决方案。我也是php新手,所以如果你能保持简单,那将会很有帮助。提前致谢。

标签: phpforms

解决方案


我假设您的 csv 文件数据是由换行符分隔的文件名数组,如下所示:

some-img-01.jpg
img-02.jpg
img-05.jpg
....

请检查以下代码。我已经重组了代码以在每个页面加载之间传递状态(如果存在于上一篇文章中)。在这里,我将其设计为循环-如果到达末尾,则从第一个开始,然后单击下一个,如果从第一个位置单击上一个,则跳转到最后一个。您可以通过注释掉相应的行来禁用此功能(检查注释)。

<?php
$current_index = isset($_POST['current_index'])? intval($_POST['current_index']) : 0;

$fp = fopen('imgname.csv', 'r');
$line = fread($fp, filesize('imgname.csv'));
fclose($fp);
$item = explode("\n", $line);
$sizecsv = sizeof($item);

$current_index = isset($_POST['current_index'])? intval($_POST['current_index']) : 0;

if ($current_index >= $sizecsv) $current_index = ($sizecsv - 1);
if ($current_index < 0) $current_index = 0;

if (isset($_POST['next'])) {
    $current_index++;
    // If reached end of the list, then clicked next, the index starts from firt again.
    // If you dont need this feature, then can comment the following line.
    if ($current_index >= $sizecsv) $current_index = 0;
}
if (isset($_POST['previous'])) {
    $current_index--;
    // If the indexin first image, but clicked the Previous button, then the index goes to last element.
    // If you dont need this feature, then can comment the following line.
    if ($current_index < 0) $current_index = ($sizecsv - 1);
}
if (isset($_POST['first'])) $current_index = 0;
if (isset($_POST['last'])) $current_index = ($sizecsv - 1);
?><!DOCTYPE html>
<html>
<head>
    <title>Your Album</title>
</head>
<body>
    <form method="post">
        <input type="text" name="current_index" value="<?php echo $current_index;?>"/>
        <input type="submit" name="first" value="FIRST"/>    
        <input type="submit" name="previous" value="PREVIOUS"/>
        <input type="submit" name="next" value="NEXT"/>
        <input type="submit" name="last" value="LAST"/>
        <input type="submit" name="dele" value="DELETE"/>
    </form>
    <br>

    <img src="images/<?php echo $item[$current_index];?>" width="250px"/>
    <br>, Image No: <?php echo ($current_index + 1);?>
</body>
</html>

推荐阅读