user997593
user997593

Reputation: 433

file_exists returns false even when the path is right

I have seen some similar questions here, but no answer fit my needs.

I have a Wordpress and in the upload dir there is an image, I have the following url for the image: ../../uploads/2016/2/56c3620a9c8af.png I try to access the file from inside the child theme folder:

/home/mydomain/www/wp-content/themes/twentyfifteen_child

I would like to check if the file exists and afterwards unlink() the image, but the file_exists always returns false, even though I can echo the image. The following simple function outputs the image but returns false.

function checkImageExist($url)
{
    echo '<img src = "'.$url.'" /><br>'; //The image is rendered

    clearstatcache();

    if(file_exists($url))
    {
       echo 'Image exists '.$url;
    }
    else
    {
        echo 'Image does not exist';    
    }
}

And this is what I CAN'T UNDERSTAND. What am I missing?

THERE WAS NO QUESTION AT ALL:

The script I was using to upload the files was duplicating the extension names, so 56c3620a9c8af.png was in fact 56c3620a9c8af.png.png this stupid mistake was the responsible of images appearing when called (as the browser was able to parse them even though the duplicated extension) but file_exists was not able to find them. So everything was working and I was mistaken about the error source.

Upvotes: 0

Views: 797

Answers (1)

Scriptonomy
Scriptonomy

Reputation: 4055

You are passing a $url when what you really need is a $path. You can get the path like this:

$path = wp_upload_dir(); //This returns an array with URL info

$path['path']. '/2016/2/56c3620a9c8af.png'

Upvotes: 1

Related Questions