0

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 : Screen shot of the problem

Knowing that the http://localhost/dedax_new/assets/produits/2052.jpegexists 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.

  • Looks like you're finding the file with the extension .jpeg and not finding it with the extension .jpg. The problem is you're exiting the foreach with your path set to the last test. – BigScar Aug 16 '15 at 14:52
  • @BigScar and how can I fix this, because I need to text all the extensions – Rahmani Seif El Moulouk Aug 16 '15 at 14:55
  • If you want to test all your extensions, maybe it would be better to store the results in different variables or an array, otherwise you just get the last result as it stands now. – BigScar Aug 16 '15 at 15:00
  • possible duplicate of [PHP's file\_exists() will not work for me?](http://stackoverflow.com/questions/1287837/phps-file-exists-will-not-work-for-me) – Saumini Navaratnam Aug 16 '15 at 15:01

2 Answers2

2

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.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

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