+ -
当前位置:首页 → 问答吧 → 昨天笔试一塌糊涂,发俩自己写的类找砖拍...

昨天笔试一塌糊涂,发俩自己写的类找砖拍...

时间:2009-11-19

来源:互联网

本帖最后由 sanalex 于 2009-11-19 11:21 编辑

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 10:56 编辑

不知道发错板块没有,第一次发帖...

问一句大伙,笔试时候用纸笔写代码感觉怎么样?
我感觉是一塌糊涂...

这两个类就是自己写着用的,我也是新人,欢迎老手指教,当然也可以无视,或拍砖...

作者: sanalex   发布时间: 2009-11-19

笔试也害怕,有次,一道题没做出来

作者: lxylxy888666   发布时间: 2009-11-19

20道题我只写了13道,考官看着我和答案一阵冷笑,心都凉了....

作者: sanalex   发布时间: 2009-11-19

哎呀,还冷笑! 我们公司没那么夸张!

作者: darkst   发布时间: 2009-11-19

本帖最后由 sanalex 于 2009-11-19 11:20 编辑

新人找工作不容易啊...

作者: sanalex   发布时间: 2009-11-19

喜欢把文件上传跟缩略图功能分开写.

作者: hedgelog   发布时间: 2009-11-19

楼主找PHP的工作还要笔试啊?  看样子得多加锻炼哦!

作者: 51phper   发布时间: 2009-11-19

回复 51phper


    用笔写实在是不习惯,更何况有时候还是要随用随查的...

作者: sanalex   发布时间: 2009-11-19

回复 hedgelog


    呵呵,看需求吧。

作者: sanalex   发布时间: 2009-11-19

笔写感觉肯定不好

作者: 若宁   发布时间: 2009-11-20

LZ还需要磨练。。。。

作者: 某个人   发布时间: 2009-11-20

不错,借用了

作者: bbayou   发布时间: 2009-11-20

哥就是不笔试 但哥就是会  BS 那些考官

作者: cunmin   发布时间: 2009-11-22