2015-03-31 58 views
0

我是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

「*,但我無法上傳*「:請說明發生了什麼事,可以提供有用的答案。參見[問]的指導方針。 – mins 2015-03-31 05:32:54

+2

儘快幫我解決?那裏有很多付費服務,他們肯定會爲少量的錢做好準備。 – alexandresaiz 2015-03-31 06:23:35

+0

感謝您的回覆。我只是想在註冊時將圖像添加到數據庫中。 – Sruthi 2015-04-02 06:25:14

回答

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']; 
} 
+0

評論不適合擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/74265/discussion-on-answer-by-samir-das-opencart-code-for-file-upload)。 – Taryn 2015-04-01 14:44:58

2

Opencart的2.0

「我只是想插入分貝的圖像,但我不能夠上傳這裏是我的。」已經做到了。「 - 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 \目錄\控制器\工具\ 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()"); 

問我,如果你不理解或需要別的東西

+0

感謝您的回覆。但這不是關於添加新產品。我只是想在註冊時將圖像添加到數據庫中。 – Sruthi 2015-04-01 08:55:55

+0

它不添加任何產品,它只是一個用戶界面,你可以在那裏上傳圖像數據庫__________________好的「註冊時」____?...好吧,我現在瞭解你的檔案 – StackQA 2015-04-01 11:25:37

+0

仍然沒有得到 – Sruthi 2015-04-02 05:46:41