0

I'm working on a plugin and trying to embed something if the file/folder exists. This perfectly works in php but not within WP. Any help would be appreciated. This code is in the admin area under the plugin's setting page. And yes, I'm sure the path to the folder is correct.

$filename = plugins_url('../admin_side/webmailing/install/', __FILE__);

if (file_exists($filename)) { 

echo 'something';

 else {

echo 'something else';

} 
14
  • What is $filename being set to? Commented Mar 20, 2015 at 12:24
  • the path to the folder? Commented Mar 20, 2015 at 12:25
  • Well, clearly it's not or it'd be working :) have you echo'd the contents to check? Commented Mar 20, 2015 at 12:26
  • yes I did, if I echo it I get the url for the folder Commented Mar 20, 2015 at 12:27
  • just to mention that I get a blank page and no errors Commented Mar 20, 2015 at 12:31

1 Answer 1

2

file_exists expects a file path but you're passing it a URL.

Change:

$filename = plugins_url('../admin_side/webmailing/install/', __FILE__);

To:

$filename = plugin_dir_path( __FILE__ ) . 'admin_side/webmailing/install/';
Sign up to request clarification or add additional context in comments.

2 Comments

No it doesn't. You can use relative or network paths. See php.net/file_exists
Slight mixup of terminology on my part but the answer remains valid. The issue is because the user is passing in a URL and not a file path. The code provided resolves the issue.

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.