Outputting an img from templates

Inside ‘app/templates/Article/view.php’, I’m trying to output an img from 'app/uploads/img/.

I tried these but none is working:

“app/uploads” is not part of what the web server is allowed to serve out. Are there restrictions on who can see these images, e.g. only people who are logged in? If so, you’ll need to make a controller that checks those permissions and then sends the file, loaded from this “secret” place. If not, your uploads folder should be somewhere under webroot instead. We can help with the details of either one, but need to know which you’re aiming for.

No, No restrictions for seeing those images. I’ll try to move the uploads folder under webroot directory I check it again. Thanks

Now the folder is under webroot/uploads/img/.
But
img src=“webroot/uploads/img/<?= h($article->user->img_name) ?>” or
img src=“uploads/img/<?= h($article->user->img_name) ?>” Still don’t work.

HTML img src are just link a src => relative to the current URL.

So use an absolute URL and try img src=“/uploads/img/<?= h($article->user->img_name) ?>”

Working finally, thank you.

Something like echo $this->Html->image("/uploads/img/{$article->user->img_name}") should also work, and I believe there’s ways to configure things so that you could just say echo $this->Html->image($article->user->img_name) and it would know how to deal with that. Can’t remember exactly how to do that right now, though, and other things to get to. :slight_smile:

1 Like

@Zuluru what you mean is present inside config/app.php and can be configured via App.imageBaseUrl config key :wink:

1 Like