I am facing an issue where I cannot attach files with spaces in the name. However, when a file with no space in the name is successfully attached. I am using CodeIgniter for this purpose, uploading the file to the server before attaching it. I use the helper Path to get the routes to my file.
function upload_file() {
//$this->load->helper('path');
//$path = set_realpath('./uploads/');
//upload file
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$config['max_filename'] = '255';
$config['encrypt_name'] = FALSE;
$config['max_size'] = '50000';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if (isset($_FILES['file']['name'])) {
if (0 < $_FILES['file']['error']) {
echo 'Error during upload' . $_FILES['file']['error'];
} else {
if (file_exists('./uploads/' . $_FILES['file']['name'])) {
echo 'File name already exists: ./uploads/' . $_FILES['file']['name'];
} else {
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file')) {
echo $this->upload->display_errors();
} else {
echo 'File uploaded! : ./uploads/' . $_FILES['file']['name'];
}
}
}
} else {
echo 'Please select a file';
}
}
Part of sendMail function:
$archivo = $_FILES['file']['name'];
$path = set_realpath('./uploads/');
$adjunto = $path.$archivo;
$this->email->attach($adjunto);
Part of the view and JS
<input type="file" id="file" name="file" />
$(document).ready(function (e) {
$('#file').on('change', function () {
var file_data = $('#file').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: 'http://mail.mayordomus.cl/Cpersona/upload_file',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function (response) {
$('#msg').html(response);
},
error: function (response) {
$('#msg').html(response);
}
});
});
});