PHP上传图片类

半城伤御伤魂 提交于 2019-12-19 01:07:54

php上传图片类

<?php
class ieb_upload
{
    var $FormName; //文件域名称
    var $Directroy; //上传至目录
    var $MaxSize; //最大上传大小
    var $CanUpload; //是否可以上传
    var $doUpFile; //上传的文件名
    var $sm_File; //缩略图名称
    var $Error; //错误参数
     
    function ieb_upload($formName = '', $dirPath = '', $maxSize = 2097152) // (1024*2)*1024=2097152 就是 2M
    {
        global $FormName, $Directroy, $MaxSize, $CanUpload, $Error, $doUpFile, $sm_File;
        // 初始化各种参数
        $FormName = $formName;
        $MaxSize = $maxSize;
        $CanUpload = true;
        $doUpFile = '';
        $sm_File = '';
        $Error = 0;
 
        if ($formName == '')
        {
            $CanUpload = false;
            $Error = 1;
            break;
        }
 
        if ($dirPath == '')
            $Directroy = $dirPath;
        else
            $Directroy = $dirPath.'/';
    }
    // 检查文件是否存在
    function scanFile()
    {
        global $FormName, $Error, $CanUpload;
 
        if ($CanUpload)
        {
            $scan = is_readable($_FILES[$FormName]['name']);
 
            if ($scan)
                $Error = 2;
 
            return $scan;
        }
    }
    // 获取文件大小
    function getSize($format = 'B')
    {
        global $FormName, $Error, $CanUpload;
 
        if ($CanUpload)
        {
            if ($_FILES[$FormName]['size'] == 0)
            {
                $Error = 3;
                $CanUpload = false;
            }
 
            switch ($format)
            {
                case 'B':
                    return $_FILES[$FormName]['size'];
                    break;
 
                case 'K':
                    return ($_FILES[$FormName]['size']) / (1024);
                    break;
 
                case 'M':
                    return ($_FILES[$FormName]['size']) / (1024 * 1024);
                    break;
            }
        }
    }
    // 获取文件类型
    function getExt()
    {
        global $FormName, $Error, $CanUpload;
 
        if ($CanUpload)
        {
            $ext = $_FILES[$FormName]['name'];
            $extStr = explode('.', $ext);
            $count = count($extStr)-1;
        }
        return $extStr[$count];
    }
    // 获取文件名称
    function getName()
    {
        global $FormName, $CanUpload;
 
        if ($CanUpload)
            return $_FILES[$FormName]['name'];
    }
    // 新建文件名
    function newName()
    {
        global $CanUpload, $FormName;
 
        if ($CanUpload)
        {
            $FullName = $_FILES[$FormName]['name'];
            $extStr = explode('.', $FullName);
            $count = count($extStr)-1;
            $ext = $extStr[$count];
 
            return date('YmdHis').rand(0, 9).'.'.$ext;
        }
    }
    // 上传文件
    function upload($fileName = '')
    {
        global $FormName, $Directroy, $CanUpload, $Error, $doUpFile;
 
        if ($CanUpload)
        {
            if ($_FILES[$FormName]['size'] == 0)
            {
                $Error = 3;
                $CanUpload = false;
                return $Error;
                break;
            }
        }
 
        if ($CanUpload)
        {
            if ($fileName == '')
                $fileName = $_FILES[$FormName]['name'];
 
            $doUpload = @copy($_FILES[$FormName]['tmp_name'], $Directroy.$fileName);
 
            if ($doUpload)
            {
                $doUpFile = $fileName;
                chmod($Directroy.$fileName, 0777);
                return true;
            }
            else
            {
                $Error = 4;
                return $Error;
            }
        }
    }
    // 创建图片缩略图
    function thumb($dscChar = '', $width = 150, $height = 113)
    {
        global $CanUpload, $Error, $Directroy, $doUpFile, $sm_File;
 
        if ($CanUpload && $doUpFile != '')
        {
            $srcFile = $doUpFile;
 
            if ($dscChar == '')
                $dscChar = 'sm_';
 
            $dscFile = $Directroy.$dscChar.$srcFile;
            $data = getimagesize($Directroy.$srcFile, &$info);
 
            switch ($data[2])
            {
                case 1:
                    $im = @imagecreatefromgif($Directroy.$srcFile);
                    break;
 
                case 2:
                    $im = @imagecreatefromjpeg($Directroy.$srcFile);
                    break;
 
                case 3:
                    $im = @imagecreatefrompng($Directroy.$srcFile);
                    break;
            }
 
            $srcW = imagesx($im);
            $srcH = imagesy($im);
            $ni = imagecreatetruecolor($width, $height);
            imagecopyresized($ni, $im, 0, 0, 0, 0, $width, $height, $srcW, $srcH);
            $cr = imagejpeg($ni, $dscFile);
            chmod($dscFile, 0777);
 
            if ($cr)
            {
                $sm_File = $dscFile;
                return true;
            }
            else
            {
                $Error = 5;
                return $Error;
            }
        }
    }
    // 显示错误参数
    function Err()
    {
        global $Error;
        return $Error;
    }
    // 上传后的文件名
    function UpFile()
    {
        global $doUpFile, $Error;
        if ($doUpFile != '')
            return $doUpFile;
        else
            $Error = 6;
    }
    // 上传文件的路径
    function filePath()
    {
        global $Directroy, $doUpFile, $Error;
        if ($doUpFile != '')
            return $Directroy.$doUpFile;
        else
            $Error = 6;
    }
    // 缩略图文件名称
    function thumbMap()
    {
        global $sm_File, $Error;
        if ($sm_File != '')
            return $sm_File;
        else
            $Error = 6;
    }
    // 显示版本信息
    function ieb_version()
    {
        return 'IEB_UPLOAD CLASS Ver 1.1';
    }
}
 
