昨天笔试一塌糊涂,发俩自己写的类找砖拍...
时间:2009-11-19
来源:互联网
1.多文件上传类[code]<?php
/*
* @upload.class.php 多文件上传类
* @author sanalex
* @version v 0.0.1 2009-11-08 11:38:29
*
* 用到了图片处理类: SimpleImage.php, 地址:
* http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
* 在原代码基础上加入按设定值等比缩放功能, 去掉其他功能
*
* 这个类在上传过程中, 除了文件系统操作的权限不足外,
* 不会给出任何打断上传操作的提示, 不符合条件的文件直接过滤掉,
* 不支持中文文件名
*/
class uploader {
public $path = "./upload/";
public $allow_type = array(".jpg",".jpeg",".png",".gif",".zip",".rar",".7z");
public $thumb_type = array(".jpg",".jpeg",".png",".gif");
public $query = array();
function __construct($path="", $max_size="", $thumb=false, $width="", $height="") {
$this->path = substr($path, -1) == "/" ? $path : $path."/";
$this->max_size = $max_size == "" ? "2048576" : $max_size;
$this->thumb = $thumb;
if ($this->thumb != false) {
$this->width = $width == "" ? "160" : $width;
$this->height = $height == "" ? "120" : $height;
}
}
function uploader($path="", $max_size="", $thumb=false, $width="", $height="") {
$this->__construct($path="", $max_size="", $thumb=false, $width="", $height="");
}
function upload($file, $name) {
if(!is_writable($this->path)) {
$this->halt("指定的路径不可写!");
}
$files = $file[$name];
$this->path = $this->path.date("y-m")."/";
for ($i=0; $i<count($files['name']); $i++) {
$this->i = $i;
if (($files['size'][$i] > 0) && ($files['size'][$i] <= $this->max_size)) {
$this->getExt($files['name'][$i]);
if (in_array($this->ext, $this->allow_type)) {
$this->getName($files['name'][$i]);
$this->tmp_name = $files['tmp_name'][$i];
$this->makeDir($this->path);
$this->destination = $this->path.$this->name.$this->ext;
$this->move($this->tmp_name, $this->destination);
$this->query[$i]['name'] = $this->name;
$this->query[$i]['destination'] = $this->destination;
if (($this->thumb == true) && (in_array($this->ext, $this->thumb_type))) {
$this->thumb_path = $this->path."thumb/";
$this->thumb_destination = $this->thumb_path.$this->name.$this->ext;
$this->makeDir($this->thumb_path);
$this->loadImage($this->destination);
if (($this->getWidth()<$this->width) && ($this->getHeight()<$this->height)) {
$this->copy($this->destination, $this->thumb_destination);
} else {
$this->resizeTo($this->width,$this->height);
$this->save($this->thumb_destination);
$this->query[$i]['thumb'] = $this->thumb_destination;
}
}
}
}
}
// return $this->query;
}
function loadImage($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function resizeTo($width,$height) {
$ratioheight = $height / $this->getHeight();
$ratiowidth = $width / $this->getWidth();
if ($ratiowidth < $ratioheight) {
$height = $this->getheight() * $ratiowidth;
} elseif ($ratiowidth >= $ratioheight) {
$width = $this->getWidth() * $ratioheight;
}
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function getName ($file_name) {
$this->name = basename($file_name, $this->ext)."_".time()."_".$this->i;
}
function getExt ($file_name) {
$arr = pathinfo($file_name);
$this->ext = strtolower(".".$arr['extension']);
}
function move ($file, $destination) {
if (!@move_uploaded_file($file, $destination)) {
$this->halt("无法移动文件到指定位置,请检查!");
}
}
function copy ($file, $destination) {
if (!@copy($file, $destination)) {
$this->halt("无法复制文件到指定位置,请检查!");
}
}
function makeDir ($path) {
if ( (!file_exists($path)) && (!@mkdir($path)) ) {
$this->halt("无法创建文件夹!");
}
}
function halt ($msg) {
echo "<font color=\"red\">注意:".$msg."</font>";
exit;
}
}
$upload = new uploader("uploads", "1024288", true, "200", "200");//生成缩略图,最大200*200
//$upload = new uploader("uploads", "1024288");//不生成缩略图
//$upload = new uploader("uploads", "1024288", true, "", "");//按默认值等比缩放
$upload->upload($_FILES, "file");//第二个参数是文件域名称,本例为"file"
if (!empty($upload->query)) {
echo "刚刚上传文件的信息为:<br />";
print_r($upload->query);
}
?>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file[]" value="" /><br />
<input type="file" name="file[]" value="" /><br />
<input type="file" name="file[]" value="" /><br />
<input type="submit" value="Upload" />
</form>[/code]2.数据库基础操作类,这个很简单了(不复杂,功能简单)[code]<?php
#==============================================================
# 功能:数据库基础操作类
# $db_host 数据库地址
# $db_user 用户名
# $db_pass 密码
# $db_name 数据库名
# $code 按此编码查询,输出数据同此编码
#==============================================================
class db {
public $conn;
public $result = 0;
public $query_count = 0;
private $results = false;
public function __construct($db_host, $db_user, $db_pass, $db_name, $code) {
try {
$this->conn = mysql_connect($db_host, $db_user, $db_pass);
} catch (Exception $e) {
echo $e;
} try {
mysql_select_db($db_name, $this->conn);
mysql_query("SET NAMES '$code'");
} catch (Exception $e) {
echo $e;
}
}
public function db () {
$this->__construct($db_host, $db_user, $db_pass, $db_name, $names);
}
public function insert ($query) {
$this->query($query);
$this->result = @mysql_insert_id($this->conn);
$this->query_count();
}
public function delete ($query) {
$this->query($query);
$this->result = @mysql_affected_rows();
$this->query_count();
}
public function update($query) {
$this->query($query);
$this->result = @mysql_affected_rows();
$this->query_count();
}
public function select($query) {
$this->query($query);
if(empty($this->results)) {
return false;
}
$this->result = array();
while ($row = @mysql_fetch_array($this->results, MYSQL_ASSOC)) {
array_push($this->result, $row);
}
$this->query_count();
}
private function query($query) {
if(empty($query)) {
return false;
}
if(empty($this->conn)) {
self::__construct();
} try {
$this->results = mysql_query($query,$this->conn);
} catch (Exception $e) {
echo $e;
}
if (!$this->results) {
return false;
}
}
private function query_count(){
$this->query_count = $this->query_count + 1;
}
}
?>[/code]
作者: sanalex 发布时间: 2009-11-19
不知道发错板块没有,第一次发帖...
问一句大伙,笔试时候用纸笔写代码感觉怎么样?
我感觉是一塌糊涂...
这两个类就是自己写着用的,我也是新人,欢迎老手指教,当然也可以无视,或拍砖...
作者: sanalex 发布时间: 2009-11-19
作者: lxylxy888666 发布时间: 2009-11-19
作者: sanalex 发布时间: 2009-11-19
作者: darkst 发布时间: 2009-11-19
新人找工作不容易啊...
作者: sanalex 发布时间: 2009-11-19
作者: hedgelog 发布时间: 2009-11-19
作者: 51phper 发布时间: 2009-11-19
用笔写实在是不习惯,更何况有时候还是要随用随查的...
作者: sanalex 发布时间: 2009-11-19
呵呵,看需求吧。
作者: sanalex 发布时间: 2009-11-19
作者: 若宁 发布时间: 2009-11-20
作者: 某个人 发布时间: 2009-11-20
作者: bbayou 发布时间: 2009-11-20
作者: cunmin 发布时间: 2009-11-22
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28