I am new to CI and I am learning it through tutorials on Youtube. I am trying to upload image with what I found on tutorial here but unfortunately its not working. Below are the codes please help me.
Controller
public function create() {
$data['title'] = 'Create Post';
$data['desc'] = 'Feel free to create a new post here.';
$data['categories'] = $this->post_model->get_categories();
$this->form_validation->set_rules('title', 'Post Title', 'required');
$this->form_validation->set_rules('desc', 'Post Description', 'required');
if($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 500;
$config['max_height'] = 500;
$this->upload->initialize($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->post_model->create_post($post_image);
return redirect('posts');
}
}
Model
public function create_post($post_image) {
$slug = url_title($this->input->post('title'));
$data = array(
'post_title' => $this->input->post('title'),
'post_slug' => $slug,
'post_desc' => $this->input->post('desc'),
'post_cat_id' => $this->input->post('catid'),
'post_image' => $post_image
);
return $this->db->insert('posts', $data);
}
View
<?php echo form_open_multipart('posts/create'); ?>
<div class="form-group">
<label>Post Title</label>
<?php echo form_input(['name'=>'title', 'placeholder'=>'Add Title', 'class'=>'form-control', 'value'=>set_value('title')]); ?>
</div>
<div class="form-group">
<label>Select Category</label>
<select name="catid" class="form-control">
<?php foreach($categories as $category): ?>
<option value="<?php echo $category['cat_id']; ?>"><?php echo $category['cat_name']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label>Post Description</label>
<?php echo form_textarea(['name'=>'desc', 'placeholder'=>'Add Description', 'class'=>'form-control', 'value'=>set_value('desc')]); ?>
</div>
<div class="form-group">
<label>Upload Image</label>
<input type="file" name="userfile" size="20">
</div>
<input type="submit" class="btn btn-primary" value="Add Post">
Now whenever I try to create a post with an image it executes if(!$this->upload->do_upload()){ as TRUE and thus inserts noimage.jpg in database as well as image is not uploaded too. Any help would be appreciated. Thanks.
EDIT
On trying to DEBUG it using print_r($this->upload->data()); die(); I got an empty list of array which means the file is not getting submitted from the form itself on the first place. But why? I don't find any error in the form.
Array
(
[file_name] =>
[file_type] =>
[file_path] => ./assets/images/posts/
[full_path] => ./assets/images/posts/
[raw_name] =>
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
and print_r($errors) give this error. I am close to solve now I guess.
Array
(
[error] =>
The image you are attempting to upload doesn't fit into the allowed dimensions.
)
SOLUTION
I had to change the max_width and max_height to 0 for unlimited. The image was not being uploaded due to this error.
If you think your code is correct. Have you checked folder permissions?
You have to give permission to folders in order to store images
Forget to load upload library do like this :
$this->load->library('upload', $config);
The whole code (file uploading part) should be like this :
else {
// Upload Image
$config['upload_path'] = FCPATH.'assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 500;
$config['max_height'] = 500;
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
print_r($this->upload->data());die;
$file_name = $this->upload->data('file_name');
$post_image = $file_name;
}
else
{
$errors = array('error' => $this->upload->display_errors());
print_r($errors);die;
$post_image = 'noimage.jpg';
}
$this->post_model->create_post($post_image);
return redirect('posts');
}
try following changes
$config['upload_path'] = './assets/images/posts'; **to** $config['upload_path'] = getcwd().'/assets/images/posts';
You must remove library upload from autoloading and edit code like this.
if($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 500;
$config['max_height'] = 500;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if(!$this->upload->do_upload('userfile')){
$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->post_model->create_post($post_image);
return redirect('posts');
}
try this code!
public function create() {
$data['title'] = 'Create Post';
$data['desc'] = 'Feel free to create a new post here.';
$data['categories'] = $this->post_model->get_categories();
$this->form_validation->set_rules('title', 'Post Title', 'required');
$this->form_validation->set_rules('desc', 'Post Description', 'required');
if($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 500;
$config['max_height'] = 500;
$this->load->library('upload');
$this->upload->initialize($config);
if(!$this->upload->do_upload('userfile'))
{
$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->post_model->create_post($post_image);
return redirect('posts');
}
Related
I have a problem with multiple files upload with different area name and want to change for every area filename before it's upload.
This is HTML form.
<input type="file" placeholder="" name="profilPic"/>
<input type="file" placeholder="" name="topPic"/>
This is controller
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
//$config['file_name'] = $this->session->sersession["id"];
$this->load->library('upload', $config);
$profilPic = $this->upload->do_upload('profilPic');
if (!$profilPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "profil pic was not uploaded= ");
}else{
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", "profil picture was uploaded.");
}
$topPic = $this->upload->do_upload('topPic');
if (!$topPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "top pic was not uploaded" );
}else{
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", "this picture was uploaded.");
}
Note: The pictures are upload to directory. But i want to rename every file file name before uploaded like "userID_profil.jpg" and "userID_top.jpg"
You can set the $config['file_name'] before the second file upload using
$this->upload->initialize($config);
Of course, you also need to set it for the first file either with $this->load->library('upload', $config) or $this->upload->initialize($config).
Docs: https://www.codeigniter.com/userguide3/libraries/file_uploading.html#setting-preferences
I solved it.
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
if($_FILES["profilPic"]["name"]){
$config["file_name"] = $this->session->usersession["id"]."_profil.jpg";
$this->load->library('upload', $config);
$profilPic = $this->upload->do_upload('profilPic');
if (!$profilPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", ".");
}else{
$profilPic = $this->upload->data("file_name");
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", ".");
}
}
if($_FILES["topPic"]["name"]){
$config["file_name"] = $this->session->usersession["id"]."_top.jpg";
if($_FILES["profilPic"]["name"]){
$this->upload->initialize($config);
}else{
$this->loadl->library('upload', $config);
}
$topPic = $this->upload->do_upload('topPic');
if (!$topPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "" );
}else{
$topPic = $this->upload->data("file_name");
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", ".");
}
}
$config['upload_path'] = './assets/images/gambar_paket/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 900;
$config['file_name'] = $file;
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = array('nama_paket' => $nama,
'deskripsi' => $deskripsi,
'harga' => $harga,
'jenis' => $jenis,
'gambar' => $file
);
$this->mod_main->createData($data,'paket');
redirect('con_main/packet','refresh');
that's my controller for doing upload, but the file doesn't upload to the upload path. Please anyone help me
$config['upload_path'] = './assets/images/gambar_paket/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 900;
$config['file_name'] = $file;
$this->load->library('upload', $config);
$this->upload->do_upload();
if(!$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
echo <div class="alert alert-danger">'.$error['error'].'</div>';
}else{
$data = array('nama_paket' => $nama,
'deskripsi' => $deskripsi,
'harga' => $harga,
'jenis' => $jenis,
'gambar' => $file
);
$this->mod_main->createData($data,'paket');
redirect('con_main/packet','refresh');
}
1:-Use the error message it will show you error
2:-Also check wheather your form has enctype='multipart/form-data'
3:-check file name and use userfile ->optional
4:-before posting data print $_FILES['userfile'] so to check if your data is missing in uplaod
5:-Also check in autoload file that is loading.Or load manually
The Error that your File is not uploading to the provided path is that you have given the relative path for the upload directory
$config['upload_path'] = './assets/images/gambar_paket/';
Hence the realative path is to be replaced with the FCPATH
Here are some of the codes that are to be used.
EXT: The PHP file extension
FCPATH: Path to the front controller (this file) (root of CI)
SELF: The name of THIS file (index.php)
BASEPATH: Path to the system folder
APPPATH: The path to the "application" folder
Hence you have to replace the two line below in your up-loader code:
Replace:
$config['upload_path'] = './assets/images/gambar_paket/';
$this->upload->do_upload();
With:
$config['upload_path'] = FCPATH ."assets/fileupload/";
$this->upload->do_upload('userimage'); // Where userimage is the name of the file uplaoder input type name
HTML will look like this:
<input type="file" name="userimage"/>
And the Entire upload function will look like as follows.
$config['upload_path'] = FCPATH ."assets/images/gambar_paket/";
$config['allowed_types'] = 'gif|jpg|png';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 900;
$config['file_name'] = $file;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userimage')) {// Here you can handle the Failure Upload}
else
{ $data = $this->upload->data(// Here you can handle the operations after the image is uploaded);}
Here is the sample form that you need to upload the image from the HTML Syntax:
<?php
echo form_open_multipart('employee/addemployee', array('name' => 'addemployee', 'class'=>'form-horizontal'));
?>
<div class="form-group">
<label class="control-label col-sm-4" for="pwd">Profile:</label>
<div class="col-sm-8">
<input type="file" class="" id="profile" name="userimage">
</div>
</div>
<?php
echo form_close();
?>
Note: It will redirect to employee/addemployee which is the Employee Controller and search for function called addemployee and there you have the code to upload the image and then save it using the model.
I hope so this explanation will be clear to understand the Error that you get and to rectify it in further projects that you make on.
Happy Coding:)
Example Model
public function InsertBerita(){
// Direktori File "folder-CI->berita"
$config['upload_path'] = './berita/';
// Format Image
$config['allowed_types'] = 'jpg|png|jpeg';
$config['encrypt_name'] = TRUE;
// Load Libary Uploud
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$cekUser = $this->db->get_where('berita', array('judul_berita' => $this->input->post('judul_berita')));
unlink("berita/".$cekUser->first_row()->cover_berita);
$data['upload_data'] = $this->upload->data();
$this->resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
$file_gambar = $data['upload_data']['file_name'];
$insert = $this->db->insert('berita', array(
'cover_berita' => $file_gambar,
'ringkasan_berita' => $this->input->post('ringkasan_berita'),
'judul_berita' => $this->input->post('judul_berita'),
'isi_berita' => $this->input->post('isi_berita'),
'tanggal_berita' => date('Y-m-d H:i:s'),
'id_admin' => '1',
));
sleep(2);
redirect(base_url('databerita?insertsuccess'));
}else{
redirect(base_url('insertberita?failed'));
}
}
// image manipulasi merisize
public function resize($path,$file){
$config['image_library']='GD2';
$config['source_image'] = $path;
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = FALSE;
// size image
$config['width'] = 1158;
$config['height'] = 550;
// kualitas diturunkan 20%
$config['quality'] = 20;
$config["image_sizes"]["square"] = array(1158, 550);
$this->load->library('image_lib', $config);
$this->image_lib->fit();
}
enter code here
Gunakan Libary Uploud Jangan Lupa
$config['upload_path'] = './assets/images/gambar_paket/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 900;
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = array('nama_paket' => $nama,
'deskripsi' => $deskripsi,
'harga' => $harga,
'jenis' => $jenis,
'gambar' => $config['upload_path'] . $this->upload->data('file_name')
);
$this->mod_main->createData($data,'paket');
redirect('con_main/packet','refresh');
Semoga bisa membantu. Jangan lupa dibuat dahulu folder assets/images/gambar_paket
I am new to CI. Currently I have the following:
$config['upload_path'] = './uploads/';
I just want to know how to update path in codeignitor. I have tried the below code. Is there something I'm doing wrong?
<?php
defined('BASEPATH')`enter code here` OR exit('No direct script access allowed');
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
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);
}
}
}
?>
From Your code Your file will be uploaded at uploads folder which is in your root directory. $config['upload_path'] = './uploads/'; this is the place where your uploaded files are stored.
You make an directory uploads there where an application folder is .
If you want your current code.
You'll need a destination folder for your uploaded images. Create a folder at the root of your CodeIgniter installation called uploads and set its file permissions to 777.
By default the upload routine expects the file to come from a form field called userfile.
from file uploading class
Use this library to upload... Its easy to use
http://demo.codesamplez.com/codeigniter/file-upload-demo
View
<form action="" method="POST" enctype="multipart/form-data" >
Select File To Upload:<br />
<input type="file" name="userfile" multiple="multiple" />
<input type="submit" name="submit" value="Upload" class="btn btn-success" />
</form>
{if isset($uploaded_file)}
{foreach from=$uploaded_file key=name item=value}
{$name} : {$value}
<br />
{/foreach}
{/if}
Controller
/**
* the demo for file upload tutorial on codesamplez.com
* #return view
*/
public function file_upload_demo()
{
try
{
if($this->input->post("submit")){
$this->load->library("app/uploader");
$this->uploader->do_upload();
}
return $this->view();
}
catch(Exception $err)
{
log_message("error",$err->getMessage());
return show_error($err->getMessage());
}
}
Component
/**
* Description of uploader
*
* #author Rana
*/
class Uploader {
var $config;
public function __construct() {
$this->ci =& get_instance();
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/files/",
'upload_url' => base_url()."files/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
}
public function do_upload(){
$this->remove_dir($this->config["upload_path"], false);
$this->ci->load->library('upload', $this->config);
if($this->ci->upload->do_upload())
{
$this->ci->data['status']->message = "File Uploaded Successfully";
$this->ci->data['status']->success = TRUE;
$this->ci->data["uploaded_file"] = $this->ci->upload->data();
}
else
{
$this->ci->data['status']->message = $this->ci->upload->display_errors();
$this->ci->data['status']->success = FALSE;
}
}
function remove_dir($dir, $DeleteMe) {
if(!$dh = #opendir($dir)) return;
while (false !== ($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!#unlink($dir.'/'.$obj)) $this->remove_dir($dir.'/'.$obj, true);
}
closedir($dh);
if ($DeleteMe){
#rmdir($dir);
}
}
}
$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('image name'))
{
$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);
}
public function add_software()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error-form">',
'</div>');
$this->form_validation->set_rules('name', 'Name',
'required|min_length[4]|max_length[25]');
$this->form_validation->set_rules('desc', 'Description', 'required');
$this->form_validation->set_rules('licence', 'Licence.', 'required');
$this->form_validation->set_rules('platform', 'Platform', 'required');
$this->form_validation->set_rules('developers', 'Developer',
'required');
$this->form_validation->set_rules('link', 'Developer Website',
'required');
$this->form_validation->set_rules('cat', 'Category', 'required');
$this->form_validation->set_rules('logo', 'Software Logo', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('admin/includes/header');
$this->load->view('admin/add_software');
$this->load->view('admin/includes/footer');
} else {
//Setting values for tabel columns
$data['name'] = $this->input->post('name');
$data['description'] = $this->input->post('desc');
$data['licence'] = $this->input->post('licence');
$data['platform'] = $this->input->post('platform');
$data['developers'] = $this->input->post('developers');
$data['link'] = $this->input->post('link');
$data['category'] = $this->input->post('cat');
$data['logo'] = $this->input->post('logo');
/////////////////////////////////
//set preferences
$config = array(
'upload_path' => '' .base_url(). '/assets/img/logo/',
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "4096000",
'max_height' => "1024",
'max_width' => "1024"
);
//load upload class library
$this->load->library('upload', $config);
$this->upload->data();
$this->upload->do_upload();
/////////////////////////////////
//Transfering data to Model
$this->insert_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
redirect('admin/view_softwares', 'refresh');
}
}
This is my codeigniter controller function. Using this function, I can add data to the database. But, I am unable to upload image. Even upload_files is enabled in php.ini. I tried resolving this problem all day long, but all in vain.
I have not used enctype in my html form because when I use enctype, I get the below error:
undefined Index filename
Friend i wil give code that i have done for uploading image.This surely work.
You can refer this.Any doubt,please ask,i wil help you
public function uploadImage() {
$this->load->helper(array('form', 'url'));
$config['upload_path'] = 'assets/images/b2bcategory';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['width'] = 75;
$config['height'] = 50;
if (isset($_FILES['catimage']['name'])) {
$filename = "-" . $_FILES['catimage']['name'];
$config['file_name'] = substr(md5(time()), 0, 28) . $filename;
}
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$field_name = "catimage";
$this->load->library('upload', $config);
if ($this->input->post('selsub')) {
if (!$this->upload->do_upload('catimage')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], $dat['upload_data']['file_name']);
}
$ip = $_SERVER['REMOTE_ADDR'];
if (empty($dat['upload_data']['file_name'])) {
$catimage = '';
} else {
$catimage = $dat['upload_data']['file_name'];
}
$data = array(
'ctg_image' => $catimage,
'ctg_dated' => time()
);
$this->b2bcategory_model->form_insert($data);
}
}
Your path is wrong.
'upload_path' => '' .base_url(). '/assets/img/logo/',
Note: Make sure your upload path "assets folder" is in the main directory out side of
application folder. You also may need to set folder permissions.
Try
'upload_path' => FCPATH . 'assets/img/logo/'
or just
'upload_path' => './assets/img/logo/'
To access file info after upload success preferences
$image_info = $this->upload->data();
echo $image_info['file_name'];
Or Through Model
$this->insert_model->form_insert($data, $image_info = array());
I am trying to implement a post functionality and want to pick message and image from a php view. My message functionality is working good. But on image upload i receive an error You did not select a file to upload. This is my controller function
function post_func()
{
session_start();
echo $post_message=$_POST['post'];
echo $share_with=$_POST['share_with'];
echo $image=$_POST['image'];
if($image==null){
echo "<br/>no image<br/>";
}
else{
////////////////////////////////////////////////////////////////////////////////////////
$config['upload_path'] = './application/css';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo "<br/>";
echo $this->upload->display_errors();
echo "<br/> image error<br/>";
}
else
{
echo "<br/> reached <br/>";
session_start();
$this->membership_model->insert_images($this->upload->data(),$email);
$data = array('upload_data' => $this->upload->data());
echo "<br/ problem<br/>";
}
////////////////////////////////////////////////////////////////////////////////////////
}
$public;
if($share_with=="public"){
echo "1";
$public=true;
}else{
echo "0";
$public=false;
}echo "-----------------------------<br/>";
echo $user=$this->session->userdata('user_identification');
$data = array
(
'userid'=> $user,
'public' => $public,
'message' => $post_message,
'picname' => "None"
);
$this->load->model('membership_model');
$this->membership_model->add_message($data);
echo "</br>";
echo $user=$this->session->userdata('user_identification');
}
This is my view.
<?php echo form_open('search/post_func');?>
<!--<form id="loginForm" action="../search/post_func" method="post" enctype="multipart/form-data" >-->
<div id="your_post">
<div id="post_image">
<img id ="post_img" src="<?php echo $this->config->item('base_url'); ?><?php echo '/application/css/'. $img ?>"/>
</div>
<textarea name="post" rows="5" cols="30" placeholder="Share an update..." id="post_text" rows="2" value=""></textarea>
//other view items
<?php
echo form_close();
?>
Please help me
Change:
$this->upload->do_upload()
To this:
$this->upload->do_upload('my_file_input_name')
and it will work!! :)
Change:
<?php echo form_open_multipart('search/post_func');?>
Your form type should be "multipart"
Change your form tag to:
<?php echo form_open_multipart('search/post_func');?>
enctype='multipart/form-data'
you should put previous attribute in form tag
$this->upload->do_upload('file_name')
pass input tag file name to pervious line in your controller
<input type="file" name ="file_name" >
example code is
$config['upload_path'] = FCPATH.'upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
if ( ! $this->upload-> do_upload('userfile'))
{
$errors = $this->upload->display_errors();
$this->session->set_flashdata('error', $errors);
redirect('ur_link');
}
else
{
$data = array('upload_data' => $this->upload->data());
$fullpath= $data['upload_data']['full_path'];
$file_name = $data['upload_data']['file_name'];
}
}
Do this:
if(isset($_FILES['profile_pic']['name']) && !empty($_FILES['profile_pic']['name'])){
$config['upload_path']= './assets/img/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$config['max_width'] = 10000;
$config['max_height'] = 10000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('profile_pic'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
exit;
}
else
{
$data = array('upload_data' => $this->upload->data());
if(!empty($data)){
$img1 = $data['upload_data']['file_name'];
}
}
}
$insert_data['profile_pic'] = $img1;
Another reason I find this problem to be occurred. if you forget to initialize after load your library:
$this->load->library('upload', $config);
$this->upload->initialize($config); //Make this line must be here.
I hope this might help you.
So note I was doing a mysql upload import and I got the "You did not select a file to upload" message. It turns out my import sql file had extra columns which caused the fail and returned this error message.