?>

使用方法

<?php
// 加载上传类
// include('ieb_upload.inc');
?>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
 
<body style="font-size:12px;">
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="提交">
<input name="scan" type="hidden" id="up" value="true">
</form><b /><br/>
 
<?php
if (isset($_REQUEST['scan']))
{
    // 声明一个上传类
    $upfos = new ieb_upload('file', 'tmp');
 
    /**
     * ieb_upload( FormName, [Directroy, MaxSize])
     *
     *   FormName: 文件域的名称,这个例子里用的是 file 。这个参数不能省略。
     *   Directroy: 保存上传文件的目录名称。此参数如果省略,文件将上传至该处理页目录中。
     *   MaxSize: 允许上传的文件的大小限制。默认值为 2097152 byte (即 2M)。
     */
    // 返回将要上传的文件名称
    echo '文件名称:'.$upfos->getName().'<br/>';
    // 返回文件后缀名
    echo '文件类型:'.$upfos->getExt().'<br/>';
    // 返回文件大小
    echo '文件大小:'.$upfos->getSize().'<br/>';
 
    /**
     * getSize( format )
     *
     *   format: 返回文件大小的单位值。默认值为 B。
     *   B 为 byte
     *   M 为 MB
     *   例:getSize( 'B' );
     */
    // 随机生成的文件名
    echo '随机文件:'.$upfos->newName().'<br/>';
 
    /**
     * 建议使用随机生成的文件名,以避免上传重名的文件。
     * 例如: $upfos->upload ( $upfos->newName());
     */
    // 上传文件
    $upfos->upload();
 
    /**
     * upload( filename )
     *
     *   filename: 上传至服务器后生成这个文件名。默认值为原来的文件名。
     */
    // 生成缩略图
    $upfos->thumb();
 
    /**
     * thumb( [key, width, height] )
     *
     *   key: 生成缩略图的关键字。默认值为"sm_"。如果上传的文件名为 12345.jpg,缩略图的文件名就为 sm_12345.jpg。
     *   width: 缩略图的宽度。默认值为 150 。
     *   height: 缩略图的高度。默认值为 113。
     *   例:$upfos->thumb ( 'slt_', 200, 140);
     */
    // 返回生成的文件名
    echo '生成文件:'.$upfos->UpFile().'<br/>';
    // 返回文件的路径
    echo '文件路径:'.$upfos->filePath().'<br/>';
    // 返回缩略图的名称
    echo '缩略图片:'.$upfos->thumbMap().'<br/>';
    // 返回上传类版本信息
    echo '版本信息:'.$upfos->ieb_version().'<br/>';
}
?>
</body>
</html>

