I just want to get the file upload progress in code Igniter.Please don't recommend any j-query integration as it is not the matter right now. Just give me a plain solution that could be done to get file progress in code igniter.
This is my code to uplaod pictures:
$base=$this->config->item('base_url');
$images=$this->config->item('images');
$config['upload_path'] = './images/teacherimages';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo "Error<br>";
}
else
{
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$pic = $upload_data['file_name'];
$this->load->model('Teacher/InsertModel');
$this->InsertModel->updatePic($pic,$id);
}
Related
I am developing a website for my university project. I have to upload some images to the site. SO I used below mentioned code. Few images cannot be uploaded to the website. I don't know the reason. Please help me. Thank you
function upload_file($filename){
$config['file_name'] = time().$_FILES[$filename]['name']; // append time to filename
$config['upload_path'] = './assets/images/adsimages';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';
$config['max_size'] = '2048000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$uploaded = $this->upload->do_upload($filename);
if ( ! $uploaded ){
$error = array('error' => $this->upload->display_errors());
$file = 'no_image.jpg'; // default file
}else{
$upload_data = $this->upload->data();
$file = $upload_data['file_name']; // uploaded file name
}
return $file; // return filename
}
I have a form including different inputs and file upload input. I want the users if they want to upload images then upload, but if they don't want to upload. Don't give an error.
if($_POST){
$config['upload_path'] = 'images/tmp';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['min_width'] = 480;
$config['min_height'] = 360;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
//Sending this $error to view and if a user didnt upload image and just filled
//the other inputs it shows error. but i dont want that.
} else { }
} else {
redirect(site_url());
}
You are on right track. Just delete or comment this line
$error = array('error' => $this->upload->display_errors());
You might be send your $error to view file like below:
$this->load->view('upload_form', $error);
So Don't send your value to view file. Just call your view when image successfully uploaded below:
if ( ! $this->upload->do_upload('userfile') )
{
// $error = array('error' => $this->upload->display_errors());
/*Delete or comment this line. Do your other stuff here if image not uploaded.*/
}else{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
/* This is example. You can do anything on successfully image upload.*/
}
You can refer codeigniter manual https://www.codeigniter.com/userguide3/libraries/file_uploading.html
$config['upload_path'] = 'images/tmp';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['min_width'] = 480;
$config['min_height'] = 360;
$this->load->library('upload', $config);
//$rand_str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// $filename=md5(str_shuffle($rand_str.mt_rand()));
// $config['file_name']=$filename;
$this->upload->initialize($config);
if ( $this->upload->do_upload('userfile'))
{
//$data = array('upload_data' => $this->upload->data());
// $data["filename"]=$filename;
// $data["ad_id"]=$lid;
// $data["filename"]=$rand_name;
// $this->Ads_model->insert_image($data);
}
I solved my problem changing the Upload class to give no error. I commented the lines including "no selected files".
I wish to upload a file to folder and then save its path in database, but i got stuck in first step only
whenever i am trying to upload a file i am getting a message
You did not select a file to upload
the code that i used is
view
<?php
echo form_open_multipart('recruiter/adddata);
$data = array(
'type'=>'file',
'name'=>'userfile',
'class'=>'fileinput btn-info',
'id'=>'filename3',
'data-filename-placement'=>'inside',
'title'=>'Resume'
);
echo form_upload($data);
$data = array(
'type'=>'submit',
'class'=>'btn btn-danger',
'name'=>'submit',
'content'=>'Submit'
);
echo form_button($data);
echo form_close();
?>
Controller
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'doc|docx|pdf';
$config['max_size'] = 10000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error); //only for checking purposes
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($data); //only for checking purposes
}
Can anyone please tell how can i upload the file and save its path in database
Make some changes like:
echo form_upload($data);
to
<?php echo form_open_multipart();?>
Controller function part:
//start of file upload code
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
//end of file upload code
I need full code , where i could upload full image size in the database (not only image path but whole image size). Thank you in advance
To upload Images or files you use following code to your controller as
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
Refferecne:Codeigniter Files upload guideline
How ever if you want to upload image using Ajax then please don't forget to knock then i will give entire codes blocks.
Thank you!
Are you trying to convert the image into bytes and save it on MYSQL database?
If yes, then try this code
<?php
$filename = "image.png";
$file = fopen($filename, "rb");
$contents = fread($file, filesize($filename));
fclose($file);
?>
The codeigniter do_upload function would not work in 000webhost.
I had already set the uploads folder permission to 777.
If I do it in XAMPP, it will work but in 000webhost ftp it wouldn't.
The following section shows my code:
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'xls|xlsx';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
redirect('customer/viewCustomer');
}
else
{
$data = array('upload_data' => $this->upload->data());
echo "success";
}
$upload_file = $this->upload->data();
}