retrieve image from folder in php codeigniter - php

imagedisplay.php(view)
<html>
<body>
<h3>Your file was successfully uploaded!</h3>
<?php print_r($upload_data); ?> </br>
<?php $str=base_url()."images/".$upload_data['file_name'] ?> </br>
<?php $str=str_replace('http://','',$str) ?>
<?php echo $str; ?>
<img src= '$str'/> </br>
For echo $str; I got the string i need to display th image
but when i pass it to img src.... i am not able to display it on the browswer
Is there any syntactical error or am i missing anything ...pls help?

Just a small syntax problem here.
Embed PHP echo command in the HTML code, like so:
<img src="<?php echo $str; ?>"/> </br>
or embed PHP echo short tags:
<img src="<?=$str?>"/> </br>
In other words: insert the PHP output at the positions, where you need it as HTML content.

Related

Why can't I insert an image into php?

trying to insert an image into some php. This code pulls from the database thumbnail page to show an item's details. It works fine when it's text "see it in 3d view" but when I try to insert a premade image in that location instead (a button jpg, aka "img src="#"), I'm getting an error. How can I do this correctly? Still learning the ins and outs of php and html, they don't always play the way I expect them to. Thanks for any help.
echo ("<br><img src= \"");
echo ($thumbnail);
echo (" \"><br><br><a href = \"");
echo ($photo);
echo ("\"><b>See it in 360 view</b></a></div>");
echo ("<div id=\"info\"; style=\"width:45%\"><br><br><div class = \"date\">");
echo ($date);
echo ("</div><br>");
echo ("<div class = \"blurbs\">");
echo ($sub);
echo ("<br><br><br>");
echo ($desc);
echo ("<br><br>");
echo ($hist);
echo ("<br><br><br><b>Provenance:</b><br>");
echo ($prov);
echo ("<br><br><b>Construction Label:</b><br>");
echo ($labl);
echo ("<br><br><br><br><b>");
echo ($cNum);
echo ("</b>");
<img src="#"> would never work. src="#" is a shortcut for "current page". e.g. browsers will try to use the current page's URL as the source for the image, which means it'll be trying to load a bunch of HTML as if it was a jpg/gif/png image. Since html isn't any of those, it'll just be a flat-out "this image contains errors" error.
Whatever you're putting in $thumbnail needs to be a proper url, e.g.
<img src="kittens.jpg">
<img src="http://example.com/kittens.jpg">
<img src="data:image/jpeg;base64,<?php echp base64_encode(file_get_contents('kittens.jpg')); ?>">
I would start out with cleaning up your file and remove some of the unneeded overhead (I personally love to have my controllers (Which is generating the output for my view files)
What is the output of this PHP file and what did you expect it to be?
<br><img src="<?= $thumbnail ?>">
<br><br><b>See it in 360 view</b>
</div>
<div id="info" style="width:45%"><br><br><div class = "date">
<?= $date ?>
</div><br>
<div class="blurbs">
<?= $sub ?>
<br><br><br>
<?= $desc ?>
<br><br>
<?= $hist ?>
<br><br><br><b>Provenance:</b><br>
<?= $prov ?>
<br><br><b>Construction Label:</b><br>
<?= $labl ?>
<br><br><br><br><b>
<?= $cNum ?>
</b>
a note to this is that Short Open tag which is enabled by default from PHP 5.4)
You should also look into using div or p tags instead of all the line breaks (it makes it easier for you to make changes to later on)

Retrieve blob image into html php

Display image from mysql.
<?php
if(isset($id1)){
$query="SELECT * FROM `newss` WHERE `id`=".$id1;
$result=mysql_query($query);
if(!$result){
echo "<h3>News Not Availabel</h3>";
}else{
while($row=mysql_fetch_array($result)){
echo "
<img src=data:image/png;<?php echo ".$row['imges']."; ?> />
<figure class=date>
<h2>".$row['description']."</h2>
<div class=blog-detail-meta>
<span class=date><span class=fa fa-file-o></span>".$row['dates']."</span>
</div>
</header>
<hr>
<p>".$row['para1']." </p>
<p>
".$row['para2']."
</p>
<p>
".$row['para3']."
</p>
";
}
}
}else{
header("location:index.php");
}
?>
Image not properly displayed.
I stored raw data blob (png format). I want to display image, but only symbols are displayed.
Try
echo "<img src=\"data:image/png;base64,".$row['imges']."\" />" ;
Instead of
echo "<img src=data:image/png;<?php echo ".$row['imges']."; ?> />" ;
Is the table name really "newss"?
Don't do things like:
echo "
HTML CODE
";
Do this:
?>
HTML CODE <?php echo $php_code;?>
<?php
You currently trying to use PHP opening Tag <?php within PHP self.
<?php
echo "<?php echo 'test';?>";
?>
PHP won't parse this, but will just echo it as pure text.
Change following line:
<img src=data:image/png;<?php echo ".$row['imges']."; ?> />
to:
<img src="data:image/png;".$row['imges'].";"/>
Also use double quotes for HTML-Parameters like src="path/to/file.format".
Not being lazyis very important in coding!
If everything does not work, what is the output of $row['imges'] ?

HTML inside php condition

The following works fine to show a source image.
<html>
<h3>First Test</h3>
<img src="example1.php" />
</html>
But I wanted to validate user, then only show source image like following,
<html>
<h3>First Test</h3>
<?php
some logic = $usermatch
if($usermatch)
<img src="example1.php" />
?>
</html>
When I try the same it simply doesn't show image and doesn't accept <img src="example1.php" /> inside the PHP code.
I am a beginner and just learning php and html.
Could you please guide me how to make it work?
Thanks.
You have to switch in and out of PHP mode
<html>
<h3>First Test</h3>
<?php if($usermatch) { ?>
<img src="example1.php" />
<?php } ?>
</html>
And some like to use echo statements, but you'll see that you get less help from editors when editing the HTML
<html>
<h3>First Test</h3>
<?php
if($usermatch)
echo '<img src="example1.php" />';
?>
A little change to your code, although this is rather an ugly way to do it.
<?php
some logic = $usermatch
if($usermatch) {
?>
<img src="example1.php" />
<?php
}
?>
Don't miss the php closing and opening tags. Try something like this
<?php
some logic = $usermatch
if($usermatch) : ?>
<img src="example1.php" />
<?php
endif;
?>
Try this:
<?php
some logic = $usermatch
if($usermatch) { ?>
<img src="example1.php" />
<?php
}
?>
The problem is that you are mixing your HTML & PHP code. It is perfectly aceptable to have HTML code in your PHP file, but you need to close the PHP code block before so that the interpreter understands that it is not code you intend to run.
Might as well add to all of the answers:
<html>
<h3>First Test</h3>
<?php echo $usermatch ? '<img src="example1.php" />' : ''; ?>
</html>
Use the following:
<html>
<h3>First Test</h3>
<?php
some logic = $usermatch
if($usermatch) {
print '<img src="example1.php" />'
}
?>
</html>
or you can use another way to print html tags in php:
<html>
<h3>First Test</h3>
<?php
some logic = $usermatch
if($usermatch) {
?>
<img src="example1.php" />
<?php } ?>
</html>

PHP spaces in HTML output when formatting code

Following code will produce unwanted whitespace between icons.
<div>
<img src="icon1.png" />
<img src="icon2.png" />
</div>
I need to keep image tags on single lines because I have some conditions in my .phtml file, it looks something like this:
<div>
<?php if ($condition1) : ?>
<img src="icon1.png" />
<?php endif ?>
<?php if ($condition2) : ?>
<img src="icon2.png" />
<?php endif ?>
</div>
I don't want to have all code messed up on a single line. Is there any solution for situations like this?
Apply font-size:0px; style to your div.
You may use echo to output parts of html code. You'll get something like this
<div>
<?php if (true) :
echo '<img src="icon2.png" />';
endif;
if (true) :
echo '<img src="icon2.png" />';
endif;
?>
</div>

Print an image in html using session variable from php

i have this code trying to print an image in an html using
session variable from a php.Here is my code:
</head>
<body>
<?php
session_id(1);
session_start();
echo $_SESSION['phname'];
?>
<img src="../uploads/$_SESSION['phname']" alt="photo" width="498" height="720" border="0" />
here:
session_id(1);
session_start();
echo $_SESSION['phname'];
i'm checking if my variable has passed
from the php and its ok.
and here
?>
<img src="../uploads/$_SESSION['phname']" alt="photo" width="498" height="720" border="0" />
i'm using php to print the image from the following source
src="../uploads/$_SESSION['phname']"
when $_SESSION['phname'] is my image's name but i'm not getting the image. Is there something wrong with my code or
is there any other way to print my image?
i try a lot of things and a lot of codes
i found on the net but nothing help me more.
You need to do your session_start before you output anything to the browser as it sends some headers.
And of course when you want to echo something in php, you need php tags and an echo statement...
<?session_start();?>
</head>
<body>
<?php
session_id(1);
echo $_SESSION['phname'];
echo "<img src=\"../uploads/". $_SESSION['phname']."\" alt=\"photo\" width=\"498\" height="720" border="0" />";
?>

Categories