Please help me with this guys,
I have this code:
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']}")
{
echo '<center><img src="admin/gallery/{$_FILES['userfile']['name']}" width="100" height="100"/>'.'</center>';
// echo "success!:)";
}
else
{
echo "photo not uploaded";
}
the problem here is when i echo it and use its tmpname i am having an error in this section
<img src="admin/gallery/{$_FILES['userfile']['name']}" width="100" //....
the userfile is underlined in red. Is it wrong to use its tempname in echoing it?
help me please...
Change your echo statement like this,
echo '<center><img src="admin/gallery/'.{$_FILES['userfile']['name']}.'" width="100" height="100"/>'.'</center>';
Also your if checks showing incorrectly.
May be like this,
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']")
{
.....
}
try with proper quoting and if()
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/".$_FILES['userfile']['name']))
{
echo '<center><img src="admin/gallery/'.$_FILES['userfile']['name'].'" width="100" height="100"/>'.'</center>';
// echo "success!:)";
}else
echo "photo not uploaded";
}
you just have a problems with your ' and "
i would do it like this
$filename = $_FILES['userfile']['name'];
echo "<center><img src='admin/gallery/{$filename}' width='100' height='100' /></center>";
this should do it
total source is
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']}")
{
$filename = $_FILES['userfile']['name'];
echo "<center><img src='admin/gallery/{$filename}' width='100' height='100' /></center>";
}
else
{
echo "photo not uploaded";
}
Try this. You can use php in html attributes and tags
<img src="admin/gallery/<?php {$_FILES['userfile']['name']} ?>" width="100" />
You should use:
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"C:/xampp/htdocs/new/admin/gallery/{$_FILES['userfile']['name']}")
And
echo "<img src='admin/gallery/{$_FILES['userfile']['name']}' width='100'...";
or better close php with an ?>then
<img src='admin/gallery/<?php echo $_FILES['userfile']['name'];?>' width='100'...
Related
How to display the particular image in case if that image is not available in database? I have
database name:project
table name: image
fields: id (int), file (varchar) [image url stored here], name (varchar) [image description]
PHP code is here:
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $row["file"]; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
simply use #getimagesize description w3school php.net this method will check if image actually exists . will return false if image deleted from db or from destination.
<?
$img="image url"; //orginal image url from db
if(!#getimagesize($img))
{
$img="default image" //if image not found this will display
}
?>
Update
for your code use like this
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
$img=$row["file"]; //orginal image url from db
if(!#getimagesize($img))
{
$img="default image" //if image not found this will display
}
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $img; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
Use a simple if condition
<img src="<? php if($row["file"]){ echo $row["file"] ; } else{// other image name}?>" height="100px" width="150px">
You can use ternary operator to do this, refer below and give a try
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
// USE TERNARY OPERATOR HERE
$imagePath = (isset($row["file"]) && !empty($row["file"]) && file_exists($row["file"]))?$row["file"]:'img.default.jpg'; // REPLACE YOUR IMAGE PATHE HERE
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $imagePath; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
If the file reference in the database is pointing to a file that may have been deleted, you have to check if the file still exists. Ideally, when he file is deleted, the reference in the database should be updated too, to avoid such problems, but that may not always be possible, especially when many people have access to FTP.
Check to see if file exists:
if(file_exists($yourImage)){
echo $yourImage;
}
else{
echo $defaultImage;
}
You're storing the directory to the image within the varchar? if so try changing your database to store the image itself using BLOB as explained here
Then you can use an if statement.
<img src="<? php if($row["file"]){echo $row["file"];} else { echo '/directory/to/image.jpg'}?>" height="100px" width="150px">
I would also recommend using a include, this may not be 100% relevant for your current project but can help down the line when needing to use more than one database connection.
create a dbconnect.php file //name it what ever you want
insert code into dbconnect.php file:
<? php $con=mysql_connect("localhost","root","") or die ("no connection"); mysql_select_db("project")or die("no database exit");?>
in any file you're trying to use a database use include_once 'dbconnect.php'
Currently I'm trying to display an image from the database i created, using phpmyadmin to test my script. The database contains images in png and jpg. But the output are just nonsense characters because the browser cannot render the image. Can some one tell me where I get wrong?
I google the problem and I can see their pattern and mine are similar so I don't really under stand where I get wrong. I can get the name of the image in database, but not the image.
this is my script:
<html>
<head></head>
<body>
<?php
$con = mysqli_connect("localhost", "Dave", "password");
if (!$con){
die ("Could not connect to database: " . mysqli_connect_error());
}
mysqli_select_db($con, "snippet");
$res = mysqli_query($con, "select * from image");
echo "<table>";
while ($row = mysqli_fetch_array($res)){
echo "<tr>";
echo "<td>"; ?> <img src="<?php echo $row["image"]; ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["image_name"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
What format is your imaged stored as?
Raw bytes?
Base-64?
or a URL?
If it's a URL then just use the attribute 'src' of the html img element.
If it's Base-64 then use
<img src="data:image/jpg;base64,THE BASE 64 code here" />
If you have raw bytes, either convert them to Base-64 with PHP
$base64segment = base64_encode($data);
or you can call a custom url within the src attribute that will lead to a PHP script where you can create an image on the fly from the bytes by sending a custom header followed by the bytes:
<?php
header('Content-type: image/jpeg');
header('Content-Length: '.strlen($data));
echo $data;
?>
The disadvantage of the Base-64 based approach is that the data takes more space than the original binary segment because the encoding is done using 64 different signs instead of 256.
With this:
<img src="<?php echo $row["image"]; ?>
You are setting the source of the image. This should be either an URL or a path from your server root. What I usually did was save the file location and name (so for example "/images/imagename.png") in the database.
If you used MIME type or BLOB in the database, this code will not work.
Have you tried this?
$base64 = 'data:image/PNG;base64,' . base64_encode($row['image']);
echo "<img src=$base64 />" ;
In your code...
while ($row = mysqli_fetch_array($res)){
echo "<tr>";
echo "<td>"; ?> <img src="<?php echo data:image/PNG;base64,'. base64_encode($row["image"]); ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["image_name"]; echo "</td>";
echo "</tr>";
}
<html>
<head></head>
<body>
<?php
$con = mysqli_connect("localhost", "Dave", "password");
if (!$con){
die ("Could not connect to database: " . mysqli_connect_error());
}
mysqli_select_db($con, "snippet");
$res = mysqli_query($con, "select * from image");
echo "<table>";
while ($row = mysqli_fetch_array($res)){
$row["image"] = "data:image/png;base64,".base64_encode($row["image"]); //the trick!
echo "<tr>";
echo "<td>"; ?> <img src="<?php echo $row["image"]; ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["image_name"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
You need to store image MIME type in database, then you can display image like this:
echo $row["image"];
header("Content-Type: ".$row['mime_type']);
For example, PNG's MIME type is image/png;
What I want to do is that, It should display 3 images on every line and along with their captions.And their could be many rows. How can do that ? here is my code..
while($info=mysql_fetch_array($query))
{
$image=$info['image'];
$cap=$info['caption'];
echo '<img src="'.$image.'" />';
echo $cap;
}
I got the answer.
I don't know hows its working . But it is ....
<table>
<?php
$i=1;
$query=mysql_query("SELECT * FROM publish");
while($info=mysql_fetch_array($query))
{
$inq=$info['ref'];
$imagesrc=$info['image'];
$ti=$info['title'];
if($i%3==1 || $info['id']==1 )
{
echo "<tr></tr>";
}
?>
<td>
<a href="movieview.php?ref=<?php echo $info['ref']; ?>">
<?php
echo '<img src="admin/'.$imagesrc.'" style="width:138px;height:200px;" />';
echo '</a><br />';
echo $ti;
$i++;
}
?>
</td>
</table>
i have this code. how i can make the image be at the center and the text make it more bigger?
if ($spell_checker->check_word($_GET['wordtext'])) {
echo '<img src="images/betul.jpg" width="100" height="100" >'.'<br />';
echo 'Tahniah, Ejaan Anda Tepat Sekali!';
}
else {
echo '<img src="images/salah.jpg" width="100" height="100">'.'<br />';
echo 'Opss.Ejaan Yang Betul Ialah: ';
print_r(implode($spell_checker->suggest($_GET['wordtext'])));
}
Hello im using page nation which is amazing but now im trying to print off there avaratar im using this code
echo "<IMG SRC=\"$list['avatar']\" WIDTH=\"268\" HEIGHT=\"176\"
BORDER=\"0\" ALT=\"\" \/>";;
but im getting this error
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in test.php on line 64
Try
echo "<IMG SRC=\"".$list['avatar']."\" WIDTH=\"268\" HEIGHT=\"176\"
BORDER=\"0\" ALT=\"\" />";
instead, or you could use this one, too
echo "<IMG SRC=\"{$list['avatar']}\" WIDTH=\"268\" HEIGHT=\"176\"
BORDER=\"0\" ALT=\"\" />";
or better and readable ones:
echo '<IMG SRC="'.$list['avatar'].'" WIDTH="268" HEIGHT="176" BORDER="0" ALT="" />';
echo '<IMG SRC="', $list['avatar'], '" WIDTH="268" HEIGHT="176" BORDER="0" ALT="" />';
I prefer this
echo "<IMG SRC=\"$list[avatar]\" WIDTH=\"268\" HEIGHT=\"176\" BORDER=\"0\" ALT=\"\" />";
eliminate de braces surrounding the variable and the single quotes for the array key
this should work :
echo '<IMG SRC="'.$list['avatar'].'" WIDTH="268" HEIGHT="176"
BORDER="0" ALT="" />';
to avoid all thet mess
?><IMG SRC="<?=$list['avatar']?> " WIDTH="268" HEIGHT="176" BORDER="0" ALT="" /><?php
and don't post your usual "parse error" comment here.
but check your other PHP syntax issue somewhere else
Try changing it to:
echo "<IMG SRC=\"{$list['avatar']}\" WIDTH=\"268\" HEIGHT=\"176\" BORDER=\"0\" ALT=\"\" />";
...or, less confusingly:
echo '<IMG SRC="'.$list['avatar'].'" WIDTH="268" HEIGHT="176" BORDER="0" ALT="" />';
Try it like this:
echo '<IMG SRC="'.$list['avatar'].'" WIDTH="268" HEIGHT="176" BORDER="0" ALT="" />';