文件上传的opencart代码

Opencart code for file upload

我是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();

您没有提到您要在哪个页面上传,但不要忘记在上传时在表单中添加 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'];
}

Opencart 2.0

"I just want to insert an image in db but i am not able to upload. Here is what I've done."-Sruthi

Need to change 3 files

oc2\catalog\view\theme\default\template\account\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 line 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\catalog\model\account\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()");

如果您不明白或需要其他东西,请问我