当前位置:Gxlcms > PHP教程 > php代码实现文件的预览

php代码实现文件的预览

时间:2021-07-01 10:21:17 帮助过:4人阅读

这篇文章主要为大家详细介绍了php实现文件预览功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

上一篇博客是上传功能,本篇是上传后图片预览和更改:

代码如下:

1.yulan.php


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>无标题文档</title>
  6. <style type="text/css">
  7. #yl{ width:200px; height:300px; background-image:url(images/timg1.jpg); background-size:200px 300px;}
  8. #file{ width:200px; height:300px; float:left; opacity:0;}
  9. </style>
  10. </head>
  11. <body>
  12. <form id="sc" action="ylchuli.php" method="post" enctype="multipart/form-data" target="shangchuan">
  13. <input type="hidden" name="tp" value="" id="tp" />
  14. <p id="yl">
  15. <input type="file" name="file" id="file" onchange="document.getElementById('sc').submit()" />
  16. </p>
  17. </form>
  18. <iframe style="display:none" name="shangchuan" id="shangchuan">
  19. </iframe>
  20. </body>
  21. <script type="text/javascript">
  22. //回调函数,调用该方法传一个文件路径,改变背景图
  23. function showimg(url)
  24. {
  25. var p = document.getElementById("yl");
  26. p.style.backgroundImage = "url("+url+")";
  27. document.getElementById("tp").value = url;
  28. }
  29. </script>
  30. </html>

2.ylchuli.php


  1. <?php
  2. if($_FILES["file"]["error"])
  3. {
  4. echo $_FILES["file"]["error"];
  5. }
  6. else
  7. {
  8. if(($_FILES["file"]["type"]=="image/jpeg" || $_FILES["file"]["type"]=="image/png")&& $_FILES["file"]["size"]<1024000)
  9. {
  10. $fname = "./images/".date("YmdHis").$_FILES["file"]["name"];
  11. $filename = iconv("UTF-8","gb2312",$fname);
  12. if(file_exists($filename))
  13. {
  14. echo "<script>alert('该文件已存在!');</script>";
  15. }
  16. else
  17. {
  18. move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
  19. $delurl = iconv("UTF-8","gb2312",$_POST["tp"]);
  20. unlink($delurl); //删除文件
  21. echo "<script>parent.showimg('{$fname}');</script>";
  22. }
  23. }
  24. }

以上就是php代码实现文件的预览的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行