简体   繁体   中英

File_exists function is not working

Since the problem was on file_exists() and it seems that the function should goes to the root directory, so when you determine the file location on the file_exists function you declare it with the base url because it only tests the file location from the server not from a url :

Wrong Code

    $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";
            }
        }
    }

The problem is that the full name is there but it always treat it as it doesnot exists, I've made some echos to check did code reaches and here's a screeenshot : 问题的屏幕截图

Knowing that the http://localhost/dedax_new/assets/produits/2052.jpeg exists in the server.

Solution :

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

Thanks to all contributors in advance.

file_exists works on the file system and not over the web. You are using a web address rather than a file location.

Also with your current loop it is possible to find the file the first time round the loop using the first ext and then fail to find the file with the second ext and assume there is no file to be found.

So try this

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

Also an other common mistake is to not respect the case sensitivity of the file system. Make sure that you respect the case sensitivity of your file's location as the URL often does not match the file system's configuration.

The problem was in the directory file_exists function tests a file location it can't test a url here is the code :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM