国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php 圖片上傳并預覽效果

2024-09-04 11:44:32
字體:
來源:轉載
供稿:網友

本文章是一款圖片上傳代碼,他可以把上傳到服務器的圖片在進行預覽修改,如果是你想的你就可以保存了,不是可以刪除再重新上傳,實例代碼如下:

  1. if(!isset($_session))session_start(); 
  2.  
  3. /**2010-6-22 
  4.  * $data 數組類型    包含以下變量 
  5.  ------------------------------- 
  6.  * $sub_type        submit類型(upload:上傳按鈕    delete:刪除按鈕),默認upload 
  7.  * $file            通過表單獲取的$_files['filename']數組 
  8.  * $img_tag_id        預覽圖片的<img>的id值 
  9.  * $img_dir            上傳圖片的目錄 
  10.  * $max_file_size    單位m(兆),默認:1m 
  11.  * $type_array        允許的上傳的圖片類型(默認:image/pjpeg、image/jpeg、image/gif) 
  12.  **/ 
  13. function upload($data){ 
  14.     if(!$data['file']){ 
  15.         echo '<script>alert("file不能為空!");</script>';exit
  16.     } 
  17.     if(!$data['img_tag_id']){ 
  18.         echo '<script>alert("預覽圖片標簽id不能為空!");</script>';exit
  19.     } 
  20.     if(!$data['img_dir']){ 
  21.         echo '<script>alert("圖片上傳目錄不能為空!");</script>';exit
  22.     } 
  23.     if(!isset($data['max_file_size'])){ 
  24.         $data['max_file_size'] = 1024 * 1024; 
  25.     }else
  26.         $data['max_file_size'] = $data['max_file_size'] * 1024 * 1024; 
  27.     } 
  28.     if(!isset($data['type_array'])){ 
  29.         $data['type_array'] = array('image/pjpeg''image/jpeg''image/gif'); 
  30.     } 
  31.     if(!isset($data['sub_type'])){ 
  32.         $data['sub_type'] = 'upload'
  33.     } 
  34.  
  35.     $imgpath = ''
  36.     if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){ 
  37.         if(isset($_session['name']) && $_session['name']){ 
  38.             if(is_file($_session['imgpath'])){ 
  39.                 $b = unlink($_session['imgpath']); 
  40.             } 
  41.             unset($_session['name'], $_session['imgpath']); 
  42.             if(!isset($_session['name'])){ 
  43.                 echo '<script>alert("刪除成功!");</script>'
  44.                 echo '<script>parent.document.getelementbyid("'.$data['img_tag_id'].'").style.display = "none";</script>'
  45.             }else
  46.                 echo '<script>alert("刪除失敗!");</script>'
  47.             } 
  48.         }else
  49.             echo '<script>alert("沒有稿件!");</script>'
  50.         }exit
  51.     } 
  52.  
  53.     if(isset($_session['imgpath']) && $_session['imgpath']){ 
  54.         echo '<script>alert("稿件已經存在,要想重新上傳請刪除原來的稿件!");</script>';exit
  55.     } 
  56.     if(!in_array($data['file']['type'], $data['type_array'])){ 
  57.         echo '<script>alert("稿件類型不匹配,請上傳.jpg、.gif和.png格式的圖片!");</script>';exit
  58.     } 
  59.     if($data['file']['size'] > $data['max_file_size']){ 
  60.         echo '<script>alert("您上傳的稿件過大,請選擇2m以下的圖片上傳!");</script>';exit
  61.     } 
  62.  
  63.     if(!is_dir($data['img_dir'])){ 
  64.         @mkdir($data['img_dir'], 0777, true); 
  65.     } 
  66.     $imgpath    = $data['img_dir'].'/'.date('his', time()).rand(100, 999).$data['file']['name']; 
  67.     $isupload    = move_uploaded_file($data['file']['tmp_name'], $imgpath); 
  68.     if(!$isupload){ 
  69.         echo '<script>alert("稿件上傳失敗,請嘗試重新上傳!");</script>';exit
  70.     }else
  71.         echo '<script>alert("稿件上傳成功!");</script>'
  72.     } 
  73.     $_session['name']        = $data['file']['name']; 
  74.     $_session['imgpath']    = $imgpath
  75.  
  76.     return $imgpath
  77.  
  78. /*test_start*/ 
  79. $sub_type = ''
  80. if(isset($_post['submit_upload']))$sub_type = 'upload'
  81. if(isset($_post['submit_delete']))$sub_type = 'delete'
  82. if($sub_type){//echo '<script>alert("'.$sub_type.'");</script>';exit; 
  83.     $data = array(    'sub_type'        => $sub_type
  84.                     'file'            => $_files['file'], 
  85.                     'img_tag_id'    => 'picview'
  86.                     'img_dir'        => 'upload_img'
  87.                   ); 
  88.     $imgpath = upload($data); 
  89. }else
  90.     $imgpath = isset($_session['imgpath'])? $_session['imgpath']: ''
  91. ?> 
  92.  
  93. <form action="ad.php" method="post" enctype="multipart/form-data" target="frame"
  94.     <input type="file" name="file" class="input"
  95.     <input type="submit" name="submit_upload" value="上  傳"
  96.     <input type="submit" name="submit_delete" value="刪  除"
  97.     <iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe> 
  98. </form> 
  99.  
  100. <img id="picview" height="100" style="display:none;"
  101. //開源代碼Vevb.com 
  102. <script> 
  103. if("<?php echo $imgpath; ?>"){ 
  104.     parent.document.getelementbyid("picview").src = "<?php echo $imgpath; ?>"
  105.     parent.document.getelementbyid("picview").style.display = "block"
  106. </script> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙雅县| 江都市| 临西县| 华安县| 宿迁市| 乌海市| 石林| 偃师市| 东乌珠穆沁旗| 武安市| 海晏县| 安新县| 桃园市| 通山县| 晋州市| 土默特左旗| 商水县| 香河县| 常州市| 平顺县| 和静县| 雷山县| 祁连县| 惠安县| 石台县| 栖霞市| 太白县| 湘潭县| 昭平县| 密云县| 木兰县| 五河县| 海南省| 永清县| 高州市| 罗山县| 华亭县| 黄浦区| 延庆县| 宁武县| 南京市|