首页 > 语言 > PHP > 正文

php 图片上传代码例子

2024-09-04 11:44:59
字体:大 中 小
来源:转载
供稿:网友

下面来为你免费提供一款php 图片上传代码,如果你正在找文件上传的图片代码就进来看看吧,这是一款只支持jpg,gif,png,swf文件上传的php实例代码.

PHP实例代码如下:

  1. <?php  
  2. if($_FILES['file']){ 
  3.  
  4.  // 上传设置 
  5.  $maxsize=10002400;            //最大允许上传的文件大小 
  6.  $alltype=array(".jpg",".JPG",".GIF",".gif",".png",".swf");         //所有允许上传的文件类型 
  7.  $imgtype=array(".jpg",".JPG",".GIF",".gif",".png",".swf");               //图片类型 
  8.  
  9.  // 判断文件大小 
  10.  if($_FILES['file']['size']>$maxsize)  { 
  11.      echo "您上传的资料大于10000K"; 
  12.      exit; 
  13.  } 
  14.   
  15.  // 判断文件类型 
  16.  $type=strstr($_FILES['file']['name'],"."); 
  17.  if(!in_array($type,$alltype)){ 
  18.      echo "不允许上传该类型的文件"; 
  19.      exit; 
  20.  } 
  21.  $updir="uploaddir"; 
  22.  $time=date("Ymd-His",time()); 
  23.  $fn=$time.$type; 
  24.  $destination=$updir."/".$fn; 
  25.  if(@move_uploaded_file($_FILES['file']['tmp_name'], $destination)){ 
  26.          @chmod($destination, 0777); 
  27.    $fileurl=$updir."/".$destination; 
  28.          $fileurl="".$destination; 
  29.             
  30.  }else{ 
  31.     echo "上传失败!"; 
  32.     echo "<script>location.href=history.back()</script>"; 
  33.  } 
  34. // ----------------------------------------------------------------------------------------------// 
  35. }//开源代码Vevb.com 
  36. if($back=="no"): 
  37.  echo "ok"; 
  38.  exit; 
  39. endif; 
  40. ?> 

html代码如下:

  1. <html> 
  2. <head> 
  3. <title>添加图片</title> 
  4. <script language="JavaScript"> 
  5. function isnull() 
  6. { 
  7.          if(document.all.fileurl.value!="")insert(); 
  8. } 
  9. function insert() 
  10. { 
  11.         images='<img style="cursor:hand" src="'+insertpic.fileurl.value+'">'; 
  12.         edit = window.opener.document.mode_iframe.document.selection.createRange(); 
  13.         edit.pasteHTML (images); 
  14.         window.close(); 
  15. } 
  16. </script> 
  17. <style> 
  18. BODY {        FONT-SIZE: 12px; FONT-FAMILY: tahoma,宋体;} 
  19. TABLE {        empty-cells: show;} 
  20. TD {        FONT-SIZE: 12px; COLOR: #000000; FONT-FAMILY: tahoma,宋体;margin: 5} 
  21. </style> 
  22. </head> 
  23. <body leftMargin=12 topMargin=12 marginwidth=2 marginheight=2 bgcolor='#D6D3CE' style="border:0px" onload=isnull()> 
  24. <form action=<?=$_SERVER['PHP_SELF']?> name=insertpic enctype=multipart/form-data method=post> 
  25. <table> 
  26. <tr> 
  27.         <td></td> 
  28.         <td width=60>上传图片</td> 
  29.         <td> 
  30.   <input class=mmcinb type="file" name="file" size="14"> 
  31.         <input class=mmcinb type="submit" value="上传"></td> 
  32. </tr> 
  33. <tr> 
  34.         <td></td> 
  35.         <td>图片URL</td> 
  36.         <td><input class=mmcinb type="text" name="fileurl" value="<?=$fileurl?>" size="26"> 
  37.         <button onclick=insert()>插入</button></td> 
  38. </tr> 
  39. <tr> 
  40.         <td></td> 
  41.          
  42.       <td colspan=2 align=left><font color=red>可以直接输入图片的URL,点击插入即可(不能有中文)<br> 
  43.         </font></td> 
  44. </tr> 
  45. </table> 
  46. </form> 
  47. <script language="JavaScript"> 
  48. <!-- 
  49.         //picurl.focus(); 
  50. //--> 
  51. </script> 
  52. </body> 
  53. </html> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表