How to display image after upload in php?

后端 未结 8 1953
無奈伤痛
無奈伤痛 2021-01-14 08:30

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

its my upload.php



        
相关标签:
8条回答
  • 2021-01-14 09:11

    We have to Put enctype = "multipart/form-data" inside the form

      <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>
    

    We Have

      //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 讨论(0)
  • 2021-01-14 09:11

    Just do this

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

    We have to take the $file_name variable because.. "" (double quotes) can only return text in the variable - it cannot return binary number to show image!

    0 讨论(0)
提交回复
热议问题