2

I want to check if a file in a directory exists. I my case it does exist.

if(file_exists("C:\\xampp\\htdocs\\myApp\\img1.jpg"))
{
    echo "is file";
    exit;
}

Here is an image of that path:

enter image description here

But somehow this does not work.

6
  • 1
    windows wants two backspaces after the drive letter. and since you have to escape them, you have to write four backspaces after C: Commented Oct 28, 2016 at 7:58
  • "On windows, use //computername/share/filename or \\computername\share\filename to check files on network shares." Commented Oct 28, 2016 at 7:59
  • @kosta it's a local drive. not a network share Commented Oct 28, 2016 at 7:59
  • than this will work if(file_exists("C://xampp/htdocs/myApp/img1.jpg")) Commented Oct 28, 2016 at 8:01
  • You can also use forward slashes for paths. Commented Oct 28, 2016 at 8:01

2 Answers 2

1

Forward slashes will also work on Windows. You can use forward slashes so you do not need to escape them:

if (file_exists("C:/xampp/htdocs/myApp/img1.jpg"))
{
    echo "file exists";
    exit;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Please use only one \ and follow bellow :

if(file_exists("C:\xampp\htdocs\myApp\img1.jpg"))
{
    echo "is file";
    exit;
}

or

if(file_exists("img1.jpg"))
{
    echo "is file";
    exit;
}

because img1.jpg is your project root folder

1 Comment

can you explain your project folder/file structure?

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.