Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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?

share|improve this question
add comment

2 Answers

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');
share|improve this answer
    
Thanks earlier i used to give my predefined images in plugin folder and based on selection i'll use those images in output, there was no problem because i use my naming convention on the dedicated folder. but now i try to give freedom to user like they can place their images in an dedicated folder in my plugin like assets or images. –  sun Oct 30 '13 at 13:39
    
so plugins_url() worked when i was using my images.am not sure about upgrade option feature in plugin –  sun Oct 30 '13 at 13:41
    
I don't understand what's not working for you... –  brasofilo Oct 30 '13 at 13:42
    
I use plugins_url() navigate to my plugin directory and choose images in previous version.Now how can i get images in a particular folder which is placed inside my plugin? –  sun Oct 30 '13 at 13:47
    
I won't know what is the name of the image will be, because its user images.how to pick those images? –  sun Oct 30 '13 at 13:49
show 2 more comments
up vote 0 down vote accepted

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:)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.