I have this situation where I'm sending an image blob via ajax to CI controller, which uploads it. The data is sent perfectly, but only when server tries to upload the file, it fails. I noticed it expects a file type with extension. Since its not provided, it fails. CI profiler shows the posted file as this:
Array
(
[name] => blob
[type] => image/png
[tmp_name] => D:xampptmpphpC5EA.tmp
[error] => 0
[size] => 634208
)
while it should be like this:
Array
(
[name] => featured.jpg
[type] => image/jpeg
[tmp_name] => D:xampptmpphpABDE.tmp
[error] => 0
[size] => 634208
)
I suspect this is where it fails. Here is my upload function code:
public function upload__image($path, $image){
$this->output->enable_profiler(TRUE);
$config['upload_path'] = "./assets/images/$path";
$config['allowed_types'] = 'png|jpeg|jpg|gif';
$config['remove_spaces'] = TRUE;
$config['file_name'] = parent::getGUID();
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload("$image"))
{
$msg = $this->upload->display_errors('', '');
return false;
}
else
{
$data = $this->upload->data();
return $data['file_name'];
}
}
any help will be appreciated. Thank you so much. Should the fix be done on client side with Jquery or on server side? Kindly recommend optimal method as well.
UPDATE
$('#saveme').click(function() {
$croppMe.cropper('getCroppedCanvas').toBlob(function (blob) {
var formData = new FormData();
var tid = $('#myid').val();
formData.append('croppedImage', blob);
formData.append('tid', tid);
$.ajax('upload.php', {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: function (response) {
if(response.Response == 200)
{
console.log("Upload successful"+response.Data);
}
else
{
console.log("some error occured");
}
}
});
});
UPDATE 2 My php upload function returns this error, when I set it to display errors:
The filetype you are attempting to upload is not allowed.