php 在php中上传后如何显示图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21703154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:23:29  来源:igfitidea点击:

How to display image after upload in php?

phpimage

提问by user3214024

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

将图像上传到文件夹后。如何显示图像..

its my upload.php

它是我的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";
  }
?>

the image is uploading fine. but the image is not displaying. a small box only showing.

图片上传正常。但图像没有显示。一个只展示的小盒子。

this part not working...

这部分不起作用...

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

how to display the image after upload success?

上传成功后如何显示图片?

回答by Manu

Try this:

尝试这个:

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

回答by Mario Radomanana

As you've already put "upload" in $img

因为您已经在 $img 中放置了“上传”

$img="upload/".$image;

You don't need to put it in src anymore

你不再需要把它放在 src 中

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

回答by php_learner

I have tried your code and its working finely just need to change the line

我已经试过你的代码,它工作得很好,只需要改变行

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

to

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

回答by Alex94

Don't put $img in quotations. When you do it the output is simply $img, not upload/...

不要将 $img 放在引号中。当你这样做时,输出只是 $img,而不是上传/...

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

回声 < img src=" ' ,$img ,' ">;

回答by putvande

You mixed up some quotes. It should be :

你混淆了一些报价。它应该是 :

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

回答by ElendilTheTall

You need:

你需要:

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

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

回答by Varun Pradhan

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

我们必须把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>
  <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);
  }    } ?>
  //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);
  }    } ?>

回答by Yagnik joshi

Just do this

就这样做

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

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

我们必须取$file_name变量,因为.. ""(双引号)只能返回变量中的文本——它不能返回二进制数来显示图像!