【问题标题】:How to check if file name exist without know the file format php [duplicate]如何在不知道文件格式php的情况下检查文件名是否存在[重复]
【发布时间】:2019-12-24 08:52:19
【问题描述】:
if(file_exists("/img/korica/Design_1.*"))
    echo "file exist";
else
    echo "there are no files with that name"

【问题讨论】:

标签: php file


【解决方案1】:

你可以使用scandir();

$files = scandir("/path/to/directory");
print_r($files);

另一个例子:

<?php

$files = array_filter(scandir('/path/to/directory'), function($item) {
    return $item !== '.' && $item !== '..';
});

foreach ($files as $filename)
{
    if(file_exists("path/to/directory/$filename"))
    {
        echo "file exist\n";
    }
    else
    {
        echo "file doesn't exist\n";
    }
}

【讨论】:

    【解决方案2】:

    你只需要使用:glob()

    $existence= glob("./Yourdir/filename.*");
    

    然后检查$existence是否有东西!

    它也可以与bash-like 大括号扩展一起使用:

    glob("./uploads/filename.{jpg,jpeg,png,gif}", GLOB_BRACE).
    

    【讨论】:

      猜你喜欢
      • 2013-08-31
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 2012-04-18
      • 2011-09-23
      相关资源
      最近更新 更多