PHP数组搜索文件类型并查看文件是否存在


PHP array search for file type and see if file exist

<?php
$rn = $_GET['rn'];
$file_exts = array("jpg", "jpeg", "png");
$upload_exts = end(explode(".", $rn));
#$images/$rn
if (in_array($upload_exts, $file_exts)) {
  #if (in_array(file_exists($haystack), $needle) {
  if (array_search($upload_exts, $file_exts/*file_exists("c:/xampp'htdocs'php/images/" .*/)) {
     $str_img = str_replace(' ', '_', $rn);
  } else {
     $str_img = 'thumb.png';
  }
}
echo $str_img;
?>

好吧,我有这个代码,但我无法得到我想要的结果
我想看看是否存在一个文件,使用array_*和其他类似file_exists的函数
我的问题是,当我想添加新的文件类型时,我只会在array字段中键入它,它会搜索它,如果找到了图像/文件,就会打印出文件名+文件类型例如google-img.jpg,否则它将打印thumb.png

将file_exists与目录名(__file__(一起使用:

$rn = $_GET['rn'];
$file_exts = array("jpg", "jpeg", "png");
$upload_exts = end(explode(".", $rn));
function exists( $file ) {
$file_exts = array("jpg", "jpeg", "png");
$upload_exts = end(explode(".", $file));
 if( in_array($upload_exts, $file_exts) && file_exists( dirname(__FILE__) . '/' . $file ) )
  return $file;
 foreach($file_exts as $a)
 if( file_exists( dirname(__FILE__) . '/' . $file . '.' . $a) )
  return $file.'.'.$a;
 return false;
 }
 if( exists($_GET['rn']) )
   $str_img = str_replace(" ", "_", exists($_GET['rn']));
 else
   $str_img = 'thumb.png';
echo $str_img;