ecshop添加上傳圖片

基礎php

cls_images.php:  function upload_image(){}html

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

 

實際操做 this

html代碼:url

<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" 不能丟掉spa

php代碼code

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


$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;
}htm

 cls_images.php blog

 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;
        }
    }
相關文章
相關標籤/搜索