[英]PHP find files on server which contain certain strings
I have a couple images in my images folder for example: 例如,我的图像文件夹中有几张图像:
User12345_gallery0.png
User12345_profilePic.jpg
User12345_gallery1.png
User12345_gallery2.jpg
User54321_gallery0.png
What I'm trying to do is pass the user to my getimages.php and get that user's images. 我想做的就是将用户传递到我的getimages.php并获取该用户的图像。 I've never done this before so I'm struggling with the syntax. 我之前从未做过此事,所以我在语法上苦苦挣扎。 Here's what I have so far: 这是我到目前为止的内容:
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???
}
}
}
I want to check if the file contains the username and the instance of "gallery". 我想检查文件是否包含用户名和“ gallery”实例。 How can I do that? 我怎样才能做到这一点? Am I on the right track? 我在正确的轨道上吗?
Use glob()
and stripos()
for this.. stripos()
使用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;
}
}
Why not just look at the pictures names in the directory and return the ones for your user ? 为什么不只查看目录中的图片名称,然后将其返回给您的用户呢?
$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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.