dsvf46980 2014-04-26 23:06
浏览 18
已采纳

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

<?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;
?>

Okay so I have this code, but I can't get the result I'm looking for.
I want to see if a file exist, using array_* and other functions like file_exists.
What my problem is, when I want to add a new file type, I will just type it in the array field, and it will search for it, if an image/file was found, the file name + file type will be printed out e.g google-img.jpg, else it whould print thumb.png.

  • 写回答

1条回答 默认 最新

  • dsfsd43523 2014-04-26 23:26
    关注

    Use file_exists with dirname(__ 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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路