CodeIgniter Forums

Full Version: file_exists() function is not working in CI Helper
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Sumon[/eluser]
I am surprised to found that file_exists() function is not working in CI Helper functions. Moreover, base_url() function works fine inside helper. It makes me confused. May be i am wrong or it's a restriction of CI.
here is my code:
Code:
$ImgLoc=base_url()."gallery/member_picture/member_thumbnail/member_no_image_thumb.jpg";
echo $ImgLoc;
if(file_exists($ImgLoc))
  return "File is in server";
else
  return "Not in server";

Surprisingly it goes else block always.. i copy and paste the URL get by echo of my code and browser displays my image in there.

Any idea where i made mistake or file_exists() function not working inside Helper?

El Forum

[eluser]mironcho[/eluser]
As far as I know, file_exists() doesn't support URL wrappers (well, since php version 5 some are supported), so you should use filesystem path instead of URL. Nevertheless, if you really want to check if URL exists, you can do it with fopen() (of course - only if allow_url_fopen is allowed in your php configuration):
Code:
if (fopen($ImgLoc, 'r'))
{
    // exists
}
else
{
    // nope...
}

http://php.net/manual/en/function.file-exists.php

El Forum

[eluser]Sumon[/eluser]
thanks. i highly appreciate your suggestions. still i am confused about why file_exists() function does not work where the image is in proper location. Moreover, is try by fopen() for an image function is good practice? i am confused.

Once again here is a function i test in my controller
Code:
function ImageLoc()
{
   if(file_exists(base_url()."/gallery/member_picture/member_no_image.gif"))
        echo "File Exist.";
   else
    echo "File Not Exist.";
}
I get the result File Not Exist. But the fact is file is in there.

El Forum

[eluser]xwero[/eluser]
You can't use the base_url function because file_exists needs the file path not the url path. You could do something like
Code:
function ImageLoc()
{
   if(file_exists(dir_name(FCPATH)."/gallery/member_picture/member_no_image.gif"))
        echo "File Exist.";
   else
    echo "File Not Exist.";
}

El Forum

[eluser]mironcho[/eluser]
Sumon, base_url() returns URL, not filesystem path, which won't work with file_exists().



// EDIT: xwero is faster again Smile

El Forum

[eluser]Seppo[/eluser]
dir_name => dirname // =)

El Forum

[eluser]Sumon[/eluser]
Yes finally i got the solution... it works with
Code:
if(file_exists("./gallery/member_picture/member_no_image.gif"))
  echo "File Paise.";
else
  echo "File Pai ni.";

file_exists function in CI not works with absolute path. Is there are any way how i can enable absolute path? However, i got my solution

El Forum

[eluser]xwero[/eluser]
the underscore was a typo but my code snippet should work. the FCPATH constant is the absolute path to the bootstrap file including the file name index.php, that is why you need the dirname function.

El Forum

[eluser]wiredesignz[/eluser]
The title of this thread should be changed to `I don't know how to use the file_exists() function`.

file_exists() is a PHP function and it has nothing to do with CI. It does work with absolute file paths as shown by xwero (typo excused)

Code:
file_exists(dirname(FCPATH)."/gallery/member_picture/member_no_image.gif")

El Forum

[eluser]Sumon[/eluser]
YES!!!! You are right. Thanks. Would you please let me know how to change current post topic? However, thanks guys... I am fully clear right now...
Pages: 1 2