Php 如何从服务器动态加载图像并显示它们
我的服务器上存储了图像,即我的服务器上有一个目录,例如: $uploadsDirectory=$\u服务器['DOCUMENT\u ROOT']$目录(u self)上传的_文件/' 我需要从目录中加载每个图像,并在以下html中显示:Php 如何从服务器动态加载图像并显示它们,php,jquery,html,Php,Jquery,Html,我的服务器上存储了图像,即我的服务器上有一个目录,例如: $uploadsDirectory=$\u服务器['DOCUMENT\u ROOT']$目录(u self)上传的_文件/' 我需要从目录中加载每个图像,并在以下html中显示: <div class="image-set"> <a class="example-image-link" href="images/demopage/1.jpg" data-lightbox="example-
<div class="image-set">
<a class="example-image-link" href="images/demopage/1.jpg" data-lightbox="example-set" data-title="Click the right half of the image to move forward."><img class="example-image" src="images/demopage/1.jpg" alt=""/></a>
<a class="example-image-link" href="images/demopage/2.jpg" data-lightbox="example-set" data-title="Or press the right arrow on your keyboard."><img class="example-image" src="images/demopage/2.jpg" alt="" /></a>
<a class="example-image-link" href="images/demopage/3.jpg" data-lightbox="example-set" data-title="The next image in the set is preloaded as you're viewing."><img class="example-image" src="images/demopage/3.jpg" alt="" /></a>
<a class="example-image-link" href="images/demopage/4.jpg" data-lightbox="example-set" data-title="Click anywhere outside the image or the X to the right to close."><img class="example-image" src="images/demopage/4.jpg" alt="" /></a>
<a class="example-image-link" href="images/demopage/5.jpg" data-lightbox="example-set" data-title="Click the right half of the image to move forward."><img class="example-image" src="images/demopage/5.jpg" alt=""/></a>
<a class="example-image-link" href="images/demopage/6.jpg" data-lightbox="example-set" data-title="Or press the right arrow on your keyboard."><img class="example-image" src="images/demopage/6.jpg" alt="" /></a>
<a class="example-image-link" href="images/demopage/7.jpg" data-lightbox="example-set" data-title="The next image in the set is preloaded as you're viewing."><img class="example-image" src="images/demopage/7.jpg" alt="" /></a>
<a class="example-image-link" href="images/demopage/8.jpg" data-lightbox="example-set" data-title="Click anywhere outside the image or the X to the right to close."><img class="example-image" src="images/demopage/8.jpg" alt="" /></a>
<a class="example-image-link" href="images/demopage/9.jpg" data-lightbox="example-set" data-title="Click the right half of the image to move forward."><img class="example-image" src="images/demopage/9.jpg" alt=""/></a>
<a class="example-image-link" href="images/demopage/10.jpg" data-lightbox="example-set" data-title="Or press the right arrow on your keyboard."><img class="example-image" src="images/demopage/10.jpg" alt="" /></a>
<a class="example-image-link" href="images/demopage/11.jpg" data-lightbox="example-set" data-title="The next image in the set is preloaded as you're viewing."><img class="example-image" src="images/demopage/11.jpg" alt="" /></a>
<a class="example-image-link" href="images/demopage/12.jpg" data-lightbox="example-set" data-title="Click anywhere outside the image or the X to the right to close."><img class="example-image" src="images/demopage/12.jpg" alt="" /></a>
<a class="example-image-link" href="images/demopage/13.jpg" data-lightbox="example-set" data-title="Click anywhere outside the image or the X to the right to close."><img class="example-image" src="images/demopage/13.jpg" alt="" /></a>
</div>
img标签可以动态增加,有人能帮我吗?
像这样的
function getImageUrls($uploadsDirectory)
{
$urls = array();
foreach(scandir($uploadsDirectory) as $filename) {
if($filename != '.' AND $filename != '..') {
$pathinfo = pathinfo($filename);
if(in_array($pathinfo['extension'], array('jpg', 'png', 'gif'))) {
$urls[] = $uploadsDirectory.$filename;
}
}
return $urls;
}
// ----------------------------------
foreach(getImageUrls($uploadsDirectory) as $src) {
echo '<a class="example-image-link" href="'.$src.'" data-lightbox="example-set"><img class="example-image" src="'.$src.'" alt=""/></a>'
}
函数getImageUrls($uploadsDirectory)
{
$url=array();
foreach(scandir($uploadsDirectory)作为$filename){
如果($filename!='.'和$filename!='.'){
$pathinfo=pathinfo($filename);
if(在数组中($pathinfo['extension'],数组('jpg','png','gif')){
$URL[]=$uploadsDirectory.$filename;
}
}
返回$URL;
}
// ----------------------------------
foreach(getImageUrls($uploadsDirectory)作为$src){
回声“
}
您可以尝试获取图像文件。使用php中的函数
<div class="image-set">
<?php foreach (glob('images/demopage/*') as $filename) { ?>
<img class="example-image" src="images/demopage/<?php echo basename($filename); ?>" alt=""/>
<?php } ?>
</div>
“alt=”“/>
首先,您需要在目录中扫描所需的所有图像文件:
$imageFiles = [];
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/';
// check folder exists
if (is_dir($uploadsDirectory))
{
for (scandir($uploadsDirectory) as $file)
{
// include only desired file types
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == 'jpg' || $ext == 'png' || $ext == 'gif')
{
$imageFiles[] = $file;
}
}
}
现在,如果您已设置URL重写,将URL“images/demopage/”链接到存储图像的目录(您可以将其限制为仅接受图像请求),则可以在HTML模板中执行以下操作:
<div class="image-set">
<?php
foreach ($imageFiles as $image)
{
echo '<a class="example-image-link" href="images/demopage/' . $image . '" data-lightbox="example-set" data-title="Click anywhere outside the image or the X to the right to close.">';
echo '<img class="example-image" src="images/demopage/' . $image . '" alt="" />';
echo '</a>';
}
?>
</div>
简单循环是什么意思?是的,如果你的意思是我需要在一个目录中显示所有图像,那么我必须循环所有图像。你做一个循环,为文件夹中的每个图像生成一行html。因此,编写一些代码来生成html…使用循环获取图像结果,一旦你将它们放在一起,如果你有问题返回并使用您尝试过的代码再次询问。祝您好运。您忘记从函数返回并结束foreach。没有显示为$src的内容,表示我得到的url数组为空