0

I am in the process of writing a Wordpress plugin, and I'm having an issue reading the files in a directory. This script works fine outside of wordpress, and I'm not sure what the issue is.

$thumbPath = '../wp-content/uploads/images/thumbs'; 

//added for debugging
$link = $thumbPath . '/1.jpg';
echo " <a href='" . $link . "'>Link</a><br />";

if ($handle = opendir($thumbPath)) 
{  
    echo "here";
}

The link works and takes me straight to the image. I've tried every variation of the path I can think of. I looked at Read Images from a Directory using PHP and from everything I can see, it should be working!

Any ideas?

EDIT Here are the code changes I've made, attempting to resolve this:

$upload_dir = wp_upload_dir();
$thumbPath =  realpath($upload_dir['baseurl']) . "/images/thumbs";
echo " <img src='" . $thumbPath . "/1.jpg' /><br />";

if ($handle = opendir($thumbPath)) 
{  //if the directory exists open
    echo "here";
}
else
{
    echo "<br />The damn thing isn't working.";
}
Community
  • 1
  • 1
Kendra
  • 116
  • 2
  • 13
  • Is your path correct with regards to WordPress? – Benjam Nov 30 '12 at 22:23
  • Are you sure that WP has the current directory you think it does? – isturdy Nov 30 '12 at 22:26
  • What do you mean exactly? The link works, so as far as I can tell the path is correct. I set up a page outside of Wordpress that does exactly what I want and uses the path '../blog/wp-content/uploads/images/thumbs', which doesn't work from inside Wordpress, since Wordpress is in the /blog directory. – Kendra Nov 30 '12 at 22:32
  • I've also tried an absolute path, with no success. – Kendra Nov 30 '12 at 22:36

2 Answers2

1

Try this:

if ($handle = opendir($thumbPath)) 

It might help.

Edit: (Some additional explanation) Variables in single quotes are treated as an ordinary text, and because of that, you're trying to open a directory called $thumbPath, which I suppose doesn't exist.

MichalHlavacek
  • 324
  • 3
  • 15
  • Please expand your answer, explaining the role of single quotes in variable interpolation – Frank Farmer Nov 30 '12 at 22:26
  • Having the quote marks doesn't seem to make a difference, but thanks for the reply. – Kendra Nov 30 '12 at 22:34
  • In that case, could you be more specific about the "I'm having an issue reading the files in a directory"? Do you get warning, or "here" on the output? – MichalHlavacek Nov 30 '12 at 22:42
  • No, this is the output I get: Link I've got a loop that will run through all the files in a particular directory and create the markup for a lightbox application. It works perfectly when ran outside of Wordpress. – Kendra Nov 30 '12 at 22:50
  • I'm not sure but I found [somewhere](http://wordpress.stackexchange.com/questions/65428/opendir-and-wordpress-path) this quite similar question, you should definitely try `$thumbPath = plugin_dir_path( __FILE__). '../wp-content/uploads/images/thumbs';` – MichalHlavacek Nov 30 '12 at 23:11
  • That doesn't work either. $thumbPath = plugin_dir_url( __FILE__). '../../uploads/images/thumbs'; did give me a complete absolute path, but it also didn't work. – Kendra Nov 30 '12 at 23:18
  • As I look at my local installation of WP, there's something not quite right with your path I think. What is your WP hierarchy exactly? Where is this file located? Because for me the `../uploads/images/thumbs` path (without wp-content) works just fine, even without this plugin_dir_path function. – MichalHlavacek Nov 30 '12 at 23:40
  • http://www.url.com/blog/css3-lightbox/ is the location the script function is being called from. – Kendra Nov 30 '12 at 23:57
  • I think that "plugins" should be places in "wp-content/plugins", but that shouldn't be such a huge problem, unless there are some restriction to it (which I don't know about). One final thing and then I'm out of ideas, try to use wp_upload_dir(), as described [here](http://codex.wordpress.org/Function_Reference/wp_upload_dir) – MichalHlavacek Dec 01 '12 at 00:13
  • $thumbPath = $upload_dir['baseurl'] . "/images/thumbs"; outputs http://www.url.com/blog/wp-content/uploads/images/thumbs which is the url for the directory - but the script still doesn't run. – Kendra Dec 01 '12 at 00:33
0

I'm not sure what I did, but it's working now.

$path = "../blog/wp-content/uploads/images";
$thumbPath = $path . "/thumbs";
$fullPath = $path . "/full";

if ($handle = opendir($thumbPath)) 

I'd already tried that url, but maybe I had a missed typo.

Kendra
  • 116
  • 2
  • 13