How to use relative path with my plugins folders?

Hi i tried for picking all images in a folder so that what ever images user puts in a specified folder inside my plugin i can take and use it in plugin.

I tried the following php functions to pick images or files in folder

Read More
glob

scandir

When i try the above in wordpress framework am getting error The system cannot find the file specified.
when i try in my local server other than wordpress area it is picking files in the specified folder.

<?php
foreach(glob('../images/*.png') as $filename){
     echo $filename;
 }?>

scan method

<?php
 foreach(scandir('../images/') as $filename){
     echo $filename;
 }?>

The above working fine outside the WordPress Framework

Same thing i tried in WordPress for my plugin in my plugin folder and i can’t accomplish the task.This is not working.

Attempt 1

<?php
foreach(glob('../images/*.png') as $filename){
     echo $filename;
 }?>

i got nothing no error,no warning and no output.

Attempt 2

<?php
     foreach(scandir('../images/') as $filename){
         echo $filename;
     }?>

I got Warning Warning: scandir(../images/,../images/): The system cannot find the file specified. (code: 2)

 Warning: scandir(../images/): failed to open dir: No such file or directory

The file am trying this is in different folder but same directory.

For example

  • Php file with this code is in inc folder inc/settings.php

  • The images am looking is in images folder

  • Both folder are inside my plugin folder

     - myplugin
               myplugin.php
               /images
               /inc
               /documentaion
    

Now how to get all images names in my images folder?

NOTE: I use to include my other php file in my main file like include('inc/settings.php');

These thing works fine so most likely there is no problem in path specifying, May be the problem in using scandir or glob function. I can’t able to guess.Can anyone help me?

When i simply use the below

foreach(glob('images/*.png') as $filename){
         echo $filename;
     }
     ?>

Am getting images from wp-admin/images folder any idea?

Related posts

2 comments

  1. This Answer relates to the first version of the Question, and I don’t know how to updated it, I’ll leave it here as general reference.

    Don’t use a plugin folder (or a theme) for this. Maybe your plugin doesn’t have an Upgrade feature and nothing will be overwritten. But the best practice is to use a folder in /wp-content root or inside /wp-content/uploads.

    In /wp-content/my-images:

    $test = glob( WP_CONTENT_DIR . '/my-images/*.png');
    

    In /wp-content/uploads/my-images:

    $up_dir = wp_upload_dir();
    $test = glob( $up_dir['basedir'] . '/images/*.png');
    
  2. scandir and glob are related to directory things, so tried with WP_PLUGIN_DIR instead of plugins_url() which return url but not directory.

    Below both function are useful

    WP_PLUGIN_DIR

    plugin_dir_path( __FILE__ )
    

    I tried the above functions it worked well for me.

    Remember if you just use

    <?php
        foreach (glob('images/*.png') as $filename) {
            echo $filename;
        }
    ?>
    

    It will get images from wp-admin/images which might be root directory. Am not sure.

    Comment if you think anything is wrong here:)

Comments are closed.