function upload($path){
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = 2000;
if($path =='profile'){
$config['upload_path'] = '../assets/uploads/avatars';
$config['file_name'] = $this->id;
}
if($path =='company'){
$config['upload_path'] = '../assets/uploads/company';
$config['file_name'] = $this->id.'_company';
}
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
if($path == 'profile'){
$this->db->update('be_user_profiles',array('avatar' => $image_data['file_name']), array('user_id' => $this->id));
}
if($path == 'company'){
$this->db->update('be_user_profiles',array('company_logo' => $image_data['file_name']), array('user_id' => $this->id));
}
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->upload_path,
'maintain_ratio' => true,
'width' => 500,
'height' => 500
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
This is my upload function, and it works fine when $path = 'profile', but when it is company, it won't upload to the "company" folder...
Is there any reason it should be so?! I'm at a loss here...This function works when it goes to the avatar folder, but not if it goes to the company folder...
I had a similar problem with multiple folders. The way I fixed was to use to use the initialize function instead of passing the config as an argument to the load library function.
$this->upload->initialize($config);
You could load the library then set your config and call the initialize method.
Hope this helps.
I think all suggestions are really good, you can also make sure you are posting the right data in your form:
$this->upload->do_upload(); is by default expecting the form name to be 'userfile'
also I find sometimes really usefull to have some errors displayed...so instead of just $this->upload->do_upload(); try something like
if (!$this->upload->do_upload($userfile)) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_error', $error);
}
and then add a file in your views called upload_error.php with the following code in it:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
</body>
</html>
good luck!
Try to unset config before setting another config like this.
The following code unset config and clear image library.
You need to clear your lib.
//Unset config for the next one
unset($config);
$this->image_lib->clear();
From CI manual...
$this->image_lib->clear()
The clear function resets all of the values used when processing an image. You will want to call this if you are processing images in a loop.
Hmmm, Here are two suggestions:
Make sure your company folder is writable
What $this->id returns? if it's returning the file name WITH the extension then this is what causing your upload process to fail because then you'll have a file name my_image.png_company which is not an allowed type.
Please refer to the documentation page for more info.
$config = array(
'upload_path' './assets/upload/profilepic/',
'allowed_types' => 'JPG|JPEG|PNG',
'overwrite' => TRUE,
'file_name' => date('YmdHis').''.rand(0,9999)
);
its not works for JPEG file type
also i had written in small latter but it didn't works.
please Share if you have something
JPEG files are not upload Others file are inserted easely.
Related
I have a web page form that needs to select image and upload to a file, but for some reasons i dont know the image is not being detected. I might be missing something. The image folder is was created in the same folder as the application and system. Find the code below
View
<div id="gallery">
<div id="upload">
<?php echo $error;?>
<?php
echo form_open_multipart('index.php/favoriteMemories/pleasurable');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>
</div>
Controller
public function pleasurable() {
$config = array(
'upload_path' => '../images/',
'allowed_types' => 'gif|jpg|png|jpeg',
'max_size' => '1000000',
'overwrite' => TRUE,
'remove_spaces' => TRUE,
'encrypt_name' => FALSE
);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('pleasurable', $error);
} else {
$this->upload->do_upload('userfile');
$this->load->view('pleasurable');
}
}
Make sure the following,
In your controller function you don't need to use the upload code twice.
$this->upload->do_upload('userfile');
its already in the if condition you don't need to call it in else part.
Make sure you have loaded the upload library because I didn't see that in controler function, you might load it in constructor or autoload but make sure.
$this->load->library('upload', $config);
Make sure you image folder path is correct and writable
Check the image type and size you provided in the $config array.
if you see any error message please post it for more help.all the best.
i have gone through your code. there is no error in ur code. accept the upload path.
if you change it to 'upload_path' => 'image/',
it will save the image in image folder located in root. just make sure the image folder is writable. you can print the upload data and change the folder accordingly.
use print_r($this->upload->data());
to check the uploaded data
I am creating some file upload functionality utilizing the upload library within the Code Igniter framework and I am having an issue where I keep on getting an error back stating that my file upload path is incorrect. I have checked many times and there is no issue with the file structure and the files definitely exist.
Here is my model upload code (I am dieing to check where I am within the process):
class Gallery_model extends CI_Model {
var $gallery_path;
function __construct() {
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images/profileImages');
//$this->gallery_path = realpath($this->config->base_url() . 'images/profileImages');
}
public function doUpload() {
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->gallery_path
);
$this->load->library('upload', $config);
if($this->upload->do_upload()) {
$data = array('upload_data' => $this->upload->data());
die(print_R($data));
} else {
$error = array('error' => $this->upload->display_errors());
die(print_R($error));
}
}
}
My images folder is outside of the application and is called 'images', which contains a folder called 'profileImage' where I want this files to save. I get the following error:
'The upload path does not appear to be valid.'
I can't for the life of me figure out why this path is not working when it definitely exists. Does anyone have any ideas?
Thanks
Codeigniter will only return that error message if $config['upload_path'] is blank or the php command is_dir($config['upload_path']) returns false. Check to make sure the 'uploads' folder is in the same directory as main index.php file and that the directory name is the same case
Is your images folder in the web root? If is this should work
$this->gallery_path = './images/profileImages/';
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 :)
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.
I've got an image uploader that doesn't upload images. I'm not receiving any errors and i've checked the folder permissions and even on 777 no images are getting uploaded.
You can see the code here: http://pastebin.com/gvH1dKh9
The value for gallery_path is /home/domain/public_html/assets/images/gallery
The value for gallery_path_url is http://domain.com/assets/images/gallery/
I have used this same code on another site with zero problems. I'm not sure what the problem is with this site?
Use
$this->load->library('upload', $config);
instead of
$this->upload->initialize($config);
In my case it was helpful
Models are intended for interaction with databases. Try moving your upload code into the controller, then if needed take the returned data ($this->upload->data();) and pass that to a model for insertion to a database.
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => '/uploads',
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$this->Gallery_model->insertImageData($image_data);
}
}
Try doing some error checking on your upload:
if(! $this->upload->do_upload('Filedata')){
echo $this->upload->display_errors();
}
$upload_info = $this->upload->data();
echo var_dump($upload_info);