user1091856
user1091856

Reputation: 3158

(PHP) Returning picture from a directory

In my Product Class, there is a function called pictures() which returns the file address of the picture of a product. It's something like this:

public function picture()
{
    $file = "images/pictures/products/" . $this->id . ".png";
    if(file_exists($file))
        return $file;
    else
        return false;
}

It works fine when running the code on the administration area of the website (which is located on /admin/ directory)

But when I call this function from the index.php which is not on the /admin/ directory it always returns false. What should I do? The only way I figured to solve this is by creating a parameter on the picture() function, like: picture($directory_prefix) then that way I'll call the function with picture("/admin/"). But there's gotta be a better way than this...

Upvotes: 1

Views: 36

Answers (1)

Jeremy Harris
Jeremy Harris

Reputation: 24579

Place your resources in a directory under the web root. Then the path would be:

$file = $_SERVER['HTTP_HOST']."/images/pictures/products/" . $this->id . ".png";

Upvotes: 2

Related Questions