0

i have an image to display in view, when i put it in public/assets/img folder and writing in view(.phtml) enclosed code it works:

<img src="/assets/img/logo.jpg"  width="150" height="150" class="img-circle pull-left" alt="<?= $fournisseur->intitule ?>"  />

I want to put the images in data/img folder and display it in my view, how can i display it in view i tried with thise code, deleting data in path but no image view:

<img src="/data/img/logo.jpg"  width="150" height="150" class="img-circle pull-left" alt="<?= $fournisseur->intitule ?>"  />

i thought to create a controller for image:

public function imageAction()
{
    /** get id by url  **/
 $imageContent = file_get_contents('./data/img/logo.jpg');
    /** set file to reponse   **/
    $response->setContent($imageContent);
    /**  creat header **/
    $response
        ->getHeaders();
    return $response;
}

in view :

<img src=<?=$this->url('fourni',array('action' => 'image');?>class="img-rounded" alt="image" width="50" height="50">
iss yaz
  • 21
  • 1
  • 8
  • Forgot to ask it before, but.. Why do you want to put images into `/data` folder? If those images are public (like, the logo, or static images that are part of the "template"), then you should put them in `/public`. – Ermenegildo Apr 27 '21 at 08:20
  • The only situation on which you could say "I do it for security purposes", is when those images (or any kind of file) is uploaded by the user and you want to control who can access them. – Ermenegildo Apr 27 '21 at 08:21

2 Answers2

0

I was searching to do same... i just solved it like this:

At my phtml file:

<img src="<?php echo $this->basePath() .'/../data/img/logo.jpg';?>">

Hope is usefull.

-1

Make sure the data folder is inside the public folder as well otherwise you will need to go up the directory "./../data/img/" for be able to access that image.

  • i put /data/img/ outside of public folder, even if i go up the directory with "./../data/img/, it doesn't work, it shows the right path (localhost/data/img/logo.png), but it doesn't display the image, data is not a public folder. – iss yaz Apr 06 '21 at 13:46
  • So if everything is correct like name and the locations, then you might check the permission on the folder and file as well. – Suman Karanjit Apr 07 '21 at 14:24
  • @SumanKaranjit all files must be inside in the `public` folder. You **can't** access the `data` folder from the HTML and putting `./../data/img` will not solve the problem. – Ermenegildo Apr 07 '21 at 15:43
  • i want use data folder for security, any one can access to public folder, but i've found for data it's not allowed to all, it must be allowed by code – iss yaz Apr 08 '21 at 07:11
  • @issyaz if you want to access files inside `data` folder, then you must create a controller which takes an input (like `file_id`) and then return a stream. I'll post an example code this afternoon – Ermenegildo Apr 09 '21 at 09:19
  • thks @Ermenegildo for your answer i created function imageAction in controller as above without using id, i have for the moment one image to display, thanks for your help, i'm waitting for the example of code, it will help me. – iss yaz Apr 10 '21 at 10:22
  • It's ok, i resolve it by making a controller and using id for image – iss yaz Apr 27 '21 at 09:07