file get contents - Assign Image SRC in php


After retrieve image path from database I want to pass it to<img> tag to show up, I read this solution, but I do not want toecho it, I want to assign it assrc for image tag

I tried this:

$image = mysqli_fetch_array($user_images);
$img1 = $image['file_path'];
$imageData = base64_encode(file_get_contents($img1));
$src = 'data: '.mime_content_type($img1).';base64,'.$imageData;

How can I assign $src to image, I tried this but no luck :(

<img id="img1" width="150" height="150" src="' . $src . '"/>

Thanks in advance.




Inside the HTML you still need toecho the value of$src. This should work:

<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>



Try with PHP tags like below:-

<img id="img1" width="150" height="150" src="<?php echo $src;?>"/>



Reasonable solution I can think of is

<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>

but the code where you get the image source has to be above the image tag, so you have the$src.




You are using PHP language and in Php, you can't pass variable directly in the Tag you have to set a variable in the PHP environment. To create Php environment you can right<?php ?> or you can use to echo variable<?= ?> between these PHP tags you can pas your variables to echo and assign or anything else what you want to do.

<img id="img1" width="150" height="150" src="<?= $src ?>"/>

People are also looking for solutions to the problem: html - How to change background image with the help of url parameter in php?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.