2017-12-24 5 views
2

나는 내 클라이언트가 Opencart에서 파일을 업로드하고 찾아 볼 수있는 모듈을 개발 중입니다. 백 엔드 서버에서 파일을 업로드 할 때 출력이 file.zip.xyzasdf으로 표시됩니다. 난 그냥 제거 할 경우이 .xyzasdf 는 하나 ... Opencart에서 sanitize를 제거하는 방법 다운로드

public function upload() { 
    $this->load->language('catalog/download'); 

    $json = array(); 

    // Check user has permission 
    if (!$this->user->hasPermission('modify', 'catalog/download')) { 
     $json['error'] = $this->language->get('error_permission'); 
    } 

    if (!$json) { 
     if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) { 
      // Sanitize the filename 
      $filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8')); 

      // Validate the filename length 
      if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 128)) { 
       $json['error'] = $this->language->get('error_filename'); 
      } 

      // Allowed file extension types 
      $allowed = array(); 

      $extension_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('config_file_ext_allowed')); 

      $filetypes = explode("\n", $extension_allowed); 

      foreach ($filetypes as $filetype) { 
       $allowed[] = trim($filetype); 
      } 

      if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), $allowed)) { 
       $json['error'] = $this->language->get('error_filetype'); 
      } 

      // Allowed file mime types 
      $allowed = array(); 

      $mime_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('config_file_mime_allowed')); 

      $filetypes = explode("\n", $mime_allowed); 

      foreach ($filetypes as $filetype) { 
       $allowed[] = trim($filetype); 
      } 

      if (!in_array($this->request->files['file']['type'], $allowed)) { 
       $json['error'] = $this->language->get('error_filetype'); 
      } 

      // Check to see if any PHP files are trying to be uploaded 
      $content = file_get_contents($this->request->files['file']['tmp_name']); 

      if (preg_match('/\<\?php/i', $content)) { 
       $json['error'] = $this->language->get('error_filetype'); 
      } 

      // Return any upload error 
      if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) { 
       $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']); 
      } 
     } else { 
      $json['error'] = $this->language->get('error_upload'); 
     } 
    } 

    if (!$json) { 
     $file = $filename . '.' . token(32); 

     move_uploaded_file($this->request->files['file']['tmp_name'], DIR_FOLDER . $file); 

     $json['filename'] = $file; 
     $json['mask'] = $filename; 

     $json['success'] = $this->language->get('text_upload'); 
    } 

    $this->response->addHeader('Content-Type: application/json'); 
    $this->response->setOutput(json_encode($json)); 
} 

이 어떤 도움을 크게 감상 할 수 ... 방법 다음 코드에서 살균을 제거하는 감사 임의 제거

+0

OC 버전은 무엇입니까? – DigitCart

+0

@DigiCart 내 Opencart 버전은 2.3.0.2 –

답변

1

날을 제안 할 수 파일 이름에 추가 된 문자열은 간단합니다. 단지에

move_uploaded_file($this->request->files['file']['tmp_name'], DIR_UPLOAD . $file); 

변경 :

move_uploaded_file($this->request->files['file']['tmp_name'], DIR_UPLOAD . $filename); 

을하지만이 문제를 가져올 것이라는 점을 명심하십시오. OpenCart는 파일 업로드시 임의의 문자열을 데이터베이스에 저장하므로 나중에 파일을 식별하는 데 사용됩니다. 이 기능을 삭제하면 관리자 패널에 업로드 된 파일을 사용할 수 없습니다.

+0

@DigiCart입니다. 해당 문자열을 제거하는 문제가있는 경우 ... 웹 사이트에서 파일을로드 할 수있는 방법이 있습니까 (예 : pdf 파일 열기)? –

관련 문제