I am trying to upload the three images from html form,with three different names.But it shows error
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->input->post('mobile_name').'_front.jpg';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload('files'))
{
$_POST['mobile_img1']= $this->upload->data()['file_name'];
}
else
{
echo "image upload fails";
exit();
}
This my codeigniter code to upload one image.
I have three image input field in html code
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<label class="control-label">Mobile Image Front</label>
<input type="file" name="mobile_img1" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
<label class="control-label">Mobile Image Back</label>
<input type="file" name="mobile_img2" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
<label class="control-label">Mobile Image Side</label>
<input type="file" name="mobile_img3" class="filestyle" data-placeholder="No file">
</div>
</div>
</div>
in this html field i need to upload the first input as mobilename_front.jpg , second as mobilename_back.jpg and third as mobilename_side.jpg and also i need to upload this names in db.Give some ideas to implement this,
View Files
<div class="row">
<div class="col-md-4 col-md-offset-4">
<form method="POST" action="<?php echo site_url('my-controller/file_upload');?>" enctype='multipart/form-data'>
<div class="form-group">
<label class="control-label">Mobile Image Front</label>
<input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
<label class="control-label">Mobile Image Back</label>
<input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
<label class="control-label">Mobile Image Side</label>
<input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<input type="submit" value="upload"></form>
</div>
</div>
My controller:
<?php
class My_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('upload');
}
public function file_upload(){
$files = $_FILES;
$cpt = count($_FILES['mobile_img']['name']);
for($i=0; $i<$cpt; $i++)
{
$mobileView=(($i == 0)?"_front":(($i == 1)?"_back":"_side")); //Here i assign first image(i = 0) as _front, second image(i = 1) as _back,third image(i = 2) as _side
$_FILES['mobile_img']['name']= $files['mobile_img']['name'][$i].$mobileView.time();// time() for time in seconds becauze we may need if same name uploaded file name
$_FILES['mobile_img']['type']= $files['mobile_img']['type'][$i];
$_FILES['mobile_img']['tmp_name']= $files['mobile_img']['tmp_name'][$i];
$_FILES['mobile_img']['error']= $files['mobile_img']['error'][$i];
$_FILES['mobile_img']['size']= $files['mobile_img']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['mobile_img']['name'];
$images[] = $fileName;
}
$fileName = implode(',',$images);
$this->my_model->upload_image($fileName);
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = './upload/'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
}
?>
My model Function:
<?php
class My_Model extends CI_Model {
public function upload_image($fileName)
{
if($filename!='' ){
$filename1 = explode(',',$filename);
foreach($filename1 as $file){
$file_data = array(
'name' => $file,
'datetime' => date('Y-m-d h:i:s')
);
$this->db->insert('uploaded_files', $file_data);
}
}
}
}
?>
<?php echo form_open('admin/product_insert');?>
<div class="row form-group">
<div class="col-lg-2">
Image 1
</div>
<div class="col-lg-5">
<input type="file" class="form-control" name="photo1" required>
</div>
</div>
<div class="row form-group">
<div class="col-lg-2">
Image 2
</div>
<div class="col-lg-5">
<input type="file" class="form-control" name="photo2" Required>
</div>
</div>
<div class="row form-group">
<div class="col-lg-2">
Image 3
</div>
<div class="col-lg-5">
<input type="file" class="form-control" name="photo3" Required>
</div>
</div>
<input type="submit">
public function product_insert(){
$pro=$this->input->post('productName');
for($i=0;$i<=3;$i++){
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name']=$pro.$i;
$this->load->library('upload',$config);
$this->upload->do_upload('photo'.$i);
$this->upload->initialize($config);
}
Related
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.
This is my controller file that receives input from view file. post data will be submited to Add function and the function manipulates do upload function which recieves configuration parameters from set_configuration_function.
public function __construct()
{
parent::__construct();
$this->load->model('App/AppModel');//load app model
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('session');
}
public function index()
{
$this->data['blogs'] = $this->AppModel->get_all(BLOGS_TABLE);
parent::LOADER('App/Blog/All');
}
public function Add()
{
if ($_POST && empty($_FILES['blog_file']['name'])) {
$new_blog = $_POST;
$this->AppModel->insert(BLOGS_TABLE, $new_blog);
$this->session->set_flashdata('flashSuccess', 'Blog Created Successfully.');
$this->index();
}
if ($_POST && !empty($_FILES['blog_file']['name'])) {
$file_name =$this->do_upload();
$file_name = array($file_name);
$blog = $_POST;
$new_blog = array_merge($blog, $file_name);
$this->AppModel->insert(BLOGS_TABLE, $new_blog);
$this->session->set_flashdata('flashSuccess', 'Blog Created Successfully.');
$this->index();
}
parent::LOADER('App/Blog/New');
}
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);
}
}
public function set_upload_options (){
$config = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '10000000000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
return $config;
}
This is my View File
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header col-md-offset-1">
<h1>
<?php echo BLOG_HEADING; ?>
<small><?php echo ADD; ?></small>
</h1>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<!-- right column -->
<div class="col-md-8 col-md-offset-1">
<!-- general form elements disabled -->
<div class="box box-warning">
<div class="box-header with-border">
<h3 class="box-title"><?php echo ADD; ?> Blog</h3>
</div>
<!-- /.box-header -->
<?php echo form_open_multipart('App/Blog/Add');?>
<div class="box-body">
<!-- text input -->
<!-- input states -->
<div class="form-group has-success">
<label class="control-label" for="inputSuccess"> <?php echo BLOG_TITLE; ?> *</label>
<input type="text" name="blog_title" class="form-control" id="inputSuccess" placeholder="Enter Title" required>
</div>
<div class="form-group has-success">
<label class="control-label" for="inputSuccess"> <?php echo BLOG_AUTHOR; ?> *</label>
<input type="text" name="blog_author" class="form-control" id="inputSuccess" placeholder="Enter Author" required>
</div>
<div class="form-group has-success">
<label class="control-label" for="inputSuccess"> <?php echo BLOG_URL; ?></label>
<input type="text" name="blog_url" class="form-control" id="inputSuccess" placeholder="Add Link" required>
</div>
<!-- textarea -->
<div class="form-group has-success">
<label class="control-label" for="inputSuccess"><?php echo BLOG_CONTENT; ?> *</label>
<textarea name="blog_content" class="form-control" rows="3" placeholder="Enter Content" required></textarea>
</div>
<div class="form-group has-success">
<label for="exampleInputFile">Blog Image</label>
<input type="file" name="blog_file" id="exampleInputFile">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary"><?php echo SAVE; ?></button>
</div>
</form>
</div>
</div>
</section>
</div>
Problem is I get error that says "you did not select a file to upload."
you are not using set_upload_options() method.
you are missing input name blog_file in your controller.
$this->upload->do_upload('blog_file');
I'm trying to post two different types of files on two different fields into my database. I've been looking for the answer on the entire Stack Overvlow, but I still couldn't find it.
This is my controller:
public function insertLamaran()
{
$pas_foto = ($_FILES['pas_foto']['name']);
$cv = ($_FILES['cv']['name']);
if ($pas_foto !== "")
{
$config['upload_path'] = './uploads/';
$config['log_threshold'] = 1;
$config['allowed_types'] = 'jpg|png|jpeg|gif';
$config['max_size'] = '100000'; // 0 = no file size limit
$config['file_name']='smallImage.jpg';
$config['overwrite'] = false;
$this->load->library('upload', $config);
$this->upload->do_upload('pas_foto');
$upload_data = $this->upload->data();
$data['pas_foto'] = $upload_data['file_name'];
}
if ($cv !== "")
{
$config['file_name']='bigImage.pdf';
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '100000'; // 0 = no file size limit
$config['overwrite'] = false;
$this->load->library('upload', $config);
$this->upload->do_upload('filename1');
$upload_data = $this->upload->data();
$data['cv'] = $upload_data['file_name'];
}
$data['u_id'] = $this->input->post('u_id');
$this->M_upload->insertLamaran($data);
}
and this is my view:
<form action="<?php echo base_url('Upload/insertLamaran')?>" method="post">
<input hidden type="text" name="u_id" value="<?php echo ucfirst($this->session->userdata('u_id')); ?>"></input><br>
<div class="row wowload fadeInLeftBig">
<div class="col-sm-6 col-sm-offset-3 col-xs-12">
<label for="pasfoto">Pas Foto (.jpg)</label>
<input id="pas_foto" type="file" name="pas_foto" multiple="true">
<label for="CV">CV (.pdf)</label>
<input id="cv" type="file" name="cv" multiple="true">
<button class="btn btn-primary"><i class="fa fa-paper-plane"></i> Send</button>
</div>
</div>
</form
the errors said that
Undefined index: pas_foto. Filename: controllers/Upload.php Line Number: 22
Does anybody know what's happening, and how I can fix it?
as i seen you correct form type so answer for pdf file
you forgot to add pdf file name
change this
$this->upload->do_upload('filename1');
into this
$this->upload->do_upload('cv');
First, load url helper in your controller's constructor. I think your view file must be like this.Need to put enctype="multipart/form-data" for file uploading.
<form action="<?php echo base_url('upload/insertLamaran');?>" method="post" enctype='multipart/form-data'>
<input hidden type="text" name="u_id" value="<?php echo ucfirst($this->session->userdata('u_id')); ?>"></input><br>
<div class="row wowload fadeInLeftBig">
<div class="col-sm-6 col-sm-offset-3 col-xs-12">
<label for="pasfoto">Pas Foto (.jpg)</label>
<input id="pas_foto" type="file" name="pas_foto" multiple="true">
<label for="CV">CV (.pdf)</label>
<input id="cv" type="file" name="cv" multiple="true">
<button class="btn btn-primary" type="submit"><i class="fa fa-paper-plane"></i> Send</button>
</div>
</div>
</form>
I have a view where the user can edit his details.
View
<form class="form-horizontal" method ="post" action="<?php echo site_url('studentDashboardController/saveUserDetails');?>">
<?php echo form_open('studentDashboardController/saveUserDetails'); ?>
<?php echo $this->session->flashdata('msg'); ?>
<fieldset>
<!-- Form Name -->
<legend>User Details</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Full Name</label>
<div class="col-md-8">
<input id="name" name="name" type="text" class="form-control input-md" value="<?php foreach($details as $detail){?><?php echo $detail->name?><?php }?>">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="dob">Date of Birth</label>
<div class="col-md-8">
<input id="dob" name="dob" type="text" placeholder="" class="form-control input-md" value="">
</div>
</div>
...
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="userfile">Upload Profile Picture</label>
<div class="col-md-4">
<input id="userfile" name="userfile" class="input-file" type="file">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" name="submit" type="submit" class="btn btn-primary">Save Changes</button>
</div>
</div>
Controller
public function saveUserDetails()
{
if (isset($this->session->userdata['logged_in']))
{
$uid = ($this->session->userdata['logged_in']['userId']);
$data['notifyCount']= $this->notifications->getUnreadNotification($uid);
$data['title']= $this->notifications->getUnreadNotificationTitle($uid);
$data['notifyCount']= $this->notifications->getUnreadNotification($uid);
$data['users'] = $this->user->getUserNames();
$config['upload_path'] = './assets/img/taro/profilePictures/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['max_width'] = '5000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
$this->upload->do_upload();
$upload_data = $this->upload->data();
$msg = $this->studentprofileModel->saveUserDetails($uid,$this->input->post());
$msgText = $msg['msg'];
$msgType = $msg['type'];
//Loading View
if($msgType == "danger")
{
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">'.$msgText.'</div>');
}
else
{
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">'.$msgText.'</div>');
}
redirect(base_url('index.php/studentDashboardController/loadGeneralProfilePage'));
}
else
{
$this->load->view('login/loginView');
}
}
What I need to do is to first save the user details collected via the form by calling the studentprofileModel's saveUserDetails method. In this method I need to send the uploaded image's name so that I can store the image path in the db table's image field. And I need to upload the image to the assets folder's subdirectory taro as well.
I have done the following and the data from the form fields get updated in the DB. But the image file is not uploaded.
Any suggestions in this regard will be highly appreciated
You can add this code in your controller "function saveUserDetails()"
public function saveUserDetails()
{
if (isset($this->session->userdata['logged_in']))
{
$uid = ($this->session->userdata['logged_in']['userId']);
$post_data = $this->input->post();
if($_FILES['image']['name'])
{
//load upload library
$this->load->library('upload');
// Specify configuration for File
$config['upload_path'] = './assets/img/taro/profilePictures/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['max_width'] = '5000';
$config['max_height'] = '3000';
$this->upload->initialize($config);
if ($this->upload->do_upload('image')){
$image_data = $this->upload->data();
$post_data['image'] = $image_data['file_name'];
}
else{
$error = $this->upload->display_errors();
}
}
$msg = $this->studentprofileModel->saveUserDetails($uid,$post_data);
$msgText = $msg['msg'];
$msgType = $msg['type'];
.....
}
Don't forget give permision to image folder.
I hope it will work for you!!
My first guess, before further tests, would be to add the enctype="multipart/form-data" attribute to your <form> tag, as it specifies how the form-data should be encoded when submitting it to the server and the multipart/form-data is required when the form includes any <input type="file">.
You can read more about this here.
Update 2:
Actually, it seems you may have two form tags opened? one from <form> and another one from form_open() function CI provides?
If so, use only one of them.
If you choose to do in HTML:
<form class="form-horizontal" method ="post" action="<?php echo site_url('studentDashboardController/saveUserDetails');?>" enctype="multipart/form-data">
If you choose to use CI's functions, there's two ways: form_open and form_open_multipart (which is exactly like form_open but it adds automatically the multipart thing)
form_open('studentDashboardController/saveUserDetails', array('enctype' => "multipart/form-data"))
or
form_open_multipart('studentDashboardController/saveUserDetails')
Update 3:
Make sure that the folder where the image will be uploaded to have enough permissions;
Pass the absolute path in here $config['upload_path'] = './assets/img/taro/profilePictures/' instead of the relative one
Also, change this $this->upload->do_upload(); to $this->upload->do_upload('userfile');
Let us know the results :)
I am working on a project where users can upload files to the site that is stored in an Amazon S3 bucket.
This doesn't really work though.. The server sees the file and everything seems good, apart from the file only being 28 bytes big when it ends up on amazon..
This is my code so far:
controller
public function uploadFile()
{
$data['username'] = $this->input->cookie('username', TRUE);
$data['pagetitle'] = "Upload file";
$data['userid'] = $this->userid;
$this->load->template('uploadFile', $data);
}
public function takeUpload()
{
$config['upload_path'] = './images/screenshots/';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$projectFileLoc = $_FILES['projectFile']['tmp_name'];
$projectFileName = $_FILES['projectFile']['name'];
if (!($this->upload->do_upload("previewImage")) && !($this->s3->putObject($projectFileLoc, '3dnation', $projectFileName, $this->s3->ACL_PUBLIC_READ)))
{
echo "Something went wrong...";
}
else
{
$imgData = $this->upload->data();
$previewImage = $imgData['file_name'];
//TODO: Add image to DB
}
}
The view file (uploadFile.php)
<div class="row">
<div class="col-xs-12 col-sm-12">
<div class="jumbotron">
<h1>Upload files</h1>
</div>
</div><!--/span-->
</div><!--/row-->
<div class="row">
<div class "col-xs-12 col-sm-12">
<form action="/home/takeUpload" method="POST" role="form" class="form-horizontal" enctype="multipart/form-data">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title" placeholder="Title" name="title" required>
</div>
</div>
<div class="form-group">
<label for="description" class="col-sm-2 control-label">Description</label>
<div class="col-sm-10">
<input type="textfield" class="form-control" id="description" placeholder="Enter a description here" name="description" required>
</div>
</div>
<div class="form-group">
<label for="projectFile" class="col-sm-2 control-label">Project/Scene files (accepted formats: zip, rar, 3ds)</label>
<div class="col-sm-10">
<input type="file" class="form-control" id="projectFile" name="projectFile" required>
</div>
</div>
<div class="form-group">
<label for="previewImage" class="col-sm-2 control-label">Preview image</label>
<div class="col-sm-10">
<input type="file" class="form-control" id="previewImage" name="previewImage" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Upload</button>
</div>
</div>
</form>
</div>
</div>
I am building it with the latest Codeigniter with this S3 library
What is wrong?
You must call to putObject with an array, if you want use a file you must call too "putObjectFile" function:
Check the definition in the class:
public static function putObjectFile($file, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $contentType = null)
{
return self::putObject(self::inputFile($file), $bucket, $uri, $acl, $metaHeaders, $contentType);
}
A example from the original library:
// Simple PUT:
if (S3::putObject(S3::inputFile($file), $bucket, $uri, S3::ACL_PRIVATE)) {
echo "File uploaded.";
} else {
echo "Failed to upload file.";
}
As you can see in the doc