简体   繁体   中英

Find path dir from mysql in php

I currently saving all my file paths in the database like this

 ID   |      file_path
 ------------------------------------------------------------
  1         home/games/ps3/cod.png
  2         home/err.png
  3         home/games/ps3logo.png
  4         home/games/xboxlogo.png
  5         home/games/pclogo.png
  6         home/games/wiilogo.png
  7         home/msg.png 

I am trying to use php to search all the files and get the files from the dir you selected

$folder_path = "home/games/ps3";

I only want to show all the images in the ps3 folder?

Assuming I understand your question, you should just be able to do a query like:

$query = "SELECT * FROM files WHERE file_path LIKE '".$folder_path."%'";

That will return all rows where the file_path column starts with whatever value the PHP $folder_path variable contains.

Try using a REGEX like so:

$query = "SELECT * FROM files WHERE file_path RLIKE '^".$folder_path."/[^/]*\$'";

The query should look like:

SELECT * FROM files
WHERE file_path RLIKE '^home/games/ps3/[^/]*$'

If you have the path, you can use the 'readdir' function:

http://php.net/manual/en/function.readdir.php

$folder_path = "home/games/ps3";

if ($handle = opendir($folder_path)) {

    while (false !== ($file = readdir($handle))) {
        // Do whatever you want with $file
    }

    closedir($handle);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM