【问题标题】:File_exists function is not workingFile_exists 功能不起作用
【发布时间】:2015-11-09 06:11:06
【问题描述】:

由于问题出在 file_exists() 上,并且该函数似乎应该转到根目录,因此当您确定 file_exists 函数上的文件位置时,您使用基本 url 声明它,因为它只测试文件来自服务器而不是来自 url 的位置:

错误代码

    $dir = base_url()."assets/produits/";

    foreach ($rows as $row) {
        $nom = $row->nShape;
        $type = array(".jpeg", ".jpg");

        foreach ($type as $ext) {
            echo "<br>I'm in the foreach loop <br>";
            $file_name = $dir . $nom . $ext;

            if (file_exists($file_name)) {
               echo "I'm in the file_exists function<br>";
               $img_src = $file_name;
            } else {
                echo "I'm in the else statement <br>";
                echo $file_name."\n";
                $img_src = $dir . "none.png";
            }
        }
    }

问题是全名在那里,但它总是认为它不存在,我做了一些回声来检查代码是否到达,这是一个屏幕截图:

知道http://localhost/dedax_new/assets/produits/2052.jpeg存在于服务器中。

解决办法:

// set the default to the no find image
$img_src = base_url() . "assets/produits/none.png";

foreach ($rows as $row) {
    $nom = $row->nShape;
    $type = array(".jpeg", ".jpg");

    foreach ($type as $ext) {
        $file_name =  $nom . $ext;

        if (file_exists("./assets/produits/".$file_name)) {
           $img_src = base_url() . 'assets/produits/'.$file_name;;
           // we found a file that exists 'get out of dodge'
           break;
        }
    }

提前感谢所有贡献者。

【问题讨论】:

  • 看起来您正在查找扩展名为 .jpeg 的文件,但没有找到扩展名为 .jpg 的文件。问题是您正在退出 foreach 并将您的路径设置为最后一个测试。
  • @BigScar 我该如何解决这个问题,因为我需要给所有的扩展发短信
  • 如果你想测试所有的扩展,也许最好将结果存储在不同的变量或数组中,否则你只会得到现在的最后一个结果。

标签: php


【解决方案1】:

file_exists 在文件系统上工作,而不是在网络上。您使用的是网址而不是文件位置。

此外,在您当前的循环中,第一次循环使用第一个 ext 可以找到文件,然后无法找到具有第二个 ext 的文件并假设没有找到文件。

所以试试这个

$dir = "dedax_new/assets/produits/";

// set the default to the no find image
$img_src = $dir . "none.png";

foreach ($rows as $row) {
    $nom = $row->nShape;
    $type = array(".jpeg", ".jpg");

    foreach ($type as $ext) {
        echo "<br>I'm in the foreach loop <br>";
        $file_name = $dir . $nom . $ext;

        if (file_exists($file_name)) {
           echo "I'm in the file_exists function looking for $filename";
           $img_src = $file_name;
           // we found a file that exists 'get out of dodge'
           break;
        }
    }
}

另外一个常见的错误是不尊重文件系统的大小写敏感性。确保您尊重文件位置的大小写敏感,因为 URL 通常与文件系统的配置不​​匹配。

【讨论】:

【解决方案2】:

问题出在 directory file_exists 函数测试文件位置,它无法测试 url 这里是代码:

// set the default to the no find image
$img_src = base_url() . "assets/produits/none.png";

foreach ($rows as $row) {
    $nom = $row->nShape;
    $type = array(".jpeg", ".jpg");

    foreach ($type as $ext) {
        $file_name =  $nom . $ext;

        if (file_exists("./assets/produits/".$file_name)) {
           $img_src = base_url() . 'assets/produits/'.$file_name;;
           // we found a file that exists 'get out of dodge'
           break;
        }
    }

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 2017-03-11
    • 2011-11-05
    • 2014-08-05
    • 2010-10-31
    • 2010-11-20
    • 2017-11-17
    • 2012-07-08
    • 1970-01-01
    相关资源
    最近更新 更多