0

This is my first time playing with PHP, and I'm having trouble making a basic if/then statement. I want to do something like

if file exists show the html code else show different html code.

Here's where I'm at currently --

<?php
if ( file_exists('pdf/'.'htmlspecialchars($_POST['apt'], ENT_COMPAT)'.'.pdf') {
    echo "the file exists";
}  else {
    echo "file does not exist";
}
?>

I think the problem here is how I wrote the

file_exists('pdf/'.'htmlspecialchars($_POST['apt'], ENT_COMPAT)'.'.pdf')

Thoughts greatly appreciated!

3
  • 2
    why do you put a function call into ' ? And also you are missing a closing ) from the if () statement. Commented Dec 13, 2012 at 8:17
  • Hi GBD, I don't think I have a proper set up, becuase any time I make an error in PHP, I just get a 500 internal server error. Commented Dec 13, 2012 at 8:18
  • just remove htmlspecialchars. If someone tries to send a char like & the if will fail anyways since characters like &, <, and " aren't allowed in file names. Commented Dec 13, 2012 at 8:25

4 Answers 4

3

Try with:

if (file_exists('pdf/' . htmlspecialchars($_POST['apt'], ENT_COMPAT) . '.pdf')) {
    echo "the file exists";
} else {
    echo "file does not exist";
}

You had some quotes before calling htmlspecialchars(). When you call a function you don't need quotes. See also that you didn't close a bracket.

Sign up to request clarification or add additional context in comments.

Comments

2

There are two problems. Firstly, you are missing a closing bracket at the end of your if condition, and secondly you have quotes where you should not, surrounding the call to htmlspecialchars.

The correct code would be:

if (file_exists('pdf/'.htmlspecialchars($_POST['apt'], ENT_COMPAT).'.pdf')) {
    echo "the file exists";
}  else {
    echo "file does not exist";
}

Comments

2

You have a problem of quotes:

file_exists('pdf/'.htmlspecialchars($_POST['apt'], ENT_COMPAT).'.pdf')

Comments

1

you dont need htmlspecialchars. It will convert improper chars into even more improper chars. For example ' becomes & # 0 3 9 ; (without spaces)

5 Comments

This is not the answer! If you have friendly advice then comment on the question, or answer the question and add your advice at the end
This is most definitly an answer. Just ask him to request a file named "&^%$#.pdf and you'll see that htmlspecialchars is not only pointless but will compound the problem.
Does it solve the issue that he is having? No it doesn't, it advises him that the function is not necessary. The correct answer is the one that has been accepted. +1 for the good advice, but this is not an answer :-)
I 100% agree with you that the htmlspecialchars function will cause problems for files with special characters, along with it not being necessary anyway, but this was not what was causing the problem in his case :-)
I saw the htmlspecialchars call and ignored the rest. The script error was from incorect syntax, MitchS got it right

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.