Is there a way to check whether files (with either an absolute or relative path) exists? Im using PHP. I found a couple of method but either they only accept absolute or relative but not both. Thanks.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
file_exists($file);does the trick for both relative and absolute paths.What’s more useful, however, is having absolute paths without hardcoding it. The best way to do that is use
dirname(__FILE__)which gets the directory’s full path of the current file in ether UNIX or Windows format. Then we can userealpath()which conveniently returns false if file does not exist. All you have to do then is specify a relative path from that file’s directory and put it all together: