level 5
用自己的当然最好 那样公司才会觉得这活你走了 别人没法干 用别人的也可以 不过 明显的地位就低了
2014年04月21日 07点04分
2
level 7
我也在看文件上传类,书上的代码有点绕,我自己写了个,但现在出了错,弄好给你啊
2014年04月21日 08点04分
3
level 11
你可以写个简单的,你没有那么高的阅历,人家的类是要考虑到各种情况的
2014年04月21日 12点04分
4
嗯,那也是我考虑的情况没那么多
2014年04月21日 12点04分
level 11
<?php
//图片上传函数
function upload($dir){
$tmpname=$_FILES['pic']['tmp_name'];
$srcname=$_FILES['pic']['name'];
$picsize=$_FILES['pic']['size'];
$picext=array_pop(explode('.',$srcname));
//中文名称上传会有问题
$picname=time().mt_rand().'.'.$picext;
//文件后缀
$allowext=array('jpg','png','gif');
//被允许的文件后缀
if($_FILES['pic']['error']===0){
if(in_array($picext,$allowext)){
if(move_uploaded_file($tmpname,$dir.'/'.$picname)){
return $picname;
}else{
return false;
}
}else{
return false;
}
}else{
return false;
}
}
//图片缩放函数
function thumb($pic,$smx,$smy,$taf){
//判断图片的后缀
$arr=getimagesize($pic);
$path=pathinfo($pic);
switch ($arr[2]) {
case 1:
$imgext="gif";
$imgcreate="imagecreatefromgif";
$imgout="imagegif";
break;
case 2:
$imgext="jpg";
$imgcreate="imagecreatefromjpeg";
$imgout="imagejpeg";
break;
case 3:
$imgext="png";
$imgcreate="imagecreatefrompng";
$imgout="imagepng";
break;
}
$lg=$imgcreate($pic);
$lgx=$arr[0];
$lgy=$arr[1];
//缩放比例
if($taf){
if($lgx/$smx>$lgy/$smy){
$b=$lgx/$smx;
}else{
$b=$lgy/$smy;
}
$smx=floor($lgx/$b);
$smy=floor($lgy/$b);
}else{
}
$sm=imagecreatetruecolor($smx,$smy);
imagecopyresampled($sm,$lg,0,0,0,0,$smx,$smy,$lgx,$lgy);
$imgout($sm,$path['dirname'].'/th_'.$path['basename']);
}
?>
这是我自己写的一个图片的上传和缩略函数。 共享给你。
2014年04月21日 13点04分
5
level 7
/*测试页面*/
<?php
if($_POST['sub']){
include('fileupload.class.php');//包含类文件
$up=new FileUpload();
/*设置上传信息*/
$up->path='./uploads';
$up->maxsize=1000000;
$up->allowtype=array('jpg','gif','png');
$up->israndname=false;
echo $up->upload();
}
?>
<form method='post' action='' enctype="multipart/form-data"><!---////////////////////->
<input type="file" name='myfile'><br>
<input type="submit" name="sub">
</form>
2014年04月24日 14点04分
6
level 7
/*'fileupload.class.php'*/
<?php
class FileUpload{
/*为上传信息赋默认值*/
private $path='./uploads';
private $maxsize=1000;
private $allowType=array('jpg','gif','png'); //允许的上传文件类型
private $israndname=false; //是否重新命名
private $errorNum=0; //错误号
private $orginName; //原文件名
private $type;
private $tmpName;
private $size;
public $newName; //新文件名
function __set($key,$val){
$this->$key=$val;
}
function __construct(){ //构造函数为属性赋值
$this->orginName=$_FILES['myfile']['name'];
$this->tmpName=$_FILES['myfile']['tmp_name'];
$this->size=$_FILES['myfile']['size'];
$this->error=$_FILES['myfile']['error'];
$this->newName=$this->getName();
$this->type=pathinfo($_FILES['myfile']['name'],4);
}
function upload(){ //上传函数
if(!$this->checkPath()||!$this->checkError()||!$this->checkSize()||!$this->checkType()||!$this->uploadFile()){
if($this->errorNum)// 不为0则存在错误
return $this->errorMes(); //用这个函数返回错误信息
}else{
return $this->orginName.'上传成功';
}
}
function errorMes(){
$str="上传文件<font color='red'>".$this->orginName."</font>时出错:";
if($this->errorNum){
switch($this->errorNum){
case 4: $str.='没有文件上传';break;//1-4为系统提交错误,-1 -4为自定义
case 3: $str.="文件只有部分被上传";break;
case 2: $str.="上传文件超过表单限制";break;
case 1: $str.="上传文件超过服务器限制";break;
case -1: $str.="未允许的文件类型";break;
case -2: $str.="上传文件不能超过{$this->maxsize}";break;
case -3: $str.="上传失败";break;
case -4: $str.="建立目录失败";break;
case -5: $str.="必须指定上传路径";break;
default: $str.="未知错误";
}
}
return $str;
}
private function uploadFile(){
if(move_uploaded_file($this->tmpName,$this->path.'/'.$this->newName)){
return true;
}else{
$this->errorNum=-3;
return false;
}
}
private function checkPath(){//检查上传路径
if(empty($this->path)){
$this->errorNum=-5;
return false;
}
if(!file_exists($this->path)||!is_writable($this->path)){
if(!mkdir($this->path,0777)){
$this->errorNum=-4;
return false;
}
}
return true;
}
private function checkError(){//检查系统提供的错误信息
if($_FILES['myfile']['error']){
$this->errorNum=$_FILES['myfile']['error'];
return false;
}
return true;
}
private function checkSize(){//检查文件大小
if($this->size > $this->maxsize){
echo 'aa';
$this->errorNum=-2;
return false;
}
return true;
}
private function checkType(){//检查文件类型
if(!in_array($this->type,$this->allowType)){
echo 'aa';
$this->errorNum=-1;
return false;
}
return true;
}
private function getName(){//设置文件名字
if($this->israndname){
return $this->newName=date('YmdHis').rand(100,999).'.'.$this->type;
}else{
return $this->newName=$_FILES['myfile']['name'];
}
}
}
?>
2014年04月24日 14点04分
7
level 7
本来是比较简单的,但老是犯些错误,弄到现在,真怀疑自己适不适合编程,上面的类是单个文件上传的,本来想要支持多个文件上传的,但现在的代码有问题,首先是还不能将上传文件表单名做为变量,只能是字符串'myfile',还有$this->size > $this->maxsizep这个代码存在问题,不能检查文件大小,还有上传函数uploadFile()并没有在主函数upload()中运行,竟然上传成功,我也不知道当初自己怎么写的
2014年04月24日 14点04分
8
![[真棒]](/static/emoticons/u771fu68d2.png)
写到这程度不错了,自信,加油
2014年04月24日 15点04分