This question already has an answer here:
Upload Photo Failed Codeigniter
(1 answer)
Closed 6 years ago.
This is my code, my photo can't be inserted into the database. I don't know where the problem is.
actually i want to make an online exam with the codeigniter. i want to upload the question with the pic. but when i tried to upload the pict, the code is not working.
but the question success inserted into the database. only the pict failed to upload
Controller :
function insert(){
$nama_asli = $_FILES['userfile']['name'];
$config ['file_name'] = $nama_asli;
$config ['upload_path'] = './images/';
$config ['allowed_types'] = 'gif|jpg|png|jpeg';
$config ['max_size'] = '2500';
$config ['max_width'] = '2600';
$config ['max_height'] = '2200';
$id_soal = '';
$soal = $_POST['soal'];
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$d = $_POST['d'];
$kunci = $_POST['kunci'];
$status = $_POST['status'];
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/home/create_admin', $error);
}
else{
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$data = array(
'id_soal' => $id_soal,
'soal' => $soal,
'a' => $a,
'b' => $b,
'c' => $c,
'd' => $d,
'kunci' => $kunci,
'status' => $status,
'foto' => $file_name,
);
$hasil = $this->soal_model->Simpan('soal', $data);
if($hasil>=1){
redirect('dashboard/index', $data);
}
}
}
Model :
class Soal_model extends Ci_Model {
public function Ambil($where= "") {
$data = $this->db->query('select * from soal '.$where);
return $data;
}
public function Simpan($tabel, $data){
$res = $this->db->insert($tabel, $data);
return $res;
}
View:
<form role="form" action="<?php echo base_url(); ?>dashboard/insert" method="POST" enctype="multipart/form-data">
<form class="form-horizontal" method="post" style = "margin : 10px;">
<div class = "row">
<div class = "col-sm-offset-3 col-sm-6">
<div class="form-group">
<label>Soal :</label>
<textarea type="text" class="form-control" name="soal" id="soal" required></textarea>
</div>
<div class="form-group">
<label>Jawaban A :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="a" id="a" required/>
</div>
<div class="form-group">
<label>Jawaban B :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="b" id="b" required/>
</div>
<div class="form-group">
<label>Jawaban C :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="c" id="c" required/>
</div>
<div class="form-group">
<label>Jawaban D :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="d" id="d" required/>
</div>
<div class="form-group">
<label>Kunci :</label>
<select name="kunci" id="kunci" class="form-control">
<option>Select</option>
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
</div>
<div class="form-group">
<label>Status :</label>
<select name="status" id="status" class="form-control">
<option value="">Select</option>
<option value="tampil">Tampil</option>
<option value="tidak">Tidak</option>
</select>
<div class="form-group">
<label>Photo :</label>
<input type="file" name="foto" id="foto" size="20"/>
</div>
<br>
<div class="form-group">
<button type="submit" class="btn btn-default">Simpan</button>
<button type="reset" class="btn btn-default">Hapus</button>
</div>
</div>
</div>
</form>
Database :
Anyone can help?
you need to add the do_upload method of upload::library
$nama_asli = $_FILES['userfile']['name'];
$config ['file_name'] = $nama_asli;
$config ['upload_path'] = './images/';
$config ['allowed_types'] = 'gif|jpg|png|jpeg';
$config ['max_size'] = '2500';
$this->load->library('upload', $config);
//you need to add this do_upload();
if ( ! $this->upload->do_upload('userfile')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else{
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$file_name = $this->upload->file_name;
.
.
.
...
}
Related
I am new to Codeigniter and new to the MVC webdesign, i have plant insert form i want to upload image and save image name into database i tried many codes but not works please help me to write upload function
plant insert form is below here
<?php echo validation_errors(); ?>
<?php echo form_open('AddPlant/InsertPlant')?>
<div class="form-group has-error">
<label for="name">Name <span class="require">*</span></label>
<input type="text" class="form-control" name="name" />
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea rows="5" class="form-control" name="description" >
</textarea>
</div>
<div class="form-group required">
<label for="exampleSelect1" class='control-label'>Job Type</label>
<select class="form-control" id="age" name="age">
<option value="1">Level 1</option>
<option value="2">Level 2</option>
<option value="3">Level 3</option>
</select>
</div>
<div class="form-group">
<p><span class="require">*</span> - required fields</p>
</div>
<div class="form-group">
<label for="description">Plant Image</label>
<input type="file" name="plantimg">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">
Create
</button>
<button class="btn btn-default">
Cancel
</button>
</div>
<?php echo form_close();?>
My Codeigniter model
<?php
class Model_plants extends CI_Model
{
function insertPlantData(){
$data =array(
'name'=> $this->input->post('name',TRUE),
'description'=> $this->input->post('description',TRUE),
'age'=> $this->input->post('age',TRUE),
);
return $this->db->insert('plants',$data);
}
}
Hope this will help you :
Note : make sure you have load database and upload library either in autoload or in controller
First your form should be like this :
<?php echo form_open_multipart('AddPlant/InsertPlant')?>
............
<?php echo form_close();?>
Your controller should be like this :
public function InsertPlant()
{
/*here make sure your path is correct*/
$config['upload_path'] = FCPATH .'assets/uploads/';
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('plantimg'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
}
else
{
//$data = array('upload_data' => $this->upload->data());
$file_name = $this->upload->data('file_name');
/*here assuming that your column name for image is image_name, change it not*/
$data =array(
'name'=> $this->input->post('name',TRUE),
'description'=> $this->input->post('description',TRUE),
'age'=> $this->input->post('age',TRUE),
'image_name'=> $file_name,
);
return $this->db->insert('plants',$data);
}
}
I have 2 websites in 1 htdocs directory. The structure's like this
The main directory it's structured like this(name:Quartiere):
Quartiere
_admin
application
assets
Second website directory it's _admin with the same structure(default CodeIgniter structure).
I'm trying to upload a file from _admin to the main directory assets folder, but for some reason, it's not working.I tried other answers from other post but nothing it helped.
What I tried:
Put 'userfile' in do_upload
Checked if the form it's multipart(it is)
What I checked:
If the path it's good(it is)
If the size of the photo it's smaller than 2048(it is)
_admin Controller:
public function add_product(){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Adauga Produs';
$data['categories'] = $this->category_model->get_categories();
$this->form_validation->set_rules('name','Nume','required');
$this->form_validation->set_rules('category','Categorie','required');
$this->form_validation->set_rules('ingredients','Ingrediente');
$this->form_validation->set_rules('price','Pret','required');
$this->form_validation->set_rules('grams','Gramaj','required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('products/adauga', $data);
$this->load->view('templates/footer');
}else{
//upload image
$config['upload_path'] = 'http://localhost/quartiere/assets/images';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('userfile')){
$errors = array('error' => $this->upload->display_errors());
$food_image = 'noimage.png';
}else{
$data = array('upload_data' => $this->upload->data());
$food_image = $_FILES['userfile']['name'];
}
$this->food_model->add_product($food_image);
redirect('produse/adauga');
}
}
_admin View:
<?php echo validation_errors();?>
<?php echo form_open_multipart('products/add_product');?>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h1 class="text-center">
<?php echo $title; ?>
</h1>
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Nume Produs" required/>
</div>
<div class="form-group">
<select class="form-control" name="category" required>
<?php foreach($categories as $category):?>
<option value="<?php echo $category['id'];?>"><?php echo $category['category_name'];?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<label>Bar:</label><br>
Da <input type="radio" name="bar" class="from-control" value="1"/>
Nu <input type="radio" name="bar" class="from-control" value="0"/>
</div>
<div class="form-group">
<input type="text" name="ingredients" class="form-control" placeholder="Ingrediente"/>
</div>
<div class="form-group">
<input type="number" name="grams" class="form-control" placeholder="Gramaj"/>
</div>
<div class="form-group">
<label>Poza:</label>
<input type="file" name="userfile" size="10"/>
</div>
<div class="form-group">
<input type="number" name="price" class="form-control" placeholder="Pret" required/>
</div>
<button type="submit" class="btn btn-success btn-block">Adauga Produs</button>
</div>
</div>
<?php form_close(); ?>
You can make the upload path to - '../assets/images' - or - $_SERVER['DOCUMENT_ROOT'].'/quartiere/assets/images'. Either way is fine.
i am using codeigniter mvc framework, i want to make a form that has a upload picture and also save inputted data in input types in one html <form> why is this my image cannot be uploaded and it displays error like this when i echo the error:
File cannot be uploadedarray(1) { ["error"]=> string(43) "
You did not select a file to upload.
" }
Here is my code in view :
<form id="frm_product" class="form-horizontal" method="POST" action="Admin_controls/addProduct">
<div style="position:relative;">
<a class='btn btn-primary' href='javascript:;'>
Choose Picture..
<input type="file" style='position:absolute;z-index:2;top:0;left:0;filter: alpha(opacity=0);-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";opacity:0;background-color:transparent;color:transparent;' name="file_source" size="40" onchange='$("#upload-file-info").html($(this).val());'>
</a>
<span class='label label-info' id="upload-file-info"></span>
</div>
<br>
<div class="form-group">
<label for="cat_name">Product Name: </label>
<input type="text" class="form-control" name="prod_name">
</div>
<div class="form-group">
<label for="cat_desc">Description: </label>
<input type="text" class="form-control" name="prod_desc">
</div>
<div class="form-group">
<label for="cat_desc">Price: </label>
<input type="text" class="form-control" name="prod_price">
</div>
<div class="form-group">
<label for="cat_desc">Category: </label>
<select class="form-control" name="prod_cat">
<?php foreach($data as $each){ ?>
<option value="<?php echo $each->Category_id; ?>"><?php echo $each->Category_name; ?></option>';
<?php } ?>
</select>
</div>
</form>
Here is my javascript to submit the form:
function submitProduct() {
document.getElementById("frm_product").submit();
}
In my controller here is my code:
public function addProduct(){
$config['upload_path'] = './assets/uploaded_images/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 1024 * 8;
$this->load->library('upload', $config);
if($this->upload->do_upload("file_source")) {
$upload_data = $this->upload->data();
$file_name = base_url().'assets/uploaded_images/'.$upload_data['file_name'];
$data = array (
'Product_name' => $this->input->post('prod_name'),
'Product_desc' => $this->input->post('prod_desc'),
'Product_price' => $this->input->post('prod_price'),
'Category_id' => $this->input->post('prod_cat'),
'Product_img' => $file_name
);
var_dump($data);
}
else {
echo "File cannot be uploaded";
$error = array('error' => $this->upload->display_errors()); var_dump($error);
}
}
Seems to me that you forgot to add enctype="multipart/form-data" to your form
<form id="frm_product" class="form-horizontal" method="POST" action="Admin_controls/addProduct" enctype="multipart/form-data">
I made a user registration form on the CodeIgniter framework so users can registrate to my website. Now the only thing that doesn't work is that I cant upload a profile picture. When I click on the register button I'm getting 2 errors. I want the profile picture to be uploaded in the product_foto column.
This is my view file: (register.php) :
<form action="" method="POST" enctype='multipart/form-data'>
<div class="form-group">
<label for="voornaam" >Voornaam</label>
<input class="form-control" name="voornaam" id="voornaam" type="text">
</div>
<div class="form-group">
<label for="achternaam">Achternaam</label>
<input class="form-control" name="achternaam" id="achternaam" type="text">
</div>
<div class="form-group">
<label for="achternaam">Straat en huisnummer</label>
<input class="form-control" name="straat" id="straat" type="text">
</div>
<div class="form-group">
<input class="form-control" name="huisnummer" id="huisnummer" type="text">
</div>
<div class="form-group">
<label for="huisnummer">Huisnummer</label>
<input class="form-control" name="huisnummer" id="huisnummer">
</div>
<div class="form-group">
<label for="postcode" >Postcode</label>
<input class="form-control" name="postcode" id="postcode">
</div>
<div class="form-group">
<label for="woonplaats" >Woonplaats</label>
<input class="form-control" name="woonplaats" id="woonplaats">
</div>
<div class="form-group">
<label for="email" >Email adres</label>
<input class="form-control" name="email" id="email" type="emai">
</div>
<div class="form-group">
<label for="wachtwoord" >Wachtwoord</label>
<input class="form-control" name="wachtwoord" id="wachtwoord" type="password">
</div>
<div class="form-group">
<label for="wachtwoord">Herhaal wachtwoord</label>
<input class="form-control" name="wachtwoord2" id="wachtwoord" type="password">
</div>
<div class="form-group">
<label for="profiel_foto">Profiel foto</label>
<input class="form-control" type="file" name="profiel_foto" id="profiel_foto">
</div>
<div class="form-group">
<label for="beschrijving">Beschrijving</label>
<input class="form-control" name="beschrijving" id="beschrijving">
</div>
<div class="form-group">
<label for="geboortedatum" >Geboortedatum</label>
<input class="form-control" name="geboortedatum" id="geboortedatum" type="date">
</div>
<div class="form-group">
<label for="geslacht" >Geslacht</label>
<select class="form-control" id="geslacht" name="geslacht">
<option value="Man">Man</option>
<option value="Vrouw">Vrouw</option>
</select>
</div>
<div>
<button class="btn btn-primary" name="register" >Registreren</button>
</div>
</form>
This is the register code in the controller:
public function register()
{
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->input->post('profiel_foto');
$data_upload_files = $this->upload->data();
$image = $data_upload_files['./upload/'];
//voeg gebruiker toe aan database
$data = array (
'voornaam'=>$_POST['voornaam'],
'achternaam'=>$_POST['achternaam'],
'email'=>$_POST['email'],
'wachtwoord'=> ($_POST['wachtwoord']),
'startdatum'=>date('Y-m-d'),
'postcode'=>$_POST['postcode'],
'huisnummer'=>$_POST['huisnummer'],
'woonplaats'=>$_POST['woonplaats'],
'beschrijving'=>$_POST['beschrijving'],
'geboortedatum'=>$_POST['geboortedatum'],
'geslacht'=>$_POST['geslacht'],
'profiel_foto'=>$image
);
$this->db->insert('users',$data);
$this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
redirect("auth/register", "refresh");
}
}
And these are the 2 errors I'm getting when I'm trying to registrate:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: ./upload/
Filename: controllers/Auth.php
Line Number: 131
Backtrace:
File: /home/ubuntu/workspace/application/controllers/Auth.php
Line: 131
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
A Database Error Occurred
Error Number: 1048
Column 'profiel_foto' cannot be null
INSERT INTO `users` (`voornaam`, `achternaam`, `email`, `wachtwoord`, `startdatum`, `postcode`, `huisnummer`, `woonplaats`, `beschrijving`, `geboortedatum`, `geslacht`, `profiel_foto`) VALUES ('hallo', 'hallo', 'hallo#gmail.com', 'hallo', '2017-06-28', 'hallo', 'hallo', 'hallo', 'hallo', '2017-06-10', 'Man', NULL)
Filename: controllers/Auth.php
Line Number: 149
I modified your code. Try this
public function register() {
$data = array();
$config = array(
'upload_path' => 'upload',
'allowed_types' => 'gif|jpg|png|jpeg',
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('profiel_foto')) {
$error = array('error' => $this->upload->display_errors());
// var_dump( $error); die; check errors
} else {
$fileName = $this->upload->data();
$data['profiel_foto'] = $fileName['file_name'];
}
// voeg gebruiker toe aan database
$data = array (
'voornaam'=>$_POST['voornaam'],
'achternaam'=>$_POST['achternaam'],
'email'=>$_POST['email'],
'wachtwoord'=> ($_POST['wachtwoord']),
'startdatum'=>date('Y-m-d'),
'postcode'=>$_POST['postcode'],
'huisnummer'=>$_POST['huisnummer'],
'woonplaats'=>$_POST['woonplaats'],
'beschrijving'=>$_POST['beschrijving'],
'geboortedatum'=>$_POST['geboortedatum'],
'geslacht'=>$_POST['geslacht'],
);
$this->db->insert('users', $data);
$this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
redirect("auth/register", "refresh");
}
replace
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->input->post('profiel_foto');
$data_upload_files = $this->upload->data();
$image = $data_upload_files['./upload/'];
With
$target_dir = "upload/";
$target_file = $target_dir . time().basename($_FILES["profiel_foto"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$imgName = time().basename($_FILES["profiel_foto"]["name"]);
move_uploaded_file($_FILES["profiel_foto"]["tmp_name"], $target_file);
Your insert function
$data = array (
'voornaam'=>$_POST['voornaam'],
'achternaam'=>$_POST['achternaam'],
'email'=>$_POST['email'],
'wachtwoord'=> ($_POST['wachtwoord']),
'startdatum'=>date('Y-m-d'),
'postcode'=>$_POST['postcode'],
'huisnummer'=>$_POST['huisnummer'],
'woonplaats'=>$_POST['woonplaats'],
'beschrijving'=>$_POST['beschrijving'],
'geboortedatum'=>$_POST['geboortedatum'],
'geslacht'=>$_POST['geslacht'],
'profiel_foto'=>$imgName
);
$this->db->insert('users',$data);
This is my code, my photo can't be inserted into the database.
Actually I want to make an online exam with the codeigniter. I want to upload the question with the pic. but when I tried to upload the pict, the code is not working.
but the question success inserted into the database. only the pict failed to upload
Controller:
function insert(){
$nama_asli = $_FILES['userfile']['name'];
$config ['file_name'] = $nama_asli;
$config ['upload_path'] = './images';
$config ['allowed_types'] = 'gif|jpg|png|jpeg';
$config ['max_size'] = '2500';
$this->load->library('upload', $config);
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$id_soal = '';
$soal = $_POST['soal'];
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$d = $_POST['d'];
$kunci = $_POST['kunci'];
$status = $_POST['status'];
$data = array(
'id_soal' => $id_soal,
'soal' => $soal,
'a' => $a,
'b' => $b,
'c' => $c,
'd' => $d,
'kunci' => $kunci,
'status' => $status,
'foto' => $file_name,
);
$hasil = $this->soal_model->Simpan('soal', $data);
if($hasil>=1){
redirect('dashboard/index', $data);
}
}
Model:
class Soal_model extends Ci_Model {
public function Ambil($where= "") {
$data = $this->db->query('select * from soal '.$where);
return $data;
}
public function Simpan($tabel, $data){
$res = $this->db->insert($tabel, $data);
return $res;
}
View:
<form role="form" action="<?php echo base_url(); ?>dashboard/insert" method="POST" enctype="multipart/form-data">
<form class="form-horizontal" method="post" style = "margin : 10px;">
<div class = "row">
<div class = "col-sm-offset-3 col-sm-6">
<div class="form-group">
<label>Soal :</label>
<textarea type="text" class="form-control" name="soal" id="soal" required></textarea>
</div>
<div class="form-group">
<label>Jawaban A :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="a" id="a" required/>
</div>
<div class="form-group">
<label>Jawaban B :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="b" id="b" required/>
</div>
<div class="form-group">
<label>Jawaban C :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="c" id="c" required/>
</div>
<div class="form-group">
<label>Jawaban D :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="d" id="d" required/>
</div>
<div class="form-group">
<label>Kunci :</label>
<select name="kunci" id="kunci" class="form-control">
<option>Select</option>
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
</div>
<div class="form-group">
<label>Status :</label>
<select name="status" id="status" class="form-control">
<option value="">Select</option>
<option value="tampil">Tampil</option>
<option value="tidak">Tidak</option>
</select>
<div class="form-group">
<label>Photo :</label>
<input type="file" name="foto" id="foto" size="20"/>
</div>
<br>
<div class="form-group">
<button type="submit" class="btn btn-default">Simpan</button>
<button type="reset" class="btn btn-default">Hapus</button>
</div>
</div>
</div>
</form>
Database:
Kindly use this code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('soal_model');
}
function insert()
{
$config =array(
'upload_path' => './images',
'allowed_types' => 'gif|jpg|png|jpeg',
'max_size' => '2500',
);
$this->load->library('upload', $config);
$this->upload->do_upload('file_upload');
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$data = array(
'foto' => $file_name,
);
$hasil = $this->soal_model->Simpan('soal', $data);
if($hasil>=1){
redirect('dashboard/index', $data);
}
}
?>