Post by danijr » Tue Jan 23, 2018 8:45 pm

Hello how can I upload photos in return.

Code: Select all

<script>
 $('button[id^=\'button-upload\']').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) {
 $('.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']);
 }
 },
 error: function(xhr, ajaxOptions, thrownError) {
 alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
 }
 });
 }
 }, 500);
 });
 </script>

Code: Select all

<div class="form-group">
 <label class="col-sm-2 control-label" for="input-file">File</label>
 <div class="col-sm-10">
 <button type="button" id="button-upload" data-loading-text="Uploading.." class="btn btn-default btn-block"><i class="fa fa-upload"></i> <?php echo 'Upload'; ?></button>
 <input type="hidden" name="file" value="" id="file"/>
 </div>
 </div>
 

Code: Select all

if($this->request->post['file']){
 $this->load->model('tool/upload');
 $upload_info = $this->model_tool_upload->getUploadByCode($this->request->post['file']);
 $phyname = DIR_UPLOAD.$upload_info['filename'];
 $temp_name = DIR_UPLOAD.$upload_info['name'];
 copy($phyname,$temp_name);
 }
 

New member

Posts

Joined
Fri Dec 02, 2016 10:28 pm

Post by straightlight » Wed Jan 24, 2018 7:45 am

Use the example from the admin/controller/tool/upload.php or from the catalog/controller/tool/upload.php file.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by danijr » Wed Jan 24, 2018 5:12 pm

Hello friend, thank for the quick answer :)
I use it opencart 2.3, i came to upload a file and send an email!
But I get a file with the name upload > size 0 kb

Code: Select all

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

		$json = array();

		if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
			// Sanitize the filename
			$filename = $this->request->files['file']['name'];

			// Validate the filename length
			if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 64)) {
				$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) {
			$json['file'] = $file = $filename;

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

			// Hide the uploaded file path so people can not link to it directly.
			$this->load->model('tool/upload');

			$json['code'] = $this->model_tool_upload->getUploadByCode($filename, $file);

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

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

New member

Posts

Joined
Fri Dec 02, 2016 10:28 pm

Post by straightlight » Wed Jan 24, 2018 10:18 pm

I use it opencart 2.3
Opencart uses 4 decimals. Imprecise version provided.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 53 guests