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

时间:2014-02-11 13:25:56

标签: php image

将图像上传到文件夹后

。如何显示图像..

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

图片上传很好。但图像没有显示。一个小盒子只显示。

这部分不起作用......

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

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

8 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:1)

因为您已经将“上传”放入$ img

$img="upload/".$image;

您不再需要将它放在src中

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

答案 2 :(得分:1)

我已经尝试过您的代码,并且其工作正常,只需要更改行

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

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

答案 3 :(得分:0)

你需要:

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

答案 4 :(得分:0)

你混淆了一些引用。它应该是:

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

答案 5 :(得分:0)

不要将$ img加入报价单。当你这样做时,输出只是$ img,而不是上传/...

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

答案 6 :(得分: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);
  }    } ?>

答案 7 :(得分:-3)

就这样做

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

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