[英]Display all .jpg images from a specific location
我有两个php文件,一个叫employeeDirectory.php,另一个叫api.php。 我正在尝试使用api.php获取所有图像,然后将其显示在employeeDirectry.php上。
employeeDirectory.php
<div class="container">
<img src="<?php include 'api.php';
echo getEmployees($images); ?>"/><br />
</div>
api.php
function getEmployees($images) {
$directory = "/employeedirectory/";
$images = glob($directory."*.jpg");
foreach($images as $image){
echo $image;
}
return $images;
使用以下内容:
employeeDirectory.php
<?php include_once ('./api.php'); ?>
...
<div class="container">
<?php
$images = getEmployees();
foreach ($images as &$value)
{
echo '<img src="'.$value.'"><br />';
unset($value);
}
?>
</div>
api.php
function getEmployees()
{
$directory = "/employeedirectory/";
$images = scandir($directory);
foreach($images as $imagePath)
{
if (strpos($imagePath, '.jpg') !== false)
{
$images[] = $imagePath;
}
}
return $images;
}
我能做的最简单的方法
api.php
function showJpgs($dir, $bet = '') {
$img = glob($dir.'*.jpg');
foreach ($img as &$i)
$i = '<img src="'.$i.'" alt=""/>';
echo implode($bet, $img);
}
employeeDirectory.php
<div class="container">
<?php showJpgs('images/', '<br/>'); //Insert what you want to be between the images where is '<br/>'
?>
</div>
更改
echo $image;
至
$returnimages .= "<img src='$image'>";
然后
return $returnimages;
这不是更好的代码吗?
<?php
include 'api.php'; ?>
<div class="container">
<?php getEmployees(); ?>
</div>
api.php:
<?php
function getEmployees() {
$directory = "/employeedirectory/";
$images = glob($directory."*.jpg");
foreach($images as $image){
echo "<img src=\"".$image."\"/><br />";
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.