当前位置:   article > 正文

【PHP】------- PHP 上传文件及图片代码_php图片上传代码

php图片上传代码

1. 图片上传的php 代码

  1. <?php
  2. session_start();
  3. $number = isset($_POST['typename'])? $_POST['typename'] : '';
  4. $_SESSION['id'] = $number;
  5. $response = array();
  6. $_SESSION['count']=count($_FILES['image']['name']);
  7. for($i=0;$i<count($_FILES['image']['name']);$i++) {
  8. $type=$_FILES['image']['type'];
  9. $__filename = time() .rand(). substr($_FILES['image']['name'][$i], strrpos($_FILES['image']['name'][$i], '.'));
  10. $filename = './picture/' . $__filename;
  11. if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $filename)) {
  12. $file_name = image($__filename,$type);
  13. $session=$_SESSION['id'];
  14. insert1($session,$file_name,$__filename);
  15. }
  16. else
  17. {
  18. echo '上传失败';
  19. }
  20. }
  21. function insert1($session,$file_name,$__filename)
  22. {
  23. include 'conn.php';
  24. $p="INSERT INTO `photo`(`Tid`, `Tphotoslt`, `Tphotoname`) VALUES ('".$session."','".$file_name."','".$__filename."')";
  25. mysqli_query($con,$p);
  26. echo '上传成功';
  27. }
  28. function image($images,$type)
  29. {
  30. for($i=0;$i<count($type);$i++)
  31. {
  32. if($type[$i]=='image/jpeg')
  33. {
  34. $image=imagecreatefromjpeg("./picture/".$images);//新建1.png 图片
  35. }
  36. else{
  37. if($type[$i]=='image/gif')
  38. {
  39. $image=imagecreatefromgif("./picture/".$images);//新建1.png 图片
  40. }
  41. else{
  42. if($type[$i]=='image/png')
  43. {
  44. $image=imagecreatefrompng("./picture/".$images);//新建1.png 图片
  45. }
  46. }
  47. }
  48. }
  49. $width=imagesx($image);//获取原图的width
  50. $height=imagesy($image);//获取原图的height
  51. if($width>2500 && $height>1500)
  52. {
  53. $thumb_width=$width*0.1;//缩略图的$thumb_width 的 width
  54. $thumb_height=$height *0.1;///缩略图的$thumb_height 的 height
  55. }
  56. else
  57. {
  58. if($width>1500 && $height>1000)
  59. {
  60. $thumb_width=$width*0.2;//缩略图的$thumb_width 的 width
  61. $thumb_height=$height *0.2;///缩略图的$thumb_height 的 height
  62. }
  63. else
  64. {
  65. if($width>800 && $height>800)
  66. {
  67. $thumb_width=$width*0.3;//缩略图的$thumb_width 的 width
  68. $thumb_height=$height *0.3;///缩略图的$thumb_height 的 height
  69. }
  70. else{
  71. $thumb_width=$width*0.4;//缩略图的$thumb_width 的 width
  72. $thumb_height=$height *0.4;///缩略图的$thumb_height 的 height
  73. }
  74. }
  75. }
  76. $thumb=imagecreatetruecolor($thumb_width,$thumb_height);//创建一个原图一半大小的画布
  77. imagecopyresampled($thumb,$image,0,0,0,0,$thumb_width,$thumb_height,$width,$height);//将原图按指定大小复制到画布上,得到缩略图
  78. imagejpeg($thumb,"./picture/img_".$images);//将缩略图保存在文件里
  79. imagedestroy($thumb);//清除占用的内存
  80. return 'img_'.$images;
  81. }
  82. ?>

2. html 上次界面代码: 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <link href="Backstage.css" rel="stylesheet" type="text/css">
  7. </head>
  8. <body>
  9. <form name="form1" id="form1">
  10. <select name="typename" id="typename">
  11. <option>--请选选择--</option>
  12. </select>
  13. <br>
  14. <br>
  15. <span>+文件上传</span>
  16. <input type="file" value="" name="image[]" id="image" multiple="multiple">
  17. <br> <br>
  18. </form>
  19. <button>
  20. 上传图片
  21. </button>
  22. <div id="upload">
  23. </div>
  24. <script>
  25. function $(id) {
  26. return document.querySelector(id);
  27. }
  28. //查看图片显示
  29. $('#image').addEventListener('change',function () {
  30. if(typeof FileReader== "undefined")
  31. {
  32. alert("检测到你的浏览器不支持FileReader对象!");
  33. }
  34. var strHTML="";
  35. var file=this.files;
  36. for(var inti=0,len=file.length;inti<len;inti++)
  37. {
  38. var tmpFile=file[inti];
  39. var reader=new FileReader();
  40. reader.readAsDataURL(tmpFile);
  41. reader.onload=function (e) {
  42. strHTML=strHTML+"<span>";
  43. strHTML=strHTML+"<img src='"+e.target.result+"' width='200px' height='200px' alt=''/>";
  44. strHTML=strHTML+"</span>";
  45. $("#upload").innerHTML="<li>"+strHTML+"</li>";
  46. }
  47. }
  48. });
  49. //获取图片的分类
  50. var image =function() {
  51. var oReq = new XMLHttpRequest();
  52. oReq.onreadystatechange = function () {
  53. if (oReq.readyState == 4) {
  54. if (oReq.status == 200) {
  55. //console.log(oReq.responseText);
  56. var json=JSON.parse(oReq.responseText);
  57. typename(json);
  58. }
  59. }
  60. };
  61. oReq.open("POST", "typename.php");
  62. oReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  63. oReq.send(null);
  64. };
  65. image();
  66. //循环分类数据
  67. function typename(data) {
  68. var option;
  69. for(var i=0,len=data.length;i<len;i++)
  70. {
  71. option=document.createElement('option');
  72. option.setAttribute('value',data[i]['Tid']);
  73. option.appendChild(document.createTextNode(data[i]['Tname']));
  74. $('#typename').appendChild(option);
  75. }
  76. }
  77. //上传图片显示 formdata
  78. $('button').addEventListener('click',function () {
  79. var form = $("#form1");
  80. var formData = new FormData(form);
  81. if(formData.getAll('typename')=="--请选选择--")
  82. {
  83. alert('请选择类型上传');
  84. return;
  85. }
  86. var oReq = new XMLHttpRequest();
  87. oReq.onreadystatechange = function () {
  88. if (oReq.readyState == 4) {
  89. if (oReq.status == 200) {
  90. console.log(oReq.responseText);
  91. /// var json=JSON.parse(oReq.responseText);
  92. }
  93. }
  94. };
  95. oReq.open("POST", "backstage.php");
  96. oReq.send(formData);
  97. return false;
  98. });
  99. </script>
  100. </body>
  101. </html>

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/970946
推荐阅读
相关标签
  

闽ICP备14008679号