I made a code to upload files on the web, when I was still using xampp, it always going well, but after I upload to hosting, file uploading not working..
here's my code :
MODEL (for updating database record)
function edit_logo_web()
{
$timestamp = date('YmdHi');
$img = $_FILES['userfile']['name'];
$logo = $timestamp."_".$img;
$data = array (
'logo' => $logo
);
$id = 1;
$this->db->where('site.id', $id);
if($this->upload->do_upload()){
$this->db->update('site', $data);}
else{
}
}
CONTROLLER (for uploading img/file to web directory)
function logo_web()
{
$timestamp = date('YmdHi');
$img = $_FILES['userfile']['name'];
$config['file_name'] = $timestamp."_".$img;
$config['upload_path'] ='asset/img/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1600';
$config['max_height'] = '1200';
$config['remove_spaces'] = FALSE;
$this->load->library('upload', $config);
$data = array('upload_data' => $this->upload->data());
$this->m_cmm_bimbel->edit_logo_web();
echo "<script>
alert('Success !!');
window.location.href='logo_web';
</script>";
}
it Does very well in xampp localserver..
not in the hostingserver..
Any tips?
Related
I have a problem with multiple image upload in codeigniter. I want to insert to each field in database.
I've tried to upload single a file but when I change to multiple upload, it's not working.
This is my controller to add the whole data from upload images:
public function addprod(){
$item->gambar_produk = null;
$item->thumb_produk1 = null;
$data = array(
'page' => 'addprod',
'row' => $item
);
$data['title'] = 'Form Add';
$this->load->view('admin/addprod', $data);
}
This is My Controller to add images and this is my first images controller:
$config['upload_path'] = './assets/upload/images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2400';
$config['max_width'] = '2024';
$config['max_height'] = '2024';
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')){
$post['image'] = $this->upload->data('file_name');
$this->Model_product->add($post);
redirect('Product/viewprod');
} else {
$error = $this->upload->display_errors();
$this->session->flashdata('error', $error);
redirect('Product/addprod');
}
and this is my second images:
$config2['upload_path'] = './assets/upload/images/';
$config2['allowed_types'] = 'gif|jpg|png|jpeg';
$config2['max_size'] = '2400';
$config2['max_width'] = '2024';
$config2['max_height'] = '2024';
$this->load->library('upload', $config2);
if ($this->upload->do_upload('image2')){
$post['image2'] = $this->upload->data('file_name');
$this->Model_product->add($post);
redirect('Product/viewprod');
} else {
redirect('Product/addprod');
}
this is my model:
public function add($post){
$data = [
'image' => $post['image'],
'image2' => $post['image2']
];
$this->db->insert('tb_prod',$data);
}
currently I'm working with codeigniter framework but I'm confused how to resize the image. I have followed the documentation but I'm still confused. This is my code
public function create(){
//Check login
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Create Post';
$data['categories'] = $this->BlogModel->get_categories();
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('content','content','required');
if($this->form_validation->run() === FALSE){
$this->load->view('frontend/header');
$this->load->view('frontend/navbar');
$this->load->view('frontend/blog/create',$data);
$this->load->view('frontend/footer');
} else{
//Upload Image
$config['upload_path'] = './assets/images/posts/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->BlogModel->create_post($post_image);
//set message
$this->session->set_flashdata('post_created','Your post has been created');
redirect('blog');
}
}
Please advice how to resize the image.
Thanks!
If you want to do different things like resize the original image directly, rather than creating a thumb set create_thumb to false, and do not use new_image. The only difference between create_thumb and new_image is that new_image allows you to specify your own path and name. Whereas the most you can do with create_thumb is change the thumb_marker. Which is all in the docs...
//Upload Image
$config['upload_path'] = './assets/images/posts/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
//$errors = array('error' => $this->upload->display_errors());
exit($this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$up_data = $this->upload->data();
$post_image = $up_data['name'];
$config = array(); // clear prev config
$config['image_library'] = 'gd2';
$config['source_image'] = $up_data['full_path'];
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_thumbnail';
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) {
exit($this->image_lib->display_errors());
}
$thumb_path = $up_data['file_path'] . $up_data['raw_name'] . $config['thumb_marker'] . $up_data['file_ext'];
}
The exits you can remove later, but leave them for debugging until you figure out a way to properly message your errors (I suggest session vars). These functions should also be moved to a model.
My image is not get inserted while uploading using codeigniter:
function add_newblog()
{
$sess_id = $this->session->userdata('id');
$result['query'] = $this->login_model->profile($sess_id);
foreach($result['query'] as $row)
{
$email = $row->blogger_email;
$url = $row->blogger_url;
$author = $row->blogger_name;
if ($this->input->post('submit')) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$media = 'No Image Uploaded';
$title = $this->input->post("title");
$category = $this->input->post('category');
$content = $this->input->post('content');
$this->blog_model->add_newblog($sess_id,$title,$category,$content,$media,$email,$url,$author);
$this->session->set_flashdata('valid', 'Blog Created without Image');
redirect('content_writer/add_blog');
}
else {
$data = array('upload_data' => $this->upload->data());
$data = $this->upload->data();
$media = $data['file_name'];
$title = $this->input->post("title");
$category = $this->input->post('category');
$content = $this->input->post('content');
$this->blog_model->add_newblog($sess_id,$title,$category,$content,$media,$email,$url,$author);
$this->session->set_flashdata('valid', 'Blog Created');
redirect('content_writer/add_blog');
}
}
else{
$this->session->set_flashdata('invalid', 'Invalid');
redirect('content_writer/add_blog');
}
}
}
The else condition always works. The image name does not get saved in the image path.
the problem is here in this line
$config['upload_path'] = './uploads/';
instead use
$config['upload_path'] = 'uploads/<folder name>';
OR incase you want to save the image directly in uploads folder just use
$config['upload_path'] = 'uploads/';
as well as make your that you form in the veiw section is open with form_open_multipart()
hope this will solve your problem
Set your max_size, max_width and max_height. This would be an example below.
$config['max_size'] = '3000';
$config['max_width'] = '1500';
$config['max_height'] = '1500';
You havent passed the image name in $this->upload->do_upload() method.
use your
input type='file' name=myDoc
$this->upload->do_upload("myDoc")
I have a problem with my multiple upload with codeigniter,
using image
another using pdf
When I uploaded the file uploaded twice and how to call different path to uploaded to database. this my code
Controller
public function upload(){
$catalog='catalog';
$userfile='userfile';
//for cover
$config['upload_path'] = './file/book/'; //Use relative or absolute path
$config['allowed_types'] = 'gif|jpg|png|';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$c=$this->upload->do_upload($userfile);
//for catalog
$config['upload_path'] = './file/book/pdf/'; //Use relative or absolute path
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$cat=$this->upload->do_upload($catalog);
if(!$this->upload->do_upload($catalog) && $this->upload->do_upload($userfile)){
$error = array('error' => $this->upload->display_errors());
$this->load->view('uploadds', $error);
}else{
$this->load->model("book_model");
$this->book_model->addnewbook();
redirect('book/book');
}
}
This model
function addnewbook(){
$fcat=array('upload_data' => $this->upload->data($userfile));
$fcatalog=array('upload_dataa' => $this->upload->data($catalog));
}
You need to handle multiple uploads independently. For this, you have to create separate custom objects for both uploads while loading the upload library. (See the code comments)
public function upload() {
// Cover upload
$config = array();
$config['upload_path'] = './file/book/';
$config['allowed_types'] = 'gif|jpg|png|';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config, 'coverupload'); // Create custom object for cover upload
$this->coverupload->initialize($config);
$upload_cover = $this->coverupload->do_upload('cover');
// Catalog upload
$config = array();
$config['upload_path'] = './file/book/pdf/';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->load->library('upload', $config, 'catalogupload'); // Create custom object for catalog upload
$this->catalogupload->initialize($config);
$upload_catalog = $this->catalogupload->do_upload('catalog');
// Check uploads success
if ($upload_cover && $upload_catalog) {
// Both Upload Success
// Data of your cover file
$cover_data = $this->coverupload->data();
print_r($cover_data);
// Data of your catalog file
$catlog_data = $this->catalogupload->data();
print_r($catlog_data);
} else {
// Error Occured in one of the uploads
echo 'Cover upload Error : ' . $this->coverupload->display_errors() . '<br/>';
echo 'Catlog upload Error : ' . $this->catalogupload->display_errors() . '<br/>';
}
}
Use the data on $cover_data['full_path'] and $catlog_data['full_path'] to update your database
$config['upload_path'] = 'frontend_assets/images/hospital';
$config['allowed_types'] = 'gif|jpg|png|jpeg|JPEG||JPG|PNG';
$this->load->library('upload', $config);
if($_FILES['logo']['name']){
$config['file_name'] = time() . $_FILES["logo"]['name'];
if (!$this->upload->do_upload('logo')) {
$this->upload->display_errors();
} else {
$upload = $this->upload->data();
$insert_data['logo'] = $config['upload_path'] . '/' . $upload['file_name'];
}
}
if($_FILES['bulding_photo']['name']){
$config['file_name'] = time() . $_FILES["bulding_photo"]['name'];
if (!$this->upload->do_upload('bulding_photo')) {
$this->upload->display_errors());
} else {
$upload = $this->upload->data();
$insert_data['bulding_photo'] = $config['upload_path'] . '/' . $upload['file_name'];
$this->image_size_fix($insert_data['bulding_photo'], $width = 200, $height = 200);
}
}
$config['upload_path'] = './uploads/logo/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image_bg'))
{
//$error = array('error' => $this->upload->display_errors());
//$this->load->view('upload_form', $error);
echo 'fail';
}
else
{
//some actions
}
when i upload a file with cyrillic names, they look worse, how can I convert a file name to a windows-1251 unicode after uploading it to the server in codeigniter?
set the file name in config.
$config['file_name'] = 'image_' . time();
$config[0]['upload_path'] = './uploaded_files/banner';
$config[0]['allowed_types'] = 'gif|jpg|png';
$config[0]['file_name'] = $image_id."_ipad";
$config[0]['remove_spaces'] = TRUE;
$config[0]['overwrite'] = TRUE;
//Loading Library - File Uploading
$this->load->library('upload');
$this->upload->initialize($config[0]);
$this->upload->do_upload('banner_image_ipad');
$data = array('upload_data' => $this->upload->data());
$banner_image_ipad = $data['upload_data']['file_name'];