php 自动创建目录
So far our PHP gallery examples have required us to write new HTML code in order to present an image in the gallery, or at least the thumbnail thereof. This means that every time that we upload a new image to the site, we would have to write more HTML to add it to our page.
到目前为止,我们的PHP画廊示例要求我们编写新的HTML代码 ,以便在画廊中显示图像或至少显示其缩略图。 这意味着,每次我们将新图像上传到网站时,我们都必须编写更多HTML才能将其添加到页面中。
But our random image lesson showed that we can read images from a directory… so it follows that we should be able to automate the entire process of presenting a gallery of images with PHP.
但是我们的随机图像课程表明,我们可以从目录中读取图像…因此,我们应该能够自动化使用PHP呈现图像库的整个过程。
Let’s take the core of the random image code:
让我们看一下随机图像代码的核心:
<?php $imagedir = dir('assets/images');
$count = 1;
$pattern="/\.(gif|jpg|jpeg|png)$/";
while(($file = $imagedir->read())) {
if (preg_match($pattern, $file)) {
$imagearray[$count] = $file;
$count++;
}
} ?>
This creates our array as before. But this time, we’re not getting a random image from $imagearray
, but the first image. Doing that alone would be easy:
像以前一样创建数组。 但是这一次,我们没有从$imagearray
获得随机图像,而是第一张图像。 一个人做会很容易:
<img src="assets/images/<?php echo $imagearray[1]; ?>" alt>
Rather than leaving the alt
attribute value blank, you should fill it with something. Ideally, you could use a variation of the string manipulation techniques I introduced in the self-made pages lesson to create values based on the file name of the image.
而不是离开alt
属性值为空,你应该用什么填充它。 理想情况下,您可以使用我在自制页面课程中介绍的各种字符串处理技术来基于图像的文件名创建值。
We want to make what selects the image a variable, so let’s create one called $pic
and set it to a default value of 1 if no value for it exists:
我们要使选择图像的变量成为变量,所以我们创建一个称为$pic
的变量,如果不存在它的值,则将其设置为默认值1 :
<?php if (!$pic) { $pic = 1; } ?>
<img src="assets/images/<?php echo $imagearray[$pic]; ?>" alt>
Now we want to allow the user to change the value of $pic
. The easiest way to do that is via clicking on a link which passes a GET
variable back to the page. First, we’ll add a line of code to the section above:
现在,我们要允许用户更改$pic
的值。 最简单的方法是单击链接,该链接将GET
变量传递回页面。 首先,我们将在上面的部分中添加一行代码:
<?php $pic = $_GET['pic'];
if (!$pic) { $pic = 1; } ?>
<img src="assets/images/<?php echo $imagearray[$pic]; ?>" alt>
Then create our link, which will increment whatever value $pic
has by 1:
然后创建我们的链接,它将使$pic
任何值增加1 :
<a href="?pic=<?php echo ($pic + 1); ?>">Next</a>
This will advance our images through the array with each click on the Next link. (As always, one of the easiest ways to understand this is to look at the source code delivered from the browser). There’s just one problem: we can keep clicking the Next button past the actual number of images we have.
每次单击“ 下一步”链接,这将使图像通过阵列前进。 (一如既往,最简单的方法之一就是查看浏览器提供的源代码)。 仅有一个问题:我们可以在实际拥有的图像数之后继续单击“ 下一步”按钮。
接口设计规则第1条 (Interface Design Rule No. 1)
If an interface element can’t be used, or if it isn’t appropriate at a certain point, it shouldn’t be seen. To solve this issue, we’ll make the appearance of the Next button conditional:
如果不能使用接口元素,或者在某个时候它不合适,则不应看到它。 要解决此问题,我们将使“ 下一步”按钮的出现成为条件:
<?php if ($pic < $count - 1) { ?>
<a href="?pic=<?php echo ($pic + 1); ?>">Next</a>
?php } ?>
翻译自: https://thenewcode.com/214/Create-an-Automatic-Portfolio-With-PHP
php 自动创建目录