先上源码:upload_file.php

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file"  /> 
<br />
<input type="submit" name="submit" value="Submit" />
<br/>
<img name="img" style="width: 100px; height: 100px;
border: 1px solid red;" src ="<?php echo "upload/" . $_FILES["file"]["name"]; ?>" />

</form>

<?php
if ((($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000000))
  {
  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"] . " 已经保存. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "存入: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }


?>

</body>
</html>

显示效果如图;

未上传:

php原生实现图片上传和查看

上传提交但没有该图片:

php原生实现图片上传和查看

上传提交但已有该图片:

php原生实现图片上传和查看

 

相关文章:

  • 2022-12-23
  • 2021-12-05
  • 2022-01-01
  • 2021-12-05
  • 2022-12-23
  • 2021-11-20
猜你喜欢
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
相关资源
相似解决方案