php - File_exists function is not working -


since problem on file_exists() , seems function should goes root directory, when determine file location on file_exists function declare base url because tests file location server not url :

wrong code

    $dir = base_url()."assets/produits/";      foreach ($rows $row) {         $nom = $row->nshape;         $type = array(".jpeg", ".jpg");          foreach ($type $ext) {             echo "<br>i'm in foreach loop <br>";             $file_name = $dir . $nom . $ext;              if (file_exists($file_name)) {                echo "i'm in file_exists function<br>";                $img_src = $file_name;             } else {                 echo "i'm in else statement <br>";                 echo $file_name."\n";                 $img_src = $dir . "none.png";             }         }     } 

the problem full name there treat doesnot exists, i've made echos check did code reaches , here's screeenshot : screen shot of problem

knowing http://localhost/dedax_new/assets/produits/2052.jpegexists in server.

solution :

// set default no find image $img_src = base_url() . "assets/produits/none.png";  foreach ($rows $row) {     $nom = $row->nshape;     $type = array(".jpeg", ".jpg");      foreach ($type $ext) {         $file_name =  $nom . $ext;          if (file_exists("./assets/produits/".$file_name)) {            $img_src = base_url() . 'assets/produits/'.$file_name;;            // found file exists 'get out of dodge'            break;         }     } 

thanks contributors in advance.

file_exists works on file system , not on web. using web address rather file location.

also current loop possible find file first time round loop using first ext , fail find file second ext , assume there no file found.

so try this

$dir = "dedax_new/assets/produits/";  // set default no find image $img_src = $dir . "none.png";  foreach ($rows $row) {     $nom = $row->nshape;     $type = array(".jpeg", ".jpg");      foreach ($type $ext) {         echo "<br>i'm in foreach loop <br>";         $file_name = $dir . $nom . $ext;          if (file_exists($file_name)) {            echo "i'm in file_exists function looking $filename";            $img_src = $file_name;            // found file exists 'get out of dodge'            break;         }     } } 

also other common mistake not respect case sensitivity of file system. make sure respect case sensitivity of file's location url not match file system's configuration.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -