0

I can't seem to get the following code to work?

$playerfileLocation = "../wp-content/gallery/playerphotos/' . $playerid . '.jpg";
if(file_exits($playerfileLocation)) 
echo "The file File exists";
else
echo "The file File does not exist";

The "playerid" field is a number that gets passed through.

I just can't seem to get it working. Urgh!

2
  • Is this relative path working? Commented Dec 24, 2011 at 4:12
  • No...the page stops once it gets to the if statement. I have text below this "test" that is not getting displayed. Commented Dec 24, 2011 at 4:21

3 Answers 3

5

You have mismatch of quotes. Try this code.

$playerfileLocation = "../wp-content/gallery/playerphotos/" . $playerid . ".jpg";
if(file_exists($playerfileLocation)) 
echo "The file File exists";
else
echo "The file File does not exist";

Update: Actually I would suggest using below code as whenever PHP see double-quotes, it tries to parse anything in between it, which is not required in this case. This is a small optimization in performance.

$playerfileLocation = '../wp-content/gallery/playerphotos/' . $playerid . '.jpg';
if(file_exists($playerfileLocation)) 
echo "The file File exists";
else
echo "The file File does not exist";

Also, to just check if file exists and if not display default image use the following code:

$playerfileLocation = '../wp-content/gallery/playerphotos/' . $playerid . '.jpg';
$defaultfileLocation = '../wp-content/gallery/playerphotos/default.jpg';
if (!file_exists($playerfileLocation)) {
    $playerfileLocation = $defaultfileLocation;
}
Sign up to request clarification or add additional context in comments.

3 Comments

What's problem for double quotes in whenever PHP see double-quotes, it tries to parse anything in between it, which is not required in this case. This is a small optimization in performance.
When PHP sees double quotes it would try to parse it which takes some time (although it is very less, but still takes some time). If you use single quotes then PHP does not need to parse it saving the time required to parse it. In this case there are no variables in the string and hence its not required to parse it, unless you do $playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg"; This will require PHP to parse the entire string. Also, the code is now not very readable.
I finally figure out the problem It was related to the path I was using. I need to define an absolute path: $absolutepath = $_SERVER{'DOCUMENT_ROOT'}.'wp-content/gallery/playerphotos/' . $photoname . '.jpg'; The file_exists function works fine now. ;)
1

Your code is misspelled: (exist not exits)

$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";

$default_player = "../wp-content/gallery/playerphotos/default.jpg";

if(file_exits($playerfileLocation)) 

{

}

else

{

$playerfileLocation=$default_player;

}

1 Comment

Well now I feel like an idiot. Thank you for solving my problem.
0

In php, we can get variable value in double quotes also. It's good practice to use double quotes in path, so no much indentation is required.

$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";


$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(!file_exists($playerfileLocation)) 
{
$playerfileLocation=$default_player;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.