私人领地

ecshop添加上传图片

基础

cls_images.php:  function upload_image(){}

$_FILES 输出值:Array ( [group_thumb_url] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )

 

实际操作 

html代码:

<form method="post" action="group_buy.php?act=insert_update" name="theForm" onsubmit="return validate()" enctype="multipart/form-data">
<input type="file" name="group_thumb_url" size="35" />
</form>

注意 :enctype="multipart/form-data" 不能丢掉

php代码

include_once(ROOT_PATH . '/includes/cls_image.php');  /*必须加上*/
$image = new cls_image($_CFG['bgcolor']);


$group_thumb_name = 'group_'.time().'.jpg'; /*图片名称命名*/ $upload_image= basename($image->upload_image($_FILES['group_thumb_url'],'group',$group_thumb_name));

if(!empty($upload_image)){     /*判断是否有新的文件上传*/
$group_thumb = $upload_image;
}

 cls_images.php 

 function upload_image($upload, $dir = '', $img_name = '')
    {
        /* 没有指定目录默认为根目录images */
        if (empty($dir))
        {
            /* 创建当月目录 */
            $dir = date('Ym');
            $dir = ROOT_PATH . $this->images_dir . '/' . $dir . '/';
        }
        else
        {
            /* 创建目录 */
            //$dir = ROOT_PATH . $this->data_dir . '/' . $dir . '/';
             $dir = ROOT_PATH .'/images/'. $dir;  /*新增 文件只想地址是跟目录下images/  */
            if ($img_name)
            {
                $img_name = $dir . $img_name; // 将图片定位到正确地址
            }
        }

        /* 如果目标目录不存在,则创建它 */
        if (!file_exists($dir))
        {
            if (!make_dir($dir))
            {
                /* 创建目录失败 */
                $this->error_msg = sprintf($GLOBALS['_LANG']['directory_readonly'], $dir);
                $this->error_no  = ERR_DIRECTORY_READONLY;

                return false;
            }
        }

        if (empty($img_name))
        {
            $img_name = $this->unique_name($dir);
            $img_name = $dir . $img_name . $this->get_filetype($upload['name']);
        }

        if (!$this->check_img_type($upload['type']))
        {
            $this->error_msg = $GLOBALS['_LANG']['invalid_upload_image_type'];
            $this->error_no  =  ERR_INVALID_IMAGE_TYPE;
            return false;
        }

        /* 允许上传的文件类型 */
        $allow_file_types = '|GIF|JPG|JEPG|PNG|BMP|SWF|';
        if (!check_file_type($upload['tmp_name'], $img_name, $allow_file_types))
        {
            $this->error_msg = $GLOBALS['_LANG']['invalid_upload_image_type'];
            $this->error_no  =  ERR_INVALID_IMAGE_TYPE;
            return false;
        }

        if ($this->move_file($upload, $img_name))
        {
            return str_replace(ROOT_PATH, '', $img_name);
        }
        else
        {
            $this->error_msg = sprintf($GLOBALS['_LANG']['upload_failure'], $upload['name']);
            $this->error_no  = ERR_UPLOAD_FAILURE;

            return false;
        }
    }

 

posted @ 2015-10-14 17:25  狂奔的蜗牛Snails  阅读(5113)  评论(0编辑  收藏  举报