检查目录中的文件名是否作为数据库中的值存在

时间:2022-05-03 12:19:21

I have a bunch of .png's in a directory that are named the same as the values in the database. I would like to do a check if the filename (without '.png') exists in the database or not.

我在一个目录中有一堆.png,其名称与数据库中的值相同。我想检查数据库中是否存在文件名(不带'.png')。

At the moment I am using this code to display the flags those are in the database

目前我正在使用此代码显示数据库中的标志

<?php
  $result = mysql_query("SELECT `country` FROM `countries`");
  while($row = mysql_fetch_array($result)) {;
?>
  <img src="images/countries/<?php echo $row['country']?>.png" />
<?php }; ?>

Since there are only 3 countries in the column it's displayed as: 检查目录中的文件名是否作为数据库中的值存在

由于列中只有3个国家/地区,因此显示为:

I would like to display the flags that are not in the database as well, but then in greyscale.

我想显示数据库中没有的标志,但是然后是灰度。

I came up wit this code to get all the flags from the directory

我想出了这段代码来获取目录中的所有标志

<?php
  $directory = "images/countries/";
  $images = glob($directory . "*.png");

  foreach($images as $image)
  {
    echo '<image src="'.$image.'" class="flaginactive"/>'; //show all flags as inactive
    echo basename($image, '.png'); //get file name with .png
  }
?>

But somehow I am stuck and clueless how I could get them both in an if statement.

但不知何故,我陷入困境,无法如何在if声明中得到它们。

Can someone advise me how I can solve this the best way. I am aware I am using the old mySQL functions.

有人可以告诉我如何以最佳方式解决这个问题。我知道我正在使用旧的mySQL函数。

3 个解决方案

#1


2  

There are many ways to achieve this. I will relate one.

有很多方法可以实现这一目标。我将介绍一个。

First load the names in the database in to an array. Then check the existence of the enumerated file names of the directory in the array to decide the class of the element.

首先将数据库中的名称加载到数组中。然后检查数组中目录的枚举文件名是否存在,以确定该元素的类。

Elements are shown inactive if a file in the directory is not found in the database.

如果在数据库中找不到目录中的文件,则元素将显示为非活动状态。

<?php

  $directory = "images/countries/";

  //Lets save all the file names in the database to an array 
  $dbImages = array();

  $result = mysql_query("SELECT `country` FROM `countries`");
  while($row = mysql_fetch_array($result)) {

     array_push($dbImages, $directory . $row['country'] . '.png'); 

  }



  //Lets go through all the files in the directory and see if they are in the $dbImages array    
  $images = glob($directory . "*.png");

  foreach($images as $image)
  {
    //Decide the class attribute based on the existence of the file name in $dbImages array

    if (in_array($image, $dbImages))
       $classAttribute = '';
    else
       $classAttribute = 'class="flaginactive"' 

    echo '<image src="'.$image.'" ' . $classAttribute . ' />'; 

  }


?>

#2


-1  

You can file_exists() function like this

你可以像这样使用file_exists()函数

<?php
         $directory = "images/countries/";
         $images = glob($directory . "*.png");

       foreach($images as $image)
       {
          if(file_exists($_SERVER['DOCUMENT_ROOT'] . $directory . $image . '.png')) //Files exists goes here
         {
            echo '<image src="'.$image.'" class="flaginactive"/>';            //show all flags as inactive
            echo basename($image, '.png'); //get file name with .png
           }
       }
?>

#3


-1  

Loop through the flags in the database and store them in an array. Then while looping through the flags in the directory, check if the file is in the array. (Assuming the file in the directory has the same name in the database which doesn't seem to be true for your case. My answer will require some tweaking.)

循环遍历数据库中的标志并将它们存储在一个数组中。然后在循环遍历目录中的标志时,检查文件是否在数组中。 (假设目录中的文件在数据库中具有相同的名称,对于您的情况似乎不是真的。我的答案需要进行一些调整。)

<?php
$result = mysql_query("SELECT `country` FROM `countries`");

$selectedFlags = array();

while($row = mysql_fetch_array($result)) {
  $selectedFlags[] = $row['country'];
}

$directory = "images/countries/";
$images = glob($directory . "*.png");

foreach($images as $image) {
  echo '<image src="'.$image . '"';
  if (!in_array($image, $selectedFlags))
    echo ' class="flaginactive"';
  echo ' />';
  // not sure why the next line is here since your sample image didn't have it but I'll leave it in
  echo basename($image, '.png'); //get file name with .png
}
?>

P.S. you don't need semicolons(;) after curly braces ({)

附:花括号({)后你不需要分号(;)

#1


2  

There are many ways to achieve this. I will relate one.

有很多方法可以实现这一目标。我将介绍一个。

First load the names in the database in to an array. Then check the existence of the enumerated file names of the directory in the array to decide the class of the element.

首先将数据库中的名称加载到数组中。然后检查数组中目录的枚举文件名是否存在,以确定该元素的类。

Elements are shown inactive if a file in the directory is not found in the database.

如果在数据库中找不到目录中的文件,则元素将显示为非活动状态。

<?php

  $directory = "images/countries/";

  //Lets save all the file names in the database to an array 
  $dbImages = array();

  $result = mysql_query("SELECT `country` FROM `countries`");
  while($row = mysql_fetch_array($result)) {

     array_push($dbImages, $directory . $row['country'] . '.png'); 

  }



  //Lets go through all the files in the directory and see if they are in the $dbImages array    
  $images = glob($directory . "*.png");

  foreach($images as $image)
  {
    //Decide the class attribute based on the existence of the file name in $dbImages array

    if (in_array($image, $dbImages))
       $classAttribute = '';
    else
       $classAttribute = 'class="flaginactive"' 

    echo '<image src="'.$image.'" ' . $classAttribute . ' />'; 

  }


?>

#2


-1  

You can file_exists() function like this

你可以像这样使用file_exists()函数

<?php
         $directory = "images/countries/";
         $images = glob($directory . "*.png");

       foreach($images as $image)
       {
          if(file_exists($_SERVER['DOCUMENT_ROOT'] . $directory . $image . '.png')) //Files exists goes here
         {
            echo '<image src="'.$image.'" class="flaginactive"/>';            //show all flags as inactive
            echo basename($image, '.png'); //get file name with .png
           }
       }
?>

#3


-1  

Loop through the flags in the database and store them in an array. Then while looping through the flags in the directory, check if the file is in the array. (Assuming the file in the directory has the same name in the database which doesn't seem to be true for your case. My answer will require some tweaking.)

循环遍历数据库中的标志并将它们存储在一个数组中。然后在循环遍历目录中的标志时,检查文件是否在数组中。 (假设目录中的文件在数据库中具有相同的名称,对于您的情况似乎不是真的。我的答案需要进行一些调整。)

<?php
$result = mysql_query("SELECT `country` FROM `countries`");

$selectedFlags = array();

while($row = mysql_fetch_array($result)) {
  $selectedFlags[] = $row['country'];
}

$directory = "images/countries/";
$images = glob($directory . "*.png");

foreach($images as $image) {
  echo '<image src="'.$image . '"';
  if (!in_array($image, $selectedFlags))
    echo ' class="flaginactive"';
  echo ' />';
  // not sure why the next line is here since your sample image didn't have it but I'll leave it in
  echo basename($image, '.png'); //get file name with .png
}
?>

P.S. you don't need semicolons(;) after curly braces ({)

附:花括号({)后你不需要分号(;)