CodeIgniter Forums
file_exists() function is not working in CI Helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: file_exists() function is not working in CI Helper (/thread-8352.html)

Pages: 1 2


file_exists() function is not working in CI Helper - El Forum - 05-15-2008

[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?


file_exists() function is not working in CI Helper - El Forum - 05-15-2008

[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


file_exists() function is not working in CI Helper - El Forum - 05-15-2008

[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.


file_exists() function is not working in CI Helper - El Forum - 05-15-2008

[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.";
}



file_exists() function is not working in CI Helper - El Forum - 05-15-2008

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



// EDIT: xwero is faster again Smile


file_exists() function is not working in CI Helper - El Forum - 05-15-2008

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


file_exists() function is not working in CI Helper - El Forum - 05-16-2008

[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


file_exists() function is not working in CI Helper - El Forum - 05-16-2008

[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.


file_exists() function is not working in CI Helper - El Forum - 05-16-2008

[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")



file_exists() function is not working in CI Helper - El Forum - 05-16-2008

[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...