Welcome Guest, Not a member yet? Register   Sign In
file_exists() function is not working in CI Helper
#1

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

[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
#3

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

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

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



// EDIT: xwero is faster again Smile
#6

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

[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
#8

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB