简体   繁体   English

PHP在服务器上查找包含某些字符串的文件

[英]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.

相关问题 PHP - 在变量中查找某些字符串 - PHP - Find certain strings in a variable 用新词替换文件夹中所有 PHP 文件中包含某个词的所有行 - replace all lines which contain a certain word with a new word in all PHP files in a folder regexp,如何查找不包含某个字符的字符串? - regexp, how to find strings that don't contain a certain character? 使用PHP在包含特定字符串的目录中搜索文件列表 - Use PHP to search a list of files within directorys that contain a certain string 正则表达式在2个字符串之间找到一个字符串,该字符串位于2个字符串之间,其中子字符串不能包含单词 - Regex to find a string between 2 strings that are between 2 strings of which the substrings can't contain a word 使用PHP加载包含分隔符的SQL文件 - Loading SQL files which contain deliminators using PHP 从数组中删除与某个单词匹配的所有字符串 (php) - Remove all strings from an array which match a certain word (php) PHP-(Regex)-查找不包含-num X num的文件 - PHP - ( Regex ) - Find Files that do not contain -num X num 使用PHP搜索文件以查找某些文件并快速删除它们 - using PHP to search files to find certain files and delete them fast 在HTML(Wordpress)中包含HTML和PHP文件的安全HTML文件或安全文件夹 - Secure HTML Files or Secure Folder Which Contain HTML and PHP files in PHP (Wordpress)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM