Search code examples
phpimagedisplaytag

PHP Image Display using my code


First File

<body>

    <form action="_31_upload_file.php" method="post" enctype="multipart/form-data">
        File: <input type="file" name="file" id="file"><br>
        <input type="submit">
    </form>
<body>

Second File

<?php
if ($_FILES["file"]["error"] > 0) 
{
  echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
$img = $_FILES["file"]["tmp_name"];
echo $img;


}


?> 
<img src="<?php echo $img; ?>" height="200" width="200">

My program is working well. But help me regarding IMAGE display using this code.

I am using img tag, and src attribute as well. but image is not displayed on webpage.


Solution

  • Use this -

    <?php
        if ($_FILES["file"]["error"] > 0) 
        {
          echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
    
    
          $tmp_name = $_FILES["file"]["tmp_name"];
          $name = $_FILES['file']['name'];
          move_uploaded_file($tmp_name, "./$name");
    
          echo '<img src="'.$name.'" />';
        }
    ?>