2

Trying to retrieve an image from outside public. I have been using file controller helper but still can't get it to work:

here is my code that is returned as AJAX to update the IMG SRC path

$imgPATH = $this->getParameter('kernel.project_dir').'/uploads/user_profile_pictures/';

$ext = [".png", ".jpg", ".jpeg", ".gif"];

foreach ($ext as $x) {
    $imgPath = $this->getParameter('kernel.project_dir').'/uploads/user_profile_pictures/'.$usertoPic.$x;

    if (file_exists($imgPath)) {
       return $this->file($imgPath , 'userProfilePicture.png' , ResponseHeaderBag::DISPOSITION_INLINE);
    } 
}

The image is found by PHP but I get the following broken HTML in the browser; I think this is because it is binary ? how can I convert it to HTML compliant ?

<img id="userIMG" src="�PNG��IHDR���S���S���lЬW���pHYs��.#��.#x�v��OiCCPPhotoshop ICC profile��xڝSgTS�=���BK���KoR RB���&amp;*!J�!��Q�EEȠ�����Q,������������{�kּ������&gt;�����H3Q5��B�������.@�$p��d!s�#��~&lt;&lt;+&quot;���x���M��0���B�\���t�8K��@z�B��@F���&amp;S���`�cb��P-�`" �������{�[�!���="" e�d�h;���v�e�x0�fk�9��-�0iwfh��������="" �0q��)�{�`�##x����f�w<�+��*��x��<�$9e�[-qww.(�i+6aa�@.�y�2�4�����������x����6��_-��"bb���ϫp@���t~��,="" ��;�m��%�h^�u��f�@�����w�p�~<<e���������j�b[a�w}�g�_�w�l�~<�����$�2]�g�����l�ϒ="" �b��g�����"�ib�x*�qq�d���2�"�b�)�%��d��,�="">
7
  • Which web-server do you use? This is something related to web-server setup. Files outside public directory (document root is probably a better term) are not meant to be accessible the way you expect them to be. Commented Aug 31, 2018 at 13:44
  • You would first need to put the kernel path "%kernel_root_dir%" Commented Aug 31, 2018 at 14:40
  • 1
    @FlorentDestremau do you mean like this ? $imgPATH = '%kernel_root_dir%/uploads/user_profile_pictures/img.png'; Commented Aug 31, 2018 at 17:40
  • Symfony 3 version but the concept should be same. Uploading images to a private directory and serving them in twig template Commented Aug 31, 2018 at 18:08
  • @BentCoder thanks for the link but the first thing is to change user permissions... can't really do that on a development server composer require server That would mean I can't test before production ? there must be a more straightforward way Commented Aug 31, 2018 at 19:20

2 Answers 2

3

Your browser cannot have access to a file that is not in the public (for Symfony 4) folder. That is pretty much the whole point of having a "public" directory.

What you can do is serving the file directly as a binary response given a certain link in your app, like documented here: https://symfony.com/blog/new-in-symfony-3-2-file-controller-helper

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class ImgController extends Controller
{
    /**
     * @Route("/get-my-img")
     */
    public function getImg()
    {
        $basePath = $this->getParameter('kernel.root_dir').'/uploads/user_profile_pictures/';

        return $this->file($basePath . "img.png");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

thank you so much for your answer, I tried but still getting 404; I updated my question with the code
oh it's ajax ? Then replace the src field of your image with the url, you're not supposed to fetch the data directly through ajax
I finally got it working but had to adapt your answer by converting to base64
Oh right, and then you decode it and set it as data://blob-xxx.. ?
I get the file with $this->getParameter then encode the file to base64 then set the image src with it. It works because this is an image; not sure about other files.
0
    // this should work quite simple:

    // site/public/css/style.css
    <link href="{{ asset('css/style.css') }}" rel="stylesheet" />

    // site/upload/favicon.ico
    <img src="{{ asset('../upload/favicon.ico') }}" />




    $target_dir = '/upload/favicon.ico'; // param in config

    $file = $this->getParameter('kernel.project_dir') . $target_dir;
    if(is_file($file)) dump('found: ' . $file);

1 Comment

$file = $this->getParameter('kernel.project_dir') . $target_dir; is giving me the correct path but symfony is not displaying it. 404 error. I also tried <img src="{{ asset('../upload/favicon.ico') }}" />; twig finds it but the view this is also not displaying it also; the ... seem to have disappeared and I get 404 with a path starting from public. So it seems that the controller works but not the view. I also tried to use the asset manager without any effect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.