php上传图片类

<?php
class ieb_upload
{
    var $FormName; //文件域名称
    var $Directroy; //上传至目录
    var $MaxSize; //最大上传大小
    var $CanUpload; //是否可以上传
    var $doUpFile; //上传的文件名
    var $sm_File; //缩略图名称
    var $Error; //错误参数
     
    function ieb_upload($formName = '', $dirPath = '', $maxSize = 2097152) // (1024*2)*1024=2097152 就是 2M
    {
        global $FormName, $Directroy, $MaxSize, $CanUpload, $Error, $doUpFile, $sm_File;
        // 初始化各种参数
        $FormName = $formName;
        $MaxSize = $maxSize;
        $CanUpload = true;
        $doUpFile = '';
        $sm_File = '';
        $Error = 0;
 
        if ($formName == '')
        {
            $CanUpload = false;
            $Error = 1;
            break;
        }
 
        if ($dirPath == '')
            $Directroy = $dirPath;
        else
            $Directroy = $dirPath.'/';
    }
    // 检查文件是否存在
    function scanFile()
    {
        global $FormName, $Error, $CanUpload;
 
        if ($CanUpload)
        {
            $scan = is_readable($_FILES[$FormName]['name']);
 
            if ($scan)
                $Error = 2;
 
            return $scan;
        }
    }
    // 获取文件大小
    function getSize($format = 'B')
    {
        global $FormName, $Error, $CanUpload;
 
        if ($CanUpload)
        {
            if ($_FILES[$FormName]['size'] == 0)
            {
                $Error = 3;
                $CanUpload = false;
            }
 
            switch ($format)
            {
                case 'B':
                    return $_FILES[$FormName]['size'];
                    break;
 
                case 'K':
                    return ($_FILES[$FormName]['size']) / (1024);
                    break;
 
                case 'M':
                    return ($_FILES[$FormName]['size']) / (1024 * 1024);
                    break;
            }
        }
    }
    // 获取文件类型
    function getExt()
    {
        global $FormName, $Error, $CanUpload;
 
        if ($CanUpload)
        {
            $ext = $_FILES[$FormName]['name'];
            $extStr = explode('.', $ext);
            $count = count($extStr)-1;
        }
        return $extStr[$count];
    }
    // 获取文件名称
    function getName()
    {
        global $FormName, $CanUpload;
 
        if ($CanUpload)
            return $_FILES[$FormName]['name'];
    }
    // 新建文件名
    function newName()
    {
        global $CanUpload, $FormName;
 
        if ($CanUpload)
        {
            $FullName = $_FILES[$FormName]['name'];
            $extStr = explode('.', $FullName);
            $count = count($extStr)-1;
            $ext = $extStr[$count];
 
            return date('YmdHis').rand(0, 9).'.'.$ext;
        }
    }
    // 上传文件
    function upload($fileName = '')
    {
        global $FormName, $Directroy, $CanUpload, $Error, $doUpFile;
 
        if ($CanUpload)
        {
            if ($_FILES[$FormName]['size'] == 0)
            {
                $Error = 3;
                $CanUpload = false;
                return $Error;
                break;
            }
        }
 
        if ($CanUpload)
        {
            if ($fileName == '')
                $fileName = $_FILES[$FormName]['name'];
 
            $doUpload = @copy($_FILES[$FormName]['tmp_name'], $Directroy.$fileName);
 
            if ($doUpload)
            {
                $doUpFile = $fileName;
                chmod($Directroy.$fileName, 0777);
                return true;
            }
            else
            {
                $Error = 4;
                return $Error;
            }
        }
    }
    // 创建图片缩略图
    function thumb($dscChar = '', $width = 150, $height = 113)
    {
        global $CanUpload, $Error, $Directroy, $doUpFile, $sm_File;
 
        if ($CanUpload && $doUpFile != '')
        {
            $srcFile = $doUpFile;
 
            if ($dscChar == '')
                $dscChar = 'sm_';
 
            $dscFile = $Directroy.$dscChar.$srcFile;
            $data = getimagesize($Directroy.$srcFile, &$info);
 
            switch ($data[2])
            {
                case 1:
                    $im = @imagecreatefromgif($Directroy.$srcFile);
                    break;
 
                case 2:
                    $im = @imagecreatefromjpeg($Directroy.$srcFile);
                    break;
 
                case 3:
                    $im = @imagecreatefrompng($Directroy.$srcFile);
                    break;
            }
 
            $srcW = imagesx($im);
            $srcH = imagesy($im);
            $ni = imagecreatetruecolor($width, $height);
            imagecopyresized($ni, $im, 0, 0, 0, 0, $width, $height, $srcW, $srcH);
            $cr = imagejpeg($ni, $dscFile);
            chmod($dscFile, 0777);
 
            if ($cr)
            {
                $sm_File = $dscFile;
                return true;
            }
            else
            {
                $Error = 5;
                return $Error;
            }
        }
    }
    // 显示错误参数
    function Err()
    {
        global $Error;
        return $Error;
    }
    // 上传后的文件名
    function UpFile()
    {
        global $doUpFile, $Error;
        if ($doUpFile != '')
            return $doUpFile;
        else
            $Error = 6;
    }
    // 上传文件的路径
    function filePath()
    {
        global $Directroy, $doUpFile, $Error;
        if ($doUpFile != '')
            return $Directroy.$doUpFile;
        else
            $Error = 6;
    }
    // 缩略图文件名称
    function thumbMap()
    {
        global $sm_File, $Error;
        if ($sm_File != '')
            return $sm_File;
        else
            $Error = 6;
    }
    // 显示版本信息
    function ieb_version()
    {
        return 'IEB_UPLOAD CLASS Ver 1.1';
    }
}
 
