[英]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 我正在尝试使用php搜索所有文件并从您选择的目录中获取文件
$folder_path = "home/games/ps3";
I only want to show all the images in the ps3 folder? 我只想显示ps3文件夹中的所有图像?
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. 这将返回file_path列以PHP $folder_path
变量包含的任何值开头的所有行。
Try using a REGEX like so: 尝试像这样使用REGEX:
$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: 如果有路径,则可以使用“ readdir”功能:
http://php.net/manual/en/function.readdir.php http://php.net/manual/zh/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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.