Jump to content

File_Exists not working, could someone help or suggest an alternative?


DrkInsanity

Recommended Posts

Okay, what I'm trying to do is check to see if a series of four files (JPG images, in this case), based on a string, exist, and if they do exist it sets a string to a variable, and if they're not, then it sets that string to a different variable. Using an If/Else statement. All of this is basically so I can either display the images, or display a "image not uploaded" image in any that don't existist's place.

 

Okay, that was worded kind of awkwardly. How about I just show the code that is apparently not working:

 

//check if member image files exist
//image 1
If (file_exists('http://thejsf.com/roster/images/members/'.$name.'_01.JPG')) {
$missingimage1 = 0;
}
Else {
$missingimage1 = 1;
}
//image 2
If (file_exists('http://thejsf.com/roster/images/members/'.$name.'_02.JPG')) {
$missingimage2 = 0;
}
Else {
$missingimage2 = 1;
}
//image 3
If (file_exists('http://thejsf.com/roster/images/members/'.$name.'_03.JPG')) {
$missingimage3 = 0;
}
Else {
$missingimage3 = 1;
}
//image 4
If (file_exists('http://thejsf.com/roster/images/members/'.$name.'_04.JPG')) {
$missingimage4 = 0;
}
Else {
$missingimage4 = 1;
}

 

What this should do is, for each image, give me a $missingimage value that is 0 if the image is not missing, and 1 if it is missing. These values I can later use in another If/Else statement to either display the image or a replacement if it doesn't exist. However, even when all four images are there, they all come up with a value of 1 (missing). I can check this when I use:

 

echo "<!-- ".$missingimage1." ".$missingimage2." ".$missingimage3." ".$missingimage4." --> ";

 

Then check the page source, and I have a row of four commented 1's.

 

Also, I am able to display the images just by using:

 

echo "<img src=\"http://thejsf.com/roster/images/members/".$name."_01.JPG\"> ";

 

So I know the images exist, and that it is not my $name string that is causing the problem. Just something is happening that causes either the file_exists to not find them, or sets all of the values to 1. I'm out of ideas (as I've tried probably twenty different things to try to get it to work).

 

The only other useful piece of information I could think of to give anyone who would like to help is that I am using a mod rewrite in my .htaccess; where http://thejsf.com/roster/member_pages.php?name=membername gets changed to look like http://thejsf.com/roster/membername/. However, this problem still persists even if I delete the whole .htaccess file outright, so I don't think it is that that is causing any problems.

 

If anyone could help me out, or at least point me in the right direct, I would certainly appreciate it. I'm pretty new to PHP coding, so I wouldn't doubt it if it turns out to be I just put a semicolon in the wrong place or something, but I'd even appreciate it if someone would point that out to me  :)  Also, sorry if I used some of these terms wrong-- I'm still getting used to/trying to learn a lot of them.

 

Thanks in advance!

Link to comment
Share on other sites

Firstly, I always like to put strings in double quotes, that way I can just easily insert variables as needed. For a piece of your code, I would do

If (file_exists("http://thejsf.com/roster/images/members/$name_01.jpg"))

 

lol and in typing that, I believe I found your error. In my experience, most of the time, when attempting to access or modify a file, it is case-sensitive. And it is likely that the images you are testing for end in ".jpg" instead of ".JPG" if they are, in fact, .JPG, then reply here and I'll give another suggestion(but that's probably it).

 

p.s. quick note. You are able to display the image in html bc as far as I know, html isn't case-sensitive, whereas file functions in most cases are.

Link to comment
Share on other sites

Okay, thanks for the suggestions, but the problem is still occurring  :-\

 

Firstly, I always like to put strings in double quotes, that way I can just easily insert variables as needed. For a piece of your code, I would do

If (file_exists("http://thejsf.com/roster/images/members/$name_01.jpg"))

 

lol and in typing that, I believe I found your error. In my experience, most of the time, when attempting to access or modify a file, it is case-sensitive. And it is likely that the images you are testing for end in ".jpg" instead of ".JPG" if they are, in fact, .JPG, then reply here and I'll give another suggestion(but that's probably it).

 

p.s. quick note. You are able to display the image in html bc as far as I know, html isn't case-sensitive, whereas file functions in most cases are.

 

I did double check to see if this was what was causing the problem, the casing, but as I expected, the images do indeed end in ".JPG". So it's not a casing problem.

 

file_exists() does not work with the http wrapper. If the file is on your server, use a file system path, not a URL.

 

Is this true? I'm not sure what to put for the filename, then. I've tried:

 

If (file_exists("/images/members/".$name."_01.JPG")) {

 

But that didn't work either. Do I need to lead it with something?

 

Oh, and actually, could it be that it is basing the URL off of the mod rewrite one (see the bottom of my OP for how it's being rewritten)? If so, how would I make it use its original, real, URL as the base? I'm sure there's some kind of function for that, if that is in fact what is causing the problem. I just don't know what to do.

 

And thanks again for the suggestions, guys!

Link to comment
Share on other sites

Alright, I got it! It was like you said: I couldn't use a URL. And it was working before because I was using a slash in front of the path, like this:

 

If (file_exists("/images/members/".$name."_01.JPG")) {

 

Which I changed to this:

 

If (file_exists("images/members/".$name."_01.JPG")) {

 

Perfect! Thanks for your guys' help  :D

 

Though I feel kinda dumb for not trying something so obvious...

Link to comment
Share on other sites

Alright, I got it! It was like you said: I couldn't use a URL. And it wasn't working before because I was using a slash in front of the path, like this:

 

If (file_exists("/images/members/".$name."_01.JPG")) {

 

Which I changed to this:

 

If (file_exists("images/members/".$name."_01.JPG")) {

 

Also, fix'd  :P

 

Thanks again for your help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.