?>

使用方法

<?php
// 加载上传类
// include('ieb_upload.inc');
?>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
 
<body style="font-size:12px;">
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="提交">
<input name="scan" type="hidden" id="up" value="true">
</form><b /><br/>
 
<?php
if (isset($_REQUEST['scan']))
{
    // 声明一个上传类
    $upfos = new ieb_upload('file', 'tmp');
 
    /**
     * ieb_upload( FormName, [Directroy, MaxSize])
     *
     *   FormName: 文件域的名称,这个例子里用的是 file 。这个参数不能省略。
     *   Directroy: 保存上传文件的目录名称。此参数如果省略,文件将上传至该处理页目录中。
     *   MaxSize: 允许上传的文件的大小限制。默认值为 2097152 byte (即 2M)。
     */
    // 返回将要上传的文件名称
    echo '文件名称:'.$upfos->getName().'<br/>';
    // 返回文件后缀名
    echo '文件类型:'.$upfos->getExt().'<br/>';
    // 返回文件大小
    echo '文件大小:'.$upfos->getSize().'<br/>';
 
    /**
     * getSize( format )
     *
     *   format: 返回文件大小的单位值。默认值为 B。
     *   B 为 byte
     *   M 为 MB
     *   例:getSize( 'B' );
     */
    // 随机生成的文件名
    echo '随机文件:'.$upfos->newName().'<br/>';
 
    /**
     * 建议使用随机生成的文件名,以避免上传重名的文件。
     * 例如: $upfos->upload ( $upfos->newName());
     */
    // 上传文件
    $upfos->upload();
 
    /**
     * upload( filename )
     *
     *   filename: 上传至服务器后生成这个文件名。默认值为原来的文件名。
     */
    // 生成缩略图
    $upfos->thumb();
 
    /**
     * thumb( [key, width, height] )
     *
     *   key: 生成缩略图的关键字。默认值为"sm_"。如果上传的文件名为 12345.jpg,缩略图的文件名就为 sm_12345.jpg。
     *   width: 缩略图的宽度。默认值为 150 。
     *   height: 缩略图的高度。默认值为 113。
     *   例:$upfos->thumb ( 'slt_', 200, 140);
     */
    // 返回生成的文件名
    echo '生成文件:'.$upfos->UpFile().'<br/>';
    // 返回文件的路径
    echo '文件路径:'.$upfos->filePath().'<br/>';
    // 返回缩略图的名称
    echo '缩略图片:'.$upfos->thumbMap().'<br/>';
    // 返回上传类版本信息
    echo '版本信息:'.$upfos->ieb_version().'<br/>';
}
?>
</body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!