我的图片文件夹中有几张图片,例如:
User12345_gallery0.png
User12345_profilePic.jpg
User12345_gallery1.png
User12345_gallery2.jpg
User54321_gallery0.png
我尝试做的是将用户传递给我的getimages.php并获取该用户的图片。我之前从未这样做过,所以我在语法上挣扎。这就是我到目前为止所拥有的:
if($_REQUEST['user']){
$ext = array('jpeg', 'png', 'jpg');
$dir = 'images/';
$user = $_REQUEST['user'];
$images = array();
foreach ($ext as $ex){
if (file_exists(strpos($dir.$user."_gallery", "gallery"))) {
//add file to $images???
}
}
}
我想检查文件是否包含用户名和" gallery"的实例。我怎样才能做到这一点?我是在正确的轨道上吗?
答案 0 :(得分:3)
使用glob()
和stripos()
进行此操作..
<?php
if($_REQUEST['user']){
$user = $_REQUEST['user'];
$images = array();
foreach (glob("images/*.{jpg,png,gif}", GLOB_BRACE) as $filename) {
if(stripos($filename,$user)!==false && stripos($filename,'gallery')!==false)
$images[]=$filename;
}
}
答案 1 :(得分:1)
为什么不直接查看目录中的图片名称并返回给您的用户?
$ext = array('jpeg', 'png', 'jpg');
$dir = 'images/';
$user = $_REQUEST['user'];
// the length of your username (for the expression bellow)
$usernamelength = strlen($user);
// read a list of all files into an array
$filesindirarray = scandir($dir);
// loop through the list
foreach($filesindirarray as $filename)
{
// does the filename start with
if(substr($filename,0,($usernamelength+8)) == $user .'_gallery')
{
// fish out the files extension
$fileext = pathinfo($filename, PATHINFO_EXTENSION);
// if the extension of the files is in your list
if(in_array($fileext,$ext))
// add to images
}
}