Reputation: 1828
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���&*!J�!��Q�EEȠ�����Q,������������{�kּ������>�����H3Q5��B�������.@�$p��d!s�#��~<<+"���x���M��0���B�\���t�8K��@z�B��@F���&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��,�="">
Upvotes: 2
Views: 2103
Reputation: 653
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");
}
}
Upvotes: 3
Reputation: 639
// 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);
Upvotes: 0