Search code examples
phpwordpresspluginsopendir

Reading a Wordpress Directory With PHP


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.";
}

Solution

  • 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.