加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_宿迁站长网 (https://www.0527zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

教你如何使用php封装类实现图片上传可直接引用

发布时间:2022-07-10 01:32:08 所属栏目:PHP教程 来源:互联网
导读:?php class image { /** *完成图片的上传 * *@param array $file 待上传的文件信息的数组,用于5个元素的那个数组 *@return mixed 如果执行成功,返回上传了的文件名,否则返回false */ public function upload($file) { if($file[error] == 0) { $allow_t
  <?php
 
  class image { /** *完成图片的上传 * *@param array $file 待上传的文件信息的数组,用于5个元素的那个数组 *@return mixed 如果执行成功,返回上传了的文件名,否则返回false */ public function upload($file) { if($file['error'] == 0) { $allow_types = array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'); if(in_array($file['type'], $allow_types)) { $maxsize = 2000000; if($file['size'] <= $maxsize) { //上传 //需要将文件重命名,1,防止不规则的字符出现在文件名中,2,防止重名 //采用时间戳加随机数的形式 //后缀名如何获得?在原始文件名中获得后缀名 //在文件名中最后一个点截取到最后就是扩展名 //strrchr(在哪个字符串中查,查的字符串); $new_filename = time() . mt_rand(10000, 99999) . strrchr($file['name'], '.');

      //移动 //此函数返回移动成功还是失败 if(move_uploaded_file($file['tmp_name'],'images/'. $new_filename)) { return $new_filename; } } } } //只有一种情况返回文件名,其他全部返回false return false; }}?>//-------------------------------------------------------------------------------------<?phpheader("content-type:text/html;charset=utf-8");function __autoload($image){ require_once($image.'.class.php');} $image = new image(); $user = $_POST['user']; $img = $_FILES['img']; //var_dump($img); $img = $image ->upload($img); mysql_connect('localhost','root','123'); mysql_select_db('lyb'); mysql_query('set names utf8'); $q = "insert test_image(name,url) values('$user','$img')"; //var_dump($q); $result = mysql_query($q); if($result){ echo "添加成功.....

      <br /><br />"; } else{ echo "添加失败。。。"; }?>//--------------------------------------------------------------------------------------<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>图片上传类</title></head><body><form enctype="multipart/form-data" method="post" action="images.php">姓名:<input type="text" name="user" id="user"/><br>图片:<input type="file" name="img" id="img"/><br><input type="submit" value="提交"/></form></body></html>
 

(编辑:云计算网_宿迁站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!