I need help to upload picture in folder. Here's the thing, I have tested before and it worked. After a while I test this again and boom it doesn't work. I really can't think straight here, please help!
my view :
<?php
echo form_open_multipart("adminFolder/admin/insert_picture");
echo form_upload("userfile", "Gambar Picture");
echo form_submit("input_picture", "Input now !!!");
?>
my controller :
public function insert_picture(){
$this->model_get->doUpload();
}
my model :
function doUpload(){
$path = './assets/images/';
chmod($path, 0777);
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '6000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$this->load->library("upload", $config);
if(!$this->upload->do_upload()){
redirect("adminFolder/admin/insertPicture");
}else{
redirect("adminFolder/admin/adminPic");
}
}
You should require to pass filename in do_upload :
So your code of model will be :
if(!$this->upload->do_upload('userfile')){
redirect("adminFolder/admin/insertPicture");
}else{
redirect("adminFolder/admin/adminPic");
}
And this will work !! Please try
use in view:
input type="file" multiple="multiple" id="userfile" name="userfile"
Controller
In your controller method add this line
$this->gallery_model->do_upload($data);
Create One model class "gallery_model"
var $gallery_path;
var $gallery_path_url;
function Gallery_model() {
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url().'images/';
}
function do_upload() {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
function get_images() {
$files = scandir($this->gallery_path);
$files = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($files as $file) {
$images []= array (
'url' => $this->gallery_path_url . $file,
'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
);
}
return $images;
}
If you need Multiple Image Upload I can paste the Code for that also.!!
Related
I am trying last 2 days i am getting same error again and again when i am uploading image
public function add_theme(){
$config = [
'upload_path' => './uploads/',
'allowed_types' => 'jpg|gif|png',
];
$this->load->library('upload', $config);
if($this->form_validation->run('add_theme') && $this->upload->do_upload()){
$data = $this->upload->data();
echo '<pre>';
print_r($data);
exit;
}else{
$error = array('error' => $this->upload->display_errors());
echo $error;//$this->create_template();
}
}
can use
$this->load->library('upload');
$this->upload->initialize($config);
i've error when upload and store an image to mysql without active record codeigniter.
It show
Message: Illegal string offset 'file_ext'
Not only file_ext, but file_size too. This problem only happen when i don't use active record like this code below:
<?php
if ($_FILES['userfile']['error'] <> 4)
{
$nmfile = $this->input->post('name');
$config['upload_path'] = './assets/images/user/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '2048'; // 2 MB
$config['max_width'] = '2000'; //pixels
$config['max_height'] = '2000'; //pixels
$config['file_name'] = $nmfile;
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$this->create();
}
else
{
$userfile = $this->upload->data();
$thumbnail = $config['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/images/user/'.$userfile['file_name'].'';
// membuat thumbnail
$config['create_thumb'] = TRUE;
// rasio resolusi
$config['maintain_ratio'] = FALSE;
// lebar
$config['width'] = 150;
// tinggi
$config['height'] = 150;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$id = $this->input->post('id');
$name = $this->input->post('name');
$username = $this->input->post('username');
$psw1 = $this->input->post('psw1');
$psw2 = $this->input->post('psw2');
$usertype = $this->input->post('usertype');
$userfile = $nmfile;
$userfile_type = $userfile['file_ext'];
$userfile_size = $userfile['file_size'];
$sql = $this->db->query("INSERT INTO user (id, name, username, psw1, psw2, usertype, userfile, userfile_type, userfile_size)
VALUES ('$id', '$name', '$username', password('$psw1'), password('$psw2'), '$usertype', '$userfile', '$userfile_type', '$userfile_size') ");
$this->session->set_flashdata('message', '<div class="alert alert-success alert">Data berhasil dibuat</div>');
redirect(site_url('auth/user'));
}
}
?>
any help will be so appricated, thank you
I believe you can achieve your results in a lovely style like below:
if (!empty($_FILES)) {
$data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'username' => $this->input->post('username'),
'psw1' => $this->input->post('psw1'),
'psw2' => $this->input->post('psw2'),
'usertype' => $this->input->post('usertype')
);
$upload = $this->imageUpload();
if (!$upload) {
$this->create();
} else {
$data['userfile'] = $upload['upload_data']['file_name'];
$data['userfile_type'] = $upload['upload_data']['file_ext'];
$data['userfile_size'] = $upload['upload_data']['file_size'];
$this->db->insert('user', $data);
$this->session->set_flashdata('message', '<div class="alert alert-success alert">Data berhasil dibuat</div>');
redirect(site_url('auth/user'));
}
}
Use these functions to upload and resize your image
function imageUpload() {
$config = array(
'upload_path' => "uploads",
'allowed_types' => "*",
'overwrite' => true,
'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "3000",
'max_width' => "3000"
);
$this->upload->initialize($config);
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile')) {
$response = array('upload_data' => $this->upload->data());
$this->doResize($response['upload_data']['file_name']);
return $response;
} else {
$error = array('error' => $this->upload->display_errors());
return false;
// var_dump($error); die(); // check errors
}
}
function doResize($filename) {
$sourcePath = './assets/images/user/' . $filename;
$targetPath = './assets/images/user/thumb_' . $filename;
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $sourcePath,
'new_image' => $targetPath,
'maintain_ratio' => true,
'width' => 100,
'height' => 100
);
$this->image_lib->initialize($config_manip);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
// Check errors
echo $this->image_lib->display_errors();
die();
}
}
i am learning how to upload an image with codeigniter, and i got an error permission denied when i click on the upload without any image, it works well if i select an image. i just hoped it will show my message it is: Please upload an Images,can you have a look on my code and help me figure down my problem.
the error says:
This is my view code:
<head>
<title>CI upload images</title>
<meta charset="UTF-8">
</head>
<body>
<div id="gallery">
<?php if(isset($images) && count($images)):
foreach ($images as $image): ?>
<div class="thumb">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['thumb_url']; ?>"/>
</a>
</div>
?>
<?php endforeach; else: ?>
<div id="blank_gallery">Please upload an Images</div>
<?php endif; ?>
</div>
<div id="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>
</body>
and this is my controller code:
class Gallery extends CI_Controller{
function index(){
$this->load->model('Gallery_model');
if($this->input->post('upload')){
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$this->load->view('gallery_view', $data);
}
}
and here is my model
class Gallery_model extends CI_Model{
var $gallery_path;
var $gallery_path_url;
function __construct(){
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url().'images/';
}
function do_upload(){
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
function get_images(){
$files = scandir($this->gallery_path);
$files = array_diff($files, array('.','..','thumbs'));
$images = array();
foreach ($files as $files) {
$images []= array(
'url'=> $this->gallery_path_url . $files,
'thumb_url' => $this->gallery_path_url . 'thumbs/' . $files
);
}
return $images;
}
}
can you please help?
'new_image' => $this->gallery_path . '/thumbs',
Sets the destination image name/path. You'll use this preference when creating an image copy. The path must be a relative or absolute server path, not a URL.
you set new_image as folder, not filename. make it like $this->gallery_path . '/thumbs/my_new_file.ext'
Do u hv terminal access ? Go to the root of you project folder and type the following in your terminal sudo chmod -R 755 * or sudo chmod -R 777 *
The first option has issues in some some projects hence the second option.
Do not upload the image directly without checking the errors. Before uploading check there any errors in file. Change your function as below:
function index()
{
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$errors = $this->Gallery_model->do_upload();
if (!$errors) {
$data['images'] = $this->Gallery_model->get_images();
$this->load->view('gallery_view', $data);
} else {
// your error displaying page.
}
}
}
function do_upload()
{
$error = array();
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
} else {
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
return $error;
}
For more information read HERE
First Validate the image upload or not, then resize the image
$result = array();
if (!$this->upload->do_upload('userfile')) {
$result['error'] = $this->upload->display_errors();
}else {
$image_data = $this->upload->data();
$result['image_data'] = $image_data;
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
return $result;
I want to upload picture from rest client to server and save file name at the same time to database using codeigniter. but it only save the other data, the picture and filename didnt upload at all
here is api model.
function save_confirm()
{
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = TRUE;
$config['max_size'] = '100000';
$config['max_width'] = '100000';
$config['max_height'] = '100000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload("pict");
$data = array('upload_data'=>$this->upload->data("pict"));
$file_name = $data['upload_data']['file_name'];
$data = array(
'id_booking' => $this->input->get('id_booking'),
'username' => $this->get_username_by_user_token(),
'transfer_date' => date('Y-m-d'),
'pict' => $file_name
);
$this->db->insert('confirmation',$data);
return $this->db->insert_id();
}
api controller code
public function confirm_get(){
$get_confirm = $this->api_model->save_confirm();
$data = array(
'meta' => array(
'status' => 200,
'message' => "OK",
"code" => 9
));
$this->response($data,200) ;
}
please help, thank you
You have your do_updoad_data() wrong see changes below must set parent variable i.e $file_data = $this->do_upload->data();
function save_confirm()
{
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = TRUE;
$config['max_size'] = '100000';
$config['max_width'] = '100000';
$config['max_height'] = '100000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload("pict") == true) {
$file_data = $this->do_upload->data();
$file_name = $file_data['file_name'];
// http://www.codeigniter.com/user_guide/libraries/file_uploading.html
// Examples
$file_type = $file_data['file_type'];
$file_path = $file_data['file_path'];
$full_path = $file_data['full_path'];
$data = array(
'id_booking' => $this->input->get('id_booking'),
'username' => $this->get_username_by_user_token(),
'transfer_date' => date('Y-m-d'),
'pict' => $file_name
);
$this->db->insert('confirmation',$data);
return $this->db->insert_id();
} else {
// return false;
}
}
Hello all im working on a admin system that can create news with a image but i cant find out how to send the image name from my model file to my controller,
this is my model file:
function uploadImg()
{
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000,
'encrypt_name' => true
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 200,
'height' => 200,
'encrypt_name' => true,
'max_size' => 2000
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
# Ret profil billed navn #
$file_array = $this->upload->data('file_name');
return $billed_sti['billed_sti'] = $file_array['file_name'];
//$this->db->where('username', $this->input->post('username'));
//$this->db->update('users', $profilBilledNavn);
}
This is my controller:
function opret() {
$this->form_validation->set_rules('overskrift', 'overskrift', 'required');
$this->form_validation->set_rules('description', 'description', 'required');
$this->form_validation->set_rules('indhold', 'indhold', 'required');
if($this->form_validation->run() == true)
{
$this->load->model('admin/nyheder_model');
$billed_sti = $this->nyheder_model->uploadImg();
$data = array(
'overskrift' => $this->input->post('overskrift'),
'description' => $this->input->post('description'),
'indhold' => $this->input->post('indhold'),
'billed_sti' => $billed_sti,
'brugernavn' => $this->session->userdata('username'),
'godkendt' => 'ja'
);
$this->db->insert('nyheder', $data);
redirect('admin/nyheder/index');
} else {
$this->index();
}
}
I do the image processing in the controller rather than the model.
"Models are PHP classes that are designed to work with information in your database."
from: http://codeigniter.com/user_guide/general/models.html
What you need to do is move the code for uploading the image to the controler.
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);
}
}
Once you did that,
You can insert the name of the file from the $data variable created in this line:
$data = array('upload_data' => $this->upload->data());
and you can get the value like this:
$data['file_name']
The file will upload the the folder you configured, and you will insert the filename to the DB From the controller.
I hope it helps.
Please use the upload function in your controller as the model classes are used to handle the database information. Please check the code below
//Controller Class
function upload_image()
{
//Check for the submit
// Submit Name refers to the name attribute on the submit input tag.
// $filename refers to the name attribute of the file input tag.
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$submit = $this->input->post('submit');
if($submit == "Submit Name")
{
//Load the relevant classes and libraries
$this->load->library('upload');
$this->load->model('admin/nyheder_model','nmodel');
$filename = "image_file";
//Define the config array
$config = array();
$config['upload_path'] = $this->gallery_path;
$config['allowed_types'] = "jpg|gif|png";
$config['max_size'] = 0; //0 is for no limit
$this->upload->initalize($config);
if(!$this->upload->do_upload("$filename"))
{
echo $this->upload->display_errors();
}
else
{
$file_data = $this->upload->data();
$filename_1 = $file_data['file_name'];
$insert_array = array('filename'=>"$filename_1");
$this->nmodel->insert_data($insert_array);
} // end of the else statement
} // end of the isset statement
} // end of the outer conditional statement
Now you have the value of the filename in the $filename_1 variable which you can pass to the model class and can store the value in the database.
Thanks
J