php 图片上传并预览效果

本文章是一款图片上传代码,他可以把上传到服务器的图片在进行预览修改,如果是你想的你就可以保存了,不是可以删除再重新上传,实例代码如下:

  1. if(!isset($_session))session_start();
  2. /**2010-6-22
  3. * $data 数组类型 包含以下变量
  4. -------------------------------
  5. * $sub_type submit类型(upload:上传按钮 delete:删除按钮),默认upload
  6. * $file 通过表单获取的$_files['filename']数组
  7. * $img_tag_id 预览图片的<img>的id值
  8. * $img_dir 上传图片的目录
  9. * $max_file_size 单位m(兆),默认:1m
  10. * $type_array 允许的上传的图片类型(默认:image/pjpeg、image/jpeg、image/gif)
  11. **/
  12. function upload($data){
  13. if(!$data['file']){
  14. echo '<script>alert("file不能为空!");</script>';exit;
  15. }
  16. if(!$data['img_tag_id']){
  17. echo '<script>alert("预览图片标签id不能为空!");</script>';exit;
  18. }
  19. if(!$data['img_dir']){
  20. echo '<script>alert("图片上传目录不能为空!");</script>';exit;
  21. }
  22. if(!isset($data['max_file_size'])){
  23. $data['max_file_size'] = 1024 * 1024;
  24. }else{
  25. $data['max_file_size'] = $data['max_file_size'] * 1024 * 1024;
  26. }
  27. if(!isset($data['type_array'])){
  28. $data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif');
  29. }
  30. if(!isset($data['sub_type'])){
  31. $data['sub_type'] = 'upload';
  32. }
  33. $imgpath = '';
  34. if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){
  35. if(isset($_session['name']) && $_session['name']){
  36. if(is_file($_session['imgpath'])){
  37. $b = unlink($_session['imgpath']);
  38. }
  39. unset($_session['name'], $_session['imgpath']);
  40. if(!isset($_session['name'])){
  41. echo '<script>alert("删除成功!");</script>';
  42. echo '<script>parent.document.getelementbyid("'.$data['img_tag_id'].'").style.display = "none";</script>';
  43. }else{
  44. echo '<script>alert("删除失败!");</script>';
  45. }
  46. }else{
  47. echo '<script>alert("没有稿件!");</script>';
  48. }exit;
  49. }
  50. if(isset($_session['imgpath']) && $_session['imgpath']){
  51. echo '<script>alert("稿件已经存在,要想重新上传请删除原来的稿件!");</script>';exit;
  52. }
  53. if(!in_array($data['file']['type'], $data['type_array'])){
  54. echo '<script>alert("稿件类型不匹配,请上传.jpg、.gif和.png格式的图片!");</script>';exit;
  55. }
  56. if($data['file']['size'] > $data['max_file_size']){
  57. echo '<script>alert("您上传的稿件过大,请选择2m以下的图片上传!");</script>';exit;
  58. }
  59. if(!is_dir($data['img_dir'])){
  60. @mkdir($data['img_dir'], 0777, true);
  61. }
  62. $imgpath = $data['img_dir'].'/'.date('his', time()).rand(100, 999).$data['file']['name'];
  63. $isupload = move_uploaded_file($data['file']['tmp_name'], $imgpath);
  64. if(!$isupload){
  65. echo '<script>alert("稿件上传失败,请尝试重新上传!");</script>';exit;
  66. }else{
  67. echo '<script>alert("稿件上传成功!");</script>';
  68. }
  69. $_session['name'] = $data['file']['name'];
  70. $_session['imgpath'] = $imgpath;
  71. return $imgpath;
  72. }
  73. /*test_start*/
  74. $sub_type = '';
  75. if(isset($_post['submit_upload']))$sub_type = 'upload';
  76. if(isset($_post['submit_delete']))$sub_type = 'delete';
  77. if($sub_type){//echo '<script>alert("'.$sub_type.'");</script>';exit;
  78. $data = array( 'sub_type' => $sub_type,
  79. 'file' => $_files['file'],
  80. 'img_tag_id' => 'picview',
  81. 'img_dir' => 'upload_img',
  82. );
  83. $imgpath = upload($data);
  84. }else{
  85. $imgpath = isset($_session['imgpath'])? $_session['imgpath']: '';
  86. }
  87. ?>
  88. <form action="ad.php" method="post" enctype="multipart/form-data" target="frame">
  89. <input type="file" name="file" class="input">
  90. <input type="submit" name="submit_upload" value="上 传">
  91. <input type="submit" name="submit_delete" value="删 除">
  92. <iframe name="frame" width="0" height="0" marginwidth="0" frame src="about:blank"></iframe>
  93. </form>
  94. <img height="100" >
  95. //开源代码phpfensi.com
  96. <script>
  97. if("<?php echo $imgpath; ?>"){
  98. parent.document.getelementbyid("picview").src = "<?php echo $imgpath; ?>";
  99. parent.document.getelementbyid("picview").style.display = "block";
  100. }
  101. </script>