php 上传类(自家用)

<?php
/**
 * File up load class
 * @version  1.0.0 (Thu Aug 18 01:32:39 CST 2005)
 * @author  sanshi
 */
class upLoad
{
 /**
 *
 * @author   sanshi
 * @version  1.0.0 Thu Aug 18 01:00:18 CST 2005
 * @param  string $info   文件内容
 * @param  string $fileName    生成的文件名
 * @return   boolean    建立成功返回true
 * @deprecated
 * 建立html文件
 */
 function createHtml( $info ,$fileName )
 {
 }
 /**
 *
 * @author  sanshi
 * @version 1.0.0 Thu Aug 18 01:03:09 CST 2005
 * @return  void
 * @deprecated
 * 构造函数
 */
 function downLoad()
 {}
 /**
 *
 * @author  sanshi
 * @version 1.0.0 Thu Aug 18 01:03:55 CST 2005
 * @param   string $fileField 在表单中的字段名
 * @param   string $length      限制的长度
 * @return    boolean      成功返回true
 * @deprecated
 * 功能实现函数
 */
 function init($fileField,$length='')
 {
  $files    = $_FILES[$fileField];
  //用户名需要改动,根据自己的实际情况做改动
  $userName = 'sanshi';
  $fileName = $files['name'];
  $fileType = $files['type'];
  $fileTemp = $files['tmp_name'];
  $fileSize = empty( $length ) ? ($files['size']+10) : $length;
  $fileError= $files['error'];//这块也许php4中没有
  //改为
  //if( $this->_isType( $fileName ) || $this->_isBig( $length ) )
  if( !$this->_isType( $fileName ) || $this->_isBig( $length ) || $fileError != 0  )
  {
   //print_r ($files);
   return false;
  }else{
   $path = $this->_createDir( $userName );//取得路径
   $createFileName = $userName . "_" . time();//设置当前文件名
   $createFileType = $this->getFileType($fileName);//设置文件类别
   return @move_uploaded_file($fileTemp,$path.$createFileName.'.'.$createFileType) ? true : false;
  }
 }
 
 /**
 *
 * @author  sanshi
 * @version 1.0.0 Thu Aug 18 01:07:43 CST 2005
 * @param   int  $length  上传限制的大小
 * @return    boolean    超过返回true
 * @deprecated
 * 判断是否超过预定大小
 */
 function _isBig($length)
 {
  $bigest = '';
  return $big > $bigest ? true : false;
 }
 /**
 *
 * @author  sanshi
 * @version 1.0.0 Thu Aug 18 01:08:55 CST 2005
 * @param   string  $fileName 文件名  
 * @return    string  $fileType 文件后缀
 * @deprecated
 * 取得文件后缀(只取得文件的最后一个后缀名)
 */
 function getFileType($fileName)
 {
  return end(explode('.',$fileName));
 }
 /**
 *
 * @author  sanshi
 * @version 1.0.0 Thu Aug 18 01:10:41 CST 2005
 * @param  string $fileName 文件名
 * @param  boolean $method  是否检查多个后缀默认false
 * @param int  $postFix 后缀个数默认为2
 * @return   boolean    存在返回true
 * @deprecated
 * 检查文件的后缀是否在类别数组中,类别数组自己设置
 *   如果$method设置为true则检查文件有几个后缀
 */
 function _isType($fileName,$method='false',$postFix=2)
 {
  //设置类别数组
  $type = array('jpeg',
       'gif',
       'bmp',
       'exe');
  $fileName = strtolower( $fileName );
  $fileTypeArray = explode( '.',$fileName );
  $fileType = end( $fileTypeArray );
  //判断是否有一个文件有多个后缀
  if($method)
  {
   if( count( $fileTypeArray ) > (is_int($postFix) ? $postFix : 2) )
   {
    return false;
   }
  }
  return in_array($fileType,$type);
 }
 
 /**
 *
 * @author  sanshi
 * @version 1.0.0 Thu Aug 18 01:17:19 CST 2005
 * @param   string $userName
 * @return    string $path
 * @deprecated
 * 建立目录 目录格式 年/月/日/用户名/
 *   权限为755
 */
 function _createDir($userName)
 {
  $root = '';
  $pathSign = DIRECTORY_SEPARATOR;
     $y = date('Y').$pathSign;
  $m = date('m').$pathSign;
  $d = date('d').$pathSign;
  $path = $root . $y . $m . $d . $userName;
  $dirArray = explode( $pathSign,$path);
  $tempDir='';
  foreach ( $dirArray as $dir)
  {
   $tempDir.= $dir.$pathSign;
   $isFile = file_exists( $tempDir );
   clearstatcache();
   if( ! $isFile && !is_dir( $tempDir ) )
   {
    @mkdir($tempDir,0755);
   }
  }
  return $path . $pathSign;
 }
 /**
 *
 * @author  sanshi
 * @version 1.0.0 Thu Aug 18 01:19:32 CST 2005
 * @param   string  $dirName  目录名
 * @return    boolean 可以操作返回true
 * @deprecated
 * 判断操作是否在上传目录
 */
 function _isDel($dirName)
 {
  //注意upLoadDir,一定要与真正使用目录相对应
  $upLoadDir = '';
  $upLoadDir = preg_replace('/\\//','\/',$upLoadDir);
  $format      = "/^{$upLoadDir}/";
  return preg_match( $format,$dirName );
 }
 /**
 *
 * @author  sanshi
 * @version 1.0.0 Thu Aug 18 01:25:58 CST 2005
 * @param  string  $fileName 文件名
 * @return   boolean  删除文件成功返回true
 * @deprecated
 * 删除文件 
 */
 function delFile( $fileName )
 {
  $cur_dir = dirname(trim($fileName));
  if( $this->_isDel( $cur_dir ) )
  {
   return @unlink( $fileName ) ? true : false;
  }else{
   return false;
  }
 }
 /**
 *
 * @author  sanshi
 * @version 1.0.0 Thu Aug 18 01:27:43 CST 2005
 * @param  string  $dieName 目录名
 * @return   boolean  删除成功返回true
 * @deprecated
 * 删除目录 目录下如果有文件不能删除
 */
 function delDir( $dirName )
 {
  if( $this->_isDel($dirName) && is_dir( $dirName ) )
  {
   return @rmdir( $dirName ) ? true : false;
  }else{
   return false;
  }
 }
 
}
?>
<?php
//使用
/*
include 'upLoad.class.php';
$up = new upLoad();
if($up->init("file"))
{
 echo 'success';
}else{
 echo 'failure';
}
*/
?>

