I have to upload a base64 encoded image that i am receiving from android application. I am using php codeigniter framework.
While searching through the forum, the question at this link How to upload base64encoded image in codeigniter is same as mine, but the solution there is not working for me.
Here is the code that i have written:
private function _save_image() {
$image = base64_decode($_POST['imageString']);
#setting the configuration values for saving the image
$config['upload_path'] = FCPATH . 'path_to_image_folder';
$config['file_name'] = 'my_image'.$_POST['imageType'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if($this->upload->do_upload($image)) {
$arr_image_info = $this->upload->data();
return ($arr_image_info['full_path']);
}
else {
echo $this->upload->display_errors();
die();
}
}
I am getting "you did not select a file to upload"
Thanks for your time.
The error is occurring because codeigniter's upload library will look into the $_FILES superglobal to and search for a index you give it at the do_upload() call.
Furthermore (at least in version 2.1.2) even if you would set up the $_FILES superglobal to mimic the behaviour of a file upload it wouldn't pass because the upload library uses is_uploaded_file to detect exacly that kind of tampering with superglobals. You can trace the code in system/libraries/Upload.php:134
I'm afraid that you will have to re-implement size checking and file renaming and moving (I would do this) or you can modify codeigniter to omit that check, but it could make upgrading the framework later difficult.
Save the $image variable's content to a temporary file, and set up the $_FILES to look like this:
$temp_file_path = tempnam(sys_get_temp_dir(), 'androidtempimage'); // might not work on some systems, specify your temp path if system temp dir is not writeable
file_put_contents($temp_file_path, base64_decode($_POST['imageString']));
$image_info = getimagesize($temp_file_path);
$_FILES['userfile'] = array(
'name' => uniqid().'.'.preg_replace('!\w+/!', '', $image_info['mime']),
'tmp_name' => $temp_file_path,
'size' => filesize($temp_file_path),
'error' => UPLOAD_ERR_OK,
'type' => $image_info['mime'],
);
Modify the upload library. You can use codeigniter's built in way of Extending Native Libraries, and define a My_Upload (or your prefix) class, copy-paste the do_upload function and change the following lines:
public function do_upload($field = 'userfile')
to:
public function do_upload($field = 'userfile', $fake_upload = false)
and the:
if ( ! is_uploaded_file($_FILES[$field]['tmp_name']) )
to:
if ( ! is_uploaded_file($_FILES[$field]['tmp_name']) && !$fake_upload )
and in your controller, call do_upload() with the flowing parameters:
$this->upload->do_upload('userfile', true);
You are aware, that if you are receiving an Base64 encoded image, as a string, then you do not need to use the Upload class.
Instead, you just need to decode it using base64_decode and then use fwrite/file_put_contents to save the decoded data...
$img = imagecreatefromstring(base64_decode($string));
if($img != false)
{
imagejpeg($img, '/path/to/new/image.jpg');
}
Credit: http://board.phpbuilder.com/showthread.php?10359450-RESOLVED-Saving-Base64-image.
Related
I'm having a problem uploading ttf file to the server's upload folder, my code is working with otf, eot, woff formats, but not working with ttf.
I've added the mime type in application/config/mimes.php
'ttf' => array('font/sfnt', 'font/truetype', 'font/ttf', 'application/x-font-truetype', 'application/x-font-ttf', 'application/octet-stream'),
'otf' => array('application/vnd.oasis.opendocument.formula-template', 'application/vnd.ms-opentype'),
'woff' => 'font/woff',
'eot' => 'application/vnd.ms-fontobject',
This is my upload class config
$config['allowed_types'] = 'ttf|otf|eot|woff';
I've checked all these one by one and together, but still not working, any help will be appreciated. thanks
In codeigniter 2 and 3 there are some multiple bugs about mime typing and allowed file types... Try another upload method without codeigniter upload library or try
$config['allowed_types'] = '*';
also you can check allowed types first then upload it
$uploaded_file_extention = 'ttf'; //fetch the file extention first...
$allowed_types = array('ttf', 'otf', 'woff', 'eot');
if( !in_array($uploaded_file_extention, $allowed_types) ) {
//do not continue to upload...
} else {
//continue to upload with $config['allowed_types'] = '*';
}
Finally, I've got a solution, well I don't know that it was a CodeIgniter bug or my server is restricting me to upload these files. this code solve my issue
$mimetype = $_FILES['fontfile']['type'];
if(in_array($mimetype, array('font/sfnt', 'font/truetype', 'font/ttf', 'application/x-font-truetype', 'application/x-font-ttf', 'application/octet-stream'))) {
return true;
} else {
return false;
}
on true, you can call the CodeIgniter upload function with:
$config['allowed_types'] = '*';
Anyone looking for a solution for both PHP and CodeIgniter, here is the solution.
Thanks to #berk-kanburlar for hint
My problem is following. I'm writing this test data seed file. I already have all functionalities wrote (creating thumbnalins, hd, watermarking img, etc...) so now I would like just to reuse this code, but with some hardcoded image data location.
$imgToUploadLocation = "../../static/img/test.jpg";
$_FILE['userfile'] = imgToUploadLocation;
$config['upload_path'] = '/pictures/';
$config['allowed_types'] = 'jpeg|jpg';
$config['remove_spaces'] = true;
$config['max_size'] = '25000';
$config['encrypt_name'] = true;
$this->upload->initialize($config);
if ($this->upload->do_upload()) {
echo "finaly!";
} else {
echo "error";
}
The problem is that however i specify my image location i will always get error you did not select any image to upload.
I wonder how to use upload library without view form? If you need any additional informations please let me know and I will provide. Thank you!
This is not the proper way of defining. Files are expected in a certain way on the $FILES global variable to be uploaded.
In order to define the file you want to upload do:
$_FILES['userfile']['name'] = 'what name you want';
$_FILES['userfile']['tmp_name'] = $imgToUploadLocation;
Read more about the $_FILES in https://secure.php.net/manual/en/features.file-upload.post-method.php
I have problems on my upload page. When I try to upload file, the extension that can be selected set by "all type", but I've tried to set my allowed type config to jpg. This is my controller :
public function upload_file($noArsip)
{
$config['upload_path'] = './assets/img/arsip/';
$config['allowed_types'] = 'jpg'; // --> how to make the allowed type only image extension?
$this->load->library('upload', $config);
$this->upload->do_upload('video');
$data_upload_files = $this->upload->data();
//proses database
$image = $data_upload_files['file_name'];
$data = array(
'video'=>$image
);
$this->daftar_arsip_m->updateData($noArsip,$data);
//$this->load->view("upload_foto_v", $data);
redirect('upload_foto');
}
i dont want to use validation when the file is selected, i just want it to filter the file for the related extension, like image or video related type. thanks for your help
Separate extensions by |
$config['allowed_types'] = 'gif|jpg|png';
PS: Extensions are not case sensitive, codeigniter will take care of Upper case formats.
Reference
To filter the browse window, just add accept="image/gif,image/jpeg,image/png" as attribute to the input tag.
Example:
<input type="file" accept="image/gif,image/jpeg,image/png">
Live demo
I'm trying to upload a css file user codigniter's upload class. Here is the controller:
public function uploadcssfile()
{
$config['upload_path'] = './stylesheets/stores/'.$memberid.'/';
$config['file_name'] = 'main.css';
$config['allowed_types'] = 'css';
$config['overwrite'] = true;
$this->load->library('upload', $config);
if (! $this->upload->do_upload('filefieldname'))
{
die($this->upload->display_errors());
}
}
I just want to upload a file of type css, yet codeigniter always gives this error:
The filetype you are attempting to upload is not allowed.
I know, this is really weird but it worked for me:
in application/config/mimes.php, line 77
'css' => 'text/css',
change to:
'css' => array('text/css','text/x-c'),
To see, if your development environment has its own sense of humour and can add new mime types to any text files (my case), check your mimes like this:
if ( ! $this->upload->do_upload(-your-form-input-name-) ){
print_r($_FILES['-your-form-input-name-']['type']);
exit($this->upload->display_errors(). 'File Type: ' . $this->upload->file_type);
}
If an upload fails it will show you what mime type your server is actually getting to deal with.
PS. Now that there is an answer to your question, go here and help me :)
Firstly, I'm not asking how to perform a multi-file upload using CodeIgniter, but rather how to prevent any/all files from being uploaded if the last one in line fails (due to filetype, size, etc.). Basically I'm looking to use CI's file validation up front (before anything is uploaded), and if everything looks good, perform all uploads.
I have a form that uploads two files. If the first file passes validation and the do_upload() method returns TRUE, but the second file fails, the first file was still uploaded -- how can I prevent this?
Here's my code:
// Controller
function upload_files()
{
// Configure first file upload
$config['upload_path'] = 'public/mp3/';
$config['overwrite'] = FALSE;
$config['allowed_types'] = 'mp3';
$config['max_size'] = '20000';
$config['remove_spaces'] = TRUE;
$this->upload->initialize($config);
// Perform first file upload
if ($this->upload->do_upload('mp3'))
{
// Store upload file info for later use
$mp3_info = $this->upload->data();
// Configure second file upload
$config['upload_path'] = 'public/doc/';
$config['overwrite'] = FALSE;
$config['allowed_types'] = 'doc|docx';
$config['max_size'] = '3000';
$config['remove_spaces'] = TRUE;
$this->upload->initialize($config);
// Perform second file upload
// Even if this fails, the first file was still uploaded
if ($this->upload->do_upload('doc'))
{
// Store upload file info for later use
$doc_info = $this->upload->data();
// Perform database interactions here
}
else
{
echo $this->upload->display_errors();
}
}
else
{
echo $this->upload->display_errors();
}
}
No dice! You can't validate an upload from php or codeigniter unless you execute the upload. How can php validate something it doesn't yet have access to?
You have to do some validation up-front with javascript. If all files pass the js validation, then upload the files and complete validation with php/codeigniter.