用于文件上传的Opencart代码

时间:2015-03-31 05:30:16

标签: opencart

我是Opencart的新手,真的很棒。

我只是想在数据库中插入图片,但我无法上传。这就是我所做的。

模板:

<div class="col-sm-10">
    <img src="" name="image" id="image" style="width: 26%;height: 100px;" >
    <input type="file" 
           name="photo" 
           value="<?php echo $photo; ?>" 
           placeholder="<?php echo $entry_photo; ?>" 
           id="photo" 
           onChange="PreviewImages();"/>
    <?php if ($error_photo) { ?>
    <div class="text-danger"><?php echo $error_photo; ?></div>
    <?php } ?>
</div>

控制器:

if (is_uploaded_file($this->request->files['photo']['tmp_name'])) {
    $handle = fopen($this->request->files['photo']['tmp_name'], "r");

    // If we know there is only a 10 chars PIN per line 
    // it is better to lower the expected line length 
    // to lower the memory consumption...
    while (($pins = fgetcsv($handle, 50, ",")) !== false) { 
        $data['photo'][] = $pins; // there is only one PIN per line
    }
    fclose($handle);
} else {
    $data['photo'] = array();
}

型号:

$this->db->query("
    INSERT INTO " . DB_PREFIX . "customer 
    SET customer_group_id = '" . (int)$customer_group_id . "', 
        store_id = '" . (int)$this->config->get('config_store_id') . "', 
        firstname = '" . $this->db->escape($data['firstname']) . "', 
        lastname = '" . $this->db->escape($data['lastname']) . "',
        photo = '" . $this->db->escape($data['photo']) . "',
        date = '" . $this->db->escape($data['date']) . "',
        email = '" . $this->db->escape($data['email']) . "',
        telephone = '" . $this->db->escape($data['telephone']) . "',
        fax = '" . $this->db->escape($data['fax']) . "',
        custom_field = '" . $this->db->escape(isset($data['custom_field']['account']) ? serialize($data['custom_field']['account']) : '') . "',
        salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "',
        password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "',
        newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "',
        ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "',
        status = '1',
        approved = '" . (int)!$customer_group_info['approval'] . "',
        date_added = NOW()
");
$customer_id = $this->db->getLastId();

2 个答案:

答案 0 :(得分:2)

Opencart 2.0

&#34;我只想在数据库中插入图像,但我无法上传。这就是我所做的。&#34; -Sruthi

  

需要更改3个文件

     

OC2 \目录\视图\主题\默认\模板\帐户\ register.tpl

<div class="form-group required">
            <label class="col-sm-2 control-label" for="input-telephone">Upload Image</label>
            <div class="col-sm-10">
              <button type="button" id="uploadimage" data-loading-text="<?php echo $text_loading; ?>" class="btn btn-default"><i class="fa fa-upload"></i> Upload Image</button>
              <input type="hidden" name="custom_field" value="" />
              <input type="hidden" name="photo" />
              <div id="demo"></div>
            </div>
          </div>


<script type="text/javascript"><!--
$('button[id^=\'uploadimage\']').on('click', function() {
  var node = this;

  $('#form-upload').remove();

  $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

  $('#form-upload input[name=\'file\']').trigger('click');

  timer = setInterval(function() {
    if ($('#form-upload input[name=\'file\']').val() != '') {
      clearInterval(timer);

      $.ajax({
        url: 'index.php?route=tool/upload',
        type: 'post',
        dataType: 'json',
        data: new FormData($('#form-upload')[0]),
        cache: false,
        contentType: false,
        processData: false,
        beforeSend: function() {
          $(node).button('loading');
        },
        complete: function() {
          $(node).button('reset');
        },
        success: function(json) {
          $(node).parent().find('.text-danger').remove();

          if (json['error']) {
            $(node).parent().find('input').after('<div class="text-danger">' + json['error'] + '</div>');
          }

          if (json['success']) {
            alert(json['success']);
            $(node).parent().find('input').attr('value', json['code']);
            $(node).parent().find('input[name=photo]').attr('value', json['photo']);           
            document.getElementById("demo").innerHTML = '<img src="' + json['photo'] +'" width="100px" height="100px">';            
          }
        },
        error: function(xhr, ajaxOptions, thrownError) {
          alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
      });
    }
  }, 500);
});
//--></script>
  

oc2 \ catalog \ controller \ tool \ upload.php第70行

$json['code'] = $this->model_tool_upload->addUpload($filename, $file);
            $json['success'] = $this->language->get('text_upload');

$json['code'] = $this->model_tool_upload->addUpload($filename, $file);
            $json['success'] = $this->language->get('text_upload');
            $json['photo'] = DIR_UPLOAD . $file;
  

OC2 \目录\模型\帐户\ customer.php

$this->db->query("INSERT INTO " . DB_PREFIX . "customer 
            SET customer_group_id = '" . (int)$customer_group_id . "', 
            store_id = '" . (int)$this->config->get('config_store_id') . "', 
            firstname = '" . $this->db->escape($data['firstname']) . "', 
            lastname = '" . $this->db->escape($data['lastname']) . "', 
            photo = '" . $this->db->escape($data['photo']) . "', 
            email = '" . $this->db->escape($data['email']) . "', 
            telephone = '" . $this->db->escape($data['telephone']) . "', 
            fax = '" . $this->db->escape($data['fax']) . "', 
            custom_field = '" . $this->db->escape(isset($data['custom_field']['account']) ? serialize($data['custom_field']['account']) : '') . "', 
            salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "', 
            password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', 
            newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', 
            ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', 
            status = '1', 
            approved = '" . (int)!$customer_group_info['approval'] . "', 
            date_added = NOW()");

问我你是否理解或不需要其他东西

答案 1 :(得分:0)

您没有提及您尝试上传的页面,但在上传时不要忘记在表单中添加enctype =“multipart / form-data”。

if (is_uploaded_file($this->request->files['photo']['tmp_name'])) {
$handle = fopen($this->request->files['photo']['tmp_name'], "r");

// If we know there is only a 10 chars PIN per line 
// it is better to lower the expected line length 
// to lower the memory consumption...
while (($pins = fgetcsv($handle, 50, ",")) !== false) { 
    $data['photo'][] = $pins; // there is only one PIN per line
}
fclose($handle);
} else {
$data['photo'] = array();
}

请按以下代码替换上述代码:

$data['photo'] = '';
$uploads_dir = 'image/data/'; // set you upload path here
if (is_uploaded_file($this->request->files['photo']['tmp_name'])) {

    move_uploaded_file($this->request->files['photo']['tmp_name'],$uploads_dir.$this->request->files['photo']['name']);
  $data['photo'] = $this->request->files['photo']['name'];
}