I Have a problem while uploading image to my php page .. i tried this code and its worked and the image uploaded but i can't show it on the page ..
please help ^^
if ($_FILES["img"]["name"]) {
$name = $_FILES["img"]["name"];
$tmp_name = $_FILES['img']['tmp_name'];
$error = $_FILES['img']['error'];
if (!empty($name)) {
$location = '/var/www/html/1.jpg';
if (move_uploaded_file($tmp_name, $location.$name)){
echo 'Image Uploaded';
echo nl2br("\n");
echo nl2br("\n");
echo <<<HEREDOC
<div style="float:left;margin-right:10px">
<img src="{$location}" alt = "Ur Image" width="400" height="400"/>
</div>
HEREDOC;
}
}
else {
echo 'please choose a file';
}
}
I have a feeling that the issue is related to your $location variable. When you save the file, you save it to /var/www/html/1.jpg1.jpg since you do $location.$name, but when you try and display it, you only use the $location, which is /var/www/html/1.jpg.
Essentially, change:
<img src="{$location}" alt = "Ur Image" width="400" height="400"/>
to
<img src="{$location.$name}" alt = "Ur Image" width="400" height="400"/>
EDIT
And as meta pointed out, don't use /var/www/html/... in the image source. If /var/www/html is the root of the website where the PHP file lives, change yout $location variable to
$location = '/var/www/html/';
And change
<img src="{$location}" alt = "Ur Image" width="400" height="400"/>
to
<img src="{$name}" alt = "Ur Image" width="400" height="400"/>
Don't use physical location /var/www... in src, use URL instead (which would depend on your http server configuration). But you can try something like http://your.domain/1.jpg
or localhost/i.jpg or you could even try relative path src="/1.jpg" or src="html/1.jpg"
make sure the image is uploading where you think it is suppoed to go
if(isset($_POST['addImage'])){
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
$target_dir = "$myfolder/";
$target_file = $target_dir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $target_file);
} else {
die('<br>An error occurred importing the file: '.$_FILES['userfile']['error']);
}
then show image with src='$target_file'
Related
Good day everyone, so I have a code here for my site for uploading images to customer profile photos, but if they haven't uploaded yet it shows a broken image, how do I put a placeholder instead of a broken image.
<div class="panel-body">
<a data-target="#myModal" data-toggle="modal" href=
""><img class="img-hover" src="<?php echo web_root. "customer/".$res->CUSPHOTO; ?>"
style="width:100%; height:100%;text-align:center" title=
"profile image"></a>
</div>
This is my code for uploading the image
function doupdateimage(){
$errofile = $_FILES['photo']['error'];
$type = $_FILES['photo']['type'];
$temp = $_FILES['photo']['tmp_name'];
$myfile =$_FILES['photo']['name'];
$location="customer_image/".$myfile;
if ( $errofile > 0) {
message("No Image Selected!", "error");
redirect(web_root. "index.php?q=profile");
}else{
#$file=$_FILES['photo']['tmp_name'];
#$image= addslashes(file_get_contents($_FILES['photo']['tmp_name']));
#$image_name= addslashes($_FILES['photo']['name']);
#$image_size= getimagesize($_FILES['photo']['tmp_name']);
if ($image_size==FALSE ) {
message(web_root. "Uploaded file is not an image!", "error");
redirect(web_root. "index.php?q=profile");
}else{
//uploading the file
move_uploaded_file($temp,"customer_image/" . $myfile);
$customer = New Customer();
$customer->CUSPHOTO = $location;
$customer->update($_SESSION['CUSID']);
redirect(web_root. "index.php?q=profile");
Assuming that "basically has no value" means NULL you could check for that value:
<?php
if (is_null($res->CUSPHOTO)) {
$url = web_root. "customer/anonymous.jpg";
}
else {
$url = web_root. "customer/".$res->CUSPHOTO;
}
?>
<div class="panel-body">
<a data-target="#myModal" data-toggle="modal" href="">
<img class="img-hover"
src=<?php echo '"'.$url.'"' ?>
style="width:100%; height:100%;text-align:center"
title="profile image">
</a>
</div>
Here anonymous.jpg is the image to be displayed when no photo has been uploaded.
Opinion: Personally I don't like mixing the PHP tags into HTML like that. You never know exactly what will happen. I always write either HTML or PHP, not mixed, but I know it can be done. That's the reason I do the strange thing with the quotes around $url, I really don't like a PHP tag inside a HTML string.
How do I properly echo images combining HTML and PHP? $offer['picture'] has link saved as example.com/picture.png. I've tried many different options, but nothing works. Can anyone help me out?
foreach($json['offers'] as $offer) {
$image = $offer['picture'];
?>
<img src="<?php echo $image ?>">
<?php
}
If there is only example.com/picture.png in $offer['picture'], problem is that images linked incorrectly. You should add http:// before image link to make browser sure you are loading image by absolute path.
foreach($json['offers'] as $offer) {
$image = $offer['picture'];
?>
<img src="http://<?php echo $image ?>">
<?php
}
Well it depends on your directory structure.
suppose your images are in example.com/assets/images/photo-1.jpg
your code should be
<?php foreach($json['offers'] as $offer) {
$image = $offer['picture']; ?>
<img src="http://www.example.com/assets/images/<?php echo $image; ?>">
<?php } ?>
In fact, you need to concatenate or hard code the path and image name will be appended dynamically.
Try this syntax,
<?php
foreach($json['offers'] as $offer) {
$image = $offer['picture'];
echo "<img src='$image' />";
}
?>
I want to show a default image if there isn't any record on the table of images. I am using this one, but it shows nothing when the db's table is empty.
<img class="image--cover" id="blah" src="<?php echo $row2['img_local']; ?>" alt="Avatar" title="DEFINIDA" onerror="this.onerror=null;this.src='1.png';">
Actually, it doesn't even shows some broken image icon, not even the allocated space for this element. Any thoughts?
Edit: this is the "upload.php" code that i am using to upload the images
<?php
session_start();
include('includes/conexao.php');
$fileinfo=PATHINFO($_FILES["image"]["name"]);
$newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
move_uploaded_file($_FILES["image"]["tmp_name"],"images/perfis/" . $newFilename);
$location="images/perfis/" . $newFilename;
$todas_fotos = mysqli_query($conexao, "select * FROM esc_usuarios_fotos WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
if( mysqli_num_rows($todas_fotos) > 0) {
//$path=$location;
//if(unlink($path)) echo "Deleted file ";
mysqli_query($conexao,"UPDATE esc_usuarios_fotos SET img_local = '$location' WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
}
else
{
mysqli_query($conexao,"insert into esc_usuarios_fotos (img_local, img_usu_codigo) values ('$location', '" . $_SESSION['codigo'] . "')");
};
header('location:perfil.php');
?>
you can try
<img class="image--cover" id="blah" src="<?php if(!empty($row2['img_local'])){ echo $row2['img_local'];}else{ echo '1.png';} ?>" alt="Avatar" title="DEFINIDA" >
Also check path of 1.png.. Like images/1.png or img/1.png etc..
we can try
set by-default value to database column img_local (Ex. default.png) , so if user not change image or there is no insert image in database it's remain default.png ,if user change it become user.png , if user remove image it's again update to default.png
Please try the below logic:
<?php
$row2['img_local'] = 'DB_data_folder_Path_to_image_with_image_name.extension';
// You do not have to use above line.
if ((!empty($row2['img_local'])) && (file_exists($row2['img_local']))) {
echo '<img class="image--cover" id="blah" src="'.$row2['img_local'].'" alt="Avatar" title="DEFINIDA" onerror="this.onerror=null;this.src=1.png;">';
} else {
echo 'Show a default image.'; // <img src="path_to_default_image" alt="Default_image"/>
}
?>
My friend,
If you get this from HTML perspective... and put that image in one DIV, your [onerror="";] could work better.
like this example:
<div id="myContainerImg">
<img class="image--cover" id="blah" src="<?php echo $row2['img_local']; ?>" alt="Avatar" title="DEFINIDA" onerror="putNoImage();">
</div>
<script type="text/javascript">
function putNoImage(){
document.getElementById('myContainerImg').innerHTML = 'no image here, or put your own img...';
}
</script>
And, you can see it running here on CodePen.
I'm coding a site in WordPress and I'd like to have a bookmark star shown before a course name.
The thing is that the code works fine, I can see that it translates to the right place where the image is, but the image appears to not be found.
Is there any configuration I need to do to make this image to show or anything I'm doing wrong?
Code:
$estrela = get_stylesheet_directory() . '/images/estrela.png';
$estrelaFavorito = get_stylesheet_directory() . '/images/estrela-favorito.png';
if ($isFavorito) {
$img = $estrelaFavorito;
} else {
$img = $estrela;
}
if (is_user_logged_in()) { ?>
<div class="add-remove-bookmark" onclick="addToBookmark(<?php echo $user_id ?>, 'course', <?php echo $course_id ?>)" >
<img src="<?php echo $img; ?>" alt="favoritos" width="20" height="20"/>
</div>
<?php }
Results in:
<div class="add-remove-bookmark" onclick="addToBookmark(x, x, x)">
<img src="/home/xxx/www/wp-content/themes/wplms_child/images/estrela-favorito.png" alt="favoritos" width="20" height="20">
</div>
Edit:
If I change to the code below, it works, but not really best practice:
$estrela = 'https://www.xxx.com.br/wp-content/themes/wplms_child/images/estrela.png';
$estrelaFavorito = 'https://www.xxx.com.br/wp-content/themes/wplms_child/images/estrela-favorito.png';
Solution:
I've replaced the variables $estrela and $estrelaFavorito for the code below and now it works!
$estrela = get_stylesheet_directory_uri() . '/images/estrela.png';
$estrelaFavorito = get_stylesheet_directory_uri() . '/images/estrela-favorito.png';
Replace:
//Returns an absolute server path
get_stylesheet_directory()
by
// return theme directory url
get_stylesheet_directory_uri()
https://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri
My script uploads the selected pictures to a folder but in my other page I have to see it. But I can't see my uploaded picture.
But if I manually drag and drop the picture from the desktop to the folder then I can see the picture on the page.
This is my code where I want show the uploaded picture:
$image = "../images/accommodatie/".$row2['acco_id']."/";
$images = glob($image."*.jpg");
sort($images);
if (count($images) > 0) {
$img = $images[0];
$img = str_replace("../","", $img);
echo "<a href='acco.php'>
<div>
<div>
<img src='$img'>
</div>";
}
I checked a few times, The folder location is correct, so that can not be the problem.
You're missing and closing double quote and a semi colon after your echo :
if (count($images) > 0) {
$img = $images[0];
$img = str_replace("../","", $img);
echo "<a href='acco.php'>
<div>
<div>
<img src='$img'>
</div>"; //<----- SEMI COLON HERE
}