File_exists函数不起作用

拉玛尼·赛义夫·穆卢克

由于问题出在file_exists()上,并且似乎该函数应该转到根目录,因此,当您确定file_exists函数上的文件位置时,请使用基本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";
            }
        }
    }

问题是全名存在,但始终将其视为不存在,我做了一些回声以检查代码是否到达,这是一个screeenshot: 问题的屏幕截图

知道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;
        }
    }

预先感谢所有贡献者。

拉玛尼·赛义夫·穆卢克

问题在于该directory file_exists功能测试文件位置,因此无法测试网址,这里是代码:

// 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;
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章