php如何实现图片上传的封装

php实现图片上传封装

1 先封装图片上传类 file.class.php

  1. <?php
  2. class File{
  3. public $images;
  4. public $filename;
  5. public $allow_size;
  6. public function __construct($images,$filename,$allow_size){
  7. $this->images=$images;
  8. $this->filename=$filename;
  9. $this->allow_size=$allow_size;
  10. }
  11. //文件大小
  12. public function allow_size(){
  13. if($_FILES[$this->filename]['size']>$this->allow_size){
  14. echo "上传文件过大";
  15. return false;
  16. }
  17. return true;
  18. }
  19. //新的名字
  20. public function type(){
  21. $type=substr($_FILES[$this->filename]['name'],strrpos($_FILES[$this->filename]['name'],'.')+1);
  22. $name=time().rand(1000,9999).".".$type;
  23. return $name;
  24. }
  25. //放到一个新的文件
  26. public function move(){
  27. $name=$this->type();
  28. if(is_uploaded_file($_FILES[$this->filename]['tmp_name'])){
  29. @move_uploaded_file($_FILES[$this->filename]['tmp_name'],$this->images.$name);
  30. echo "<script>alert('添加图片成功')</script>";
  31. return $name;
  32. }else{
  33. echo "<script>alert('添加图片失败')</script>";
  34. }
  35. }
  36. //判断文件上传是否成功
  37. public function error(){
  38. if($_FILES[$this->filename]['error']==0){
  39. return true;
  40. }else if($_FILES[$this->filename]['error']==1){
  41. echo "文件的大小超过了php.ini中配置文件的大小";
  42. return false;
  43. }else if($_FILES[$this->filename]['error']==2){
  44. echo "文件中的配置大小有问题";
  45. return false;
  46. }else if($_FILES[$this->filename]['error']==3){
  47. echo "找不到文件的位置";
  48. return false;
  49. }
  50. }
  51. }

2 封装数据库类 mysql.class.php

  1. <?php
  2. class Mysql{
  3. public $db_link;
  4. public $db_address;
  5. public $db_user;
  6. public $db_pwd;
  7. public $db_name;
  8. //public function __construct(IP地址, 用户名, 密码, 数据库)
  9. public function __construct($address,$user,$pwd,$name){
  10. $this->db_address=$address;
  11. $this->db_user=$user;
  12. $this->db_pwd=$pwd;
  13. $this->db_name=$name;
  14. $this->connect();
  15. }
  16. //连接数据库
  17. public function connect(){
  18. $this->db_link=mysql_connect($this->db_address,$this->db_user,$this->db_pwd);
  19. mysql_select_db($this->db_name);
  20. mysql_query("set names utf8");
  21. }
  22. //进行 增删改
  23. public function dml($sql){
  24. $res=mysql_query($sql);
  25. if(!$res){
  26. echo"sql语句错误";
  27. }else{
  28. return $res;
  29. }
  30. }
  31. //进行多条数据的查询
  32. public function select_all($sql){
  33. $res=mysql_query($sql);
  34. if(is_resource($res) && mysql_affected_rows()>0){
  35. $arr=array();
  36. while($w=mysql_fetch_assoc($res)){
  37. $arr[]=$w;
  38. }
  39. return $arr;
  40. }else{
  41. return false;
  42. }
  43. }
  44. //进行单行数据进行查询
  45. public function select_one($sql){
  46. $res=mysql_query($sql);
  47. if(is_resource($res) && mysql_affected_rows()>0){
  48. return mysql_fetch_assoc($res);
  49. }else{
  50. return false;
  51. }
  52. }
  53. //将数据中的某一条数据进行删除 @parme : 表名 条件
  54. public function delete($table,$where){
  55. //$str=mysql_query($sql);
  56. $str="delete from $table where $where";
  57. return mysql_query($str);
  58. }
  59. //update table set name='fasf ' where id=4;
  60. public function update1($table,$arr,$where){
  61. $str="";
  62. foreach($arr as $k=>$v){
  63. if(is_string($v)){
  64. $str=$str.$k.'="$v",';
  65. }else{
  66. $str=$str.$k."=".$v.",";
  67. }
  68. }
  69. $value=rtrim($str,',');
  70. $sql="update $table set $value where $where";
  71. return mysql_query($sql);
  72. }
  73. //添加一条数据insert into table(name,age) value('$name','$age');
  74. public function insert($table,$arr){
  75. $str1='';
  76. $str2='';
  77. foreach($arr as $k=>$v){
  78. $str1=$str1.$k.",";
  79. if(is_string($v)){
  80. $str2=$str2.'"'.$v.'",';
  81. }else{
  82. $str2=$str2.$v.',';
  83. }
  84. }
  85. $key=substr($str1,0,strlen($str1)-1);
  86. $value=substr($str2,0,strlen($str2)-1);
  87. $sql="insert into $table($key) value($value)";
  88. mysql_query($sql);
  89. return mysql_insert_id();
  90. }
  91. public function counts($sql){
  92. mysql_query($sql);
  93. return mysql_affected_rows();
  94. }
  95. }
  96. ?>

3 创建文件上传表单upload.php

  1. <form action="uploading_insert.php" method="post" enctype="multipart/form-data">
  2. <meta charset="utf-8"/>
  3. <fieldset>
  4. <legend>用户上传信息</legend>
  5. <p>
  6. <label>姓名:</label>
  7. <input type="text" name="username">
  8. </p>
  9. <p>
  10. <label>密码:</label>
  11. <input type="text" name="pwd">
  12. </p>
  13. <p>
  14. <label>身份证号:</label>
  15. <input type="text" name="card1">
  16. </p>
  17. <p>
  18. <label>上传图像:</label>
  19. <input type="file" name="filename"><input type="hidden" name="MAX_FILES_SIZE" value="1000000">
  20. </p>
  21. <p>
  22. <input type="submit" value="上传">
  23. </p>
  24. </fieldset>
  25. </form>