时间: 2024-08-19 20:49:13

php 上传类(自家用)的相关文章

PHP中codeigniter文件上传类代码实例

  codeigniter文件上传类代码实例 文件上传类 CodeIgniter 的文件上传类允许文件被上传.您可以设置指定上传某类型的文件及指定大小的文件. 处理过程 上传文件普遍的过程: 一个上传文件用的表单,允许用户选择一个文件并上传它. 当这个表单被提交,该文件被上传到指定的目录. 同时,该文件将被验证是否符合您设定的要求. 一旦文件上传成功,还要返回一个上传成功的确认窗口. 这里有一个简短的教程来显示这个过程.此后你将会找到相关的参考信息. 创建上传表单 运用文本编辑器创建一个名为up

ASP实例:一个简单的ASP无组件上传类

简单的ASP无组件上传类,发出来让大家看看.可以做做实验! 以下为引用的内容: <%@ language="javascript"%><%var self = Request.serverVariables("SCRIPT_NAME");if (Request.serverVariables("REQUEST_METHOD")=="POST"){        var oo = new uploadFile(

ASP无组件上传类的应用实例

上传|无组件|应用实例|上传|无组件|应用实例 ''''''-------- upload.htm ------------- <script language="javascript">   function checkFile(myForm)    { if(myForm.File1.value=='') return false;    myForm.submit();     }</script><form method="POST&qu

经典的PHP文件上传类

在课堂上给大家写了一个经典的文件上传类,灵活性还可以,大家可以参考使用,   上传文件:   <?php /** * author : PHP100.com * date :2012-9-15 经典的文件上传类  **/  if(!empty($_POST['sub'])){   include("up.class.php");      $up = new up($_FILES['up']); //参数,文件流必选,[指定目录,指定大小,指定文件名]可选  }  ?>  

PHP非常实用的上传类,上传效果在线演示

  #********************************************************* #文件名称: inc_class.upload.php #Copyright (c) 2007-2009 青春一度 all rights reserved. #最后更新: 2009-08-05 #版本 : v 2.0.a #注:转发时请保留此声明信息,这段声明不并会影响你的速度! #如有修改请将修改后的文件以邮件形式发送给作者一份,谢谢! # #***************

化境ASP无组件上传类 - upload

上传|无组件 化境ASP无组件上传类 - upload_5xsoft 使用手册 1.0 稻香老农 http://www.5xsoft.com/ [ 下载 ] 目 录 1.关于 upload_5xsoft 2.运行平台与注意事项 2.类的成员与对象 3.使用示例 关于 upload_5xsoft 一直以来,由于FileSystemObject的局限,所以ASP最大的难题就是文件上传,大多解决法就是安装 第三方上传组件.可第三方组件有很多问题,有的组件要注册,有的组件要在表单中加上他的版权信息. 还

ASP动态网页实例:表单多文件上传类

动态|上传|网页 <%    Class Upload        Public  Form, Finished        Private bVBCrlf, bSeparate, formData, cFields, folderPath, itemCount, sErrors, sAuthor, sVersion        Private itemStart(), itemLength(), dataStart(), dataLength(), itemName(), itemDat

php 图片上传类代码

php 图片上传类代码,功能也比较全,大家根据需要选择. 先来个简单的: <? //http://www.jzxue.com class upLoad{ public $length; //限定文件大小 public $file; //判断此类是用于图片上传还是文件上传 public $fileName; //文件名 public $fileTemp; //上传临时文件 public $fileSize; //上传文件大小 public $error; //上传文件是否有错,php4没有 pub

自己写的一个PHP上传类

主要功能:文件上传,获取文件名,获取文件大小,随机生成新文件名,获取文件类型,图片生成缩略图,返回缩略图文件名,返回上传后生成的文件的文件名,返回上传后的文件路径   //----------------------------------------------------------------------//转发时请保留此声明信息,这段声明不并会影响你的速度!//*******************   IEB上传类 v1.1  *****************************

ASP.NET实现的简单易用文件上传类

  这篇文章主要介绍了ASP.NET实现的简单易用文件上传类,本文给出实现代码和使用方法示例,需要的朋友可以参考下 调用方法: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 UploadFile uf = new UploadFile();   /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认:true),true原始文件名,false系统生成新文件名