How to display image after upload in php?

after uploading the image to a folder. how to display an image.

its my upload.php

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000000000000000000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"]."<br>";

$image=$_FILES["file"]["name"]; /* Displaying Image*/
      $img="upload/".$image;
      echo '<img src= "upload/".$img>'; 

      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

image is uploaded fine. but the image is not displayed. only a small box.

this part does not work ...

/* Displaying Image*/
       $image=$_FILES["file"]["name"]; 
              $img="upload/".$image;
              echo '<img src= "upload/".$img>';

How to display image after successful upload?

+3
source share
8 answers

Try the following:

header('Content-Type: image/jpeg');
readfile($imgPath);
+1
source

As you already put "upload" in $ img

$img="upload/".$image;

You no longer need to put it in src

echo '<img src= "'.$img.'">';
+1
source

echo'<img src= "upload/".$img>'; 

echo'<img src="'.$img.'">';
+1

:

echo '<img src="upload/'.$img.'"/>';

0

. :

$image = $_FILES["file"]["name"]; 
$img = "upload/".$image;
echo "<img src=\"upload/$img\">";
0

$img . , $img, upload/...

echo < img src=" ' ,$img ,' ">;

0

enctype = "multipart/form-data"

  <html>
  <body>
  <form action = "" method = "POST" enctype = "multipart/form-data">
     <input type = "file" name = "image" />
     <input type = "submit"/>

     <ul>
        <li>Sent file:  echo $_FILES['image']['name'];  
        <li>File size:  echo $_FILES['image']['size'];  
        <li>File type:  echo $_FILES['image']['type'] 
        <li><img src=" echo 'images/'.$file_name; "> 
     </ul>

  </form>
      </body> </html>

  //file_upload.php

  if(isset($_FILES['image'])){$errors= array();

  $file_name = $_FILES['image']['name'];

  $file_size = $_FILES['image']['size'];

  $file_tmp = $_FILES['image']['tmp_name'];

  $file_type = $_FILES['image']['type'];

  $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

  $extensions= array("jpeg","jpg","png");

  if(in_array($file_ext,$extensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  }

  if($file_size > 2097152) {
     $errors[]='File size must be excately 2 MB';
  }

  if(empty($errors)==true) {
     move_uploaded_file($file_tmp,"images/".$file_name);
     echo "Success";
  }else{
     print_r($errors);
  }    } ?>
0
source

Just do it

echo '<img src="upload/'.$file_name.' "/>

We must take a variable $file_namebecause .. ""(double quotes) can only return text in a variable - it cannot return a binary number to display an image!

-3
source

All Articles