php - 使用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新手,所以如果你能保持简单,那将会很有帮助。提前致谢。
解决方案
我假设您的 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>
推荐阅读
- c# - 表行中的 asp-route 未传递 id
- php - 做一个 Preg_match 然后单独检查每个选项
- azure - AKS 入站规则会定期重置
- google-apps-script - 如何将增量字段添加到 Google 表单
- swift - UIrefreshcontrol 不会使用分页重新加载新项目
- .net - 在 linq 中引用匿名类型成员的问题
- mysql - Rails - 无法连接到 Digital Ocean Managed MySQL
- mysql - 创建 MySQL 触发器以插入第二个表
- ios - 在 [BluetoothDevice classicDevice] 无法识别的选择器上寻找崩溃源
- android - 无法阻止android中的所有http连接