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;
Related
I made a file upload in codeigniter, but in the controller I set $ directory = './assets/images/'; unreadable on vmware.
please help, here is my controller
public function tambahfotoproses()
{
$variable = 'foto'; //variable name dari form
$directory = './assets/images/'; //direktori
$allowed_file = 'gif|jpg|jpeg|png|JPG|GIF|PNG|JPEG'; //file yang diizinkan dibatasi dengan tanda
$upload['detail'] = $this->m_admin->uploadfile($variable,$directory,$allowed_file); //proses dengan modul insertfile
var_dump($upload); die();
/* to upload file */
$filename = $upload['detail']['file_name'];
if ($filename != "")
{
$data = array(
'foto' => $filename,
'person_nm' => $this->input->post('person_nm'),
'pengupload' => $this->input->post('pengupload'),
'tgl' => $this->input->post('tgl')
);
$insert = $this->db->insert('eregister_foto',$data);
} else {
$data = array(
'foto' => $filename,
'person_nm' => $this->input->post('person_nm'),
'pengupload' => $this->input->post('pengupload'),
'tgl' => $this->input->post('tgl')
);
$insert = $this->db->insert('eregister_foto',$data);
}
redirect('index.php/admin/foto_dr');
}
I deliberately gave var_dump () to find out the results of $ upload
the result is array (1) {["detail"] => int (0)}
photo not uploaded
and here is my modals.php
/AKSI UPLOAD FILE/
function uploadfile($var,$dir,$all){
$new_name = $this->input->post('person_nm');
$config2=array(
'image_library' => 'gd2',
'upload_path' => $dir."/dokter/", //lokasi gambar akan di simpan
'allowed_types' => $all, //ekstensi gambar yang boleh di unggah
'create_thumb' => TRUE,
'max_size' => '2048', //batas maksimal ukuran gambar
'file_name' => $new_name
);
$this->load->library('upload');
$this->upload->initialize($config2);
if ($this->upload->do_upload($var))
{
return $this->upload->data();
}
else
{
return 0;
}
}
i've try to rewrite folder "dokter" too, and still can't work
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();
}
}
Hi friends its my first question on here i hope will make you understand what I want because I am new on codeigniter and also not good in english :p
So my problems is I am trying to upload image using Codeigniter I have two folder
1.(for full image)
2.(for thumnils image)
but when i upload its gives me error message
Column 'img_path' cannot be null
INSERT INTO `gallery` (`img_path`, `img_name`) VALUES (NULL, 'asdasd')
here is my controller model and view
Controller:
`
<?php
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', $data);
}
}
`
Model
`
<?php
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',
'width'=>150,
'height'=>100,
'maintain_ratio'=>true
);
$new_image_insert_data = array(
'img_path' => $this->input->post('userfile'),
'img_name' => $this->input->post('img_name')
);
$insert = $this->db->insert('gallery', $new_image_insert_data);
return $insert;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
redirect('gallery');
}
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;
}
}
View
<?php $this->load->view('includes/header'); ?>
<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 Image</div>
<?php endif; ?>
</div>
<div id="page_content">
<div id="page_content_inner upload">
<?php echo form_open_multipart('gallery'); ?>
<div class="md-card">
<div class="md-card-content">
<div class="uk-grid" data-uk-grid-margin>
<div class="uk-width-medium-1-2">
<div class="uk-form-row">
<label for="fullname">Select Image<span class="req"></span></label><br>
<input type="file" id="userfle" name="userfle" required class="md-input" />
</div>
<div class="uk-form-row">
<label for="fullname">Image Title<span class="req"></span></label>
<input type="text" id="img_name" name="img_name" required class="md-input" />
</div>
<div class="uk-form-row">
<input type="submit" name="upload" required class="md-input" />
</div>
</div>
</div>
</div>
</div>
<? echo form_close();?>
</div>
</div>
<?php $this->load->view('includes/footer'); ?>
I try hard to solve this problem and also to make you guys understand whats my problem is ?
You are going wrong way
$new_image_insert_data = array(
'img_path' => $this->input->post('userfile'),
'img_name' => $this->input->post('img_name')
);
File input can't get like this.
After Upload file you will get all data of uploaded file
So you can store like this
$new_image_insert_data = array(
'img_path' => $image_data['file_path'],
'img_name' => $image_data['file_name']
);
See this link
In your form you have input field like
<input type="file" id="userfle" name="userfle" required class="md-input" />
change it to
<input type="file" id="userfle" name="userfile" required class="md-input" />
as in model you are using userfile Not userfle.
$new_image_insert_data = array(
'img_path' => $this->input->post('userfile'),
'img_name' => $this->input->post('img_name')
);
And img_path should be your folder path with
$new_image_insert_data = array(
'img_path' => $this->gallery_path_url,
'img_name' => $this->input->post('img_name') /** if you don't want to change uploaded image file name**/
);
Upload code should be moved to controller. Model is only for Database
this is wrong input
$new_image_insert_data = array(
'img_path' => $this->input->post('userfile'), # Wrong
'img_name' => $this->input->post('img_name')
);
should be add this $this->gallery_path
$new_image_insert_data = array(
'img_path' => $this->gallery_path,
'img_name' => $this->input->post('img_name')
);
or
$new_image_insert_data = array(
'img_path' => $image_data['file_path'],
'img_name' => $image_data['file_name']
);
So on db, record will be
img_path -->../images'
img_name --> xyz.jpg
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.!!
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