'You did not select a file to upload' codeigniter - php

I am trying to upload an image via codeigniter but constantly recieve the following message: 'You did not select a file to upload'.
I have viewed many SO answers on this but nothing has helped:
PHP config max File upload size: 2M (I tested with 0.5mb files)
Form is multipart
Website is on localhost
HTML:
<h3>Upload profile image</h3>
<div data-role="fieldcontainer">
<form action='do_upload_prof' method='post' enctype="multipart/form-data">
<input type="file" name='file'>
<input type='submit' value='upload' />
</form>
</div>
PHP Controller:
public function do_upload_prof() {
$config = array(
'upload_path' => "./profile_img/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "4048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if ($this->upload->do_upload('file')) {
$data = array(
'upload_data' => $this->upload->data()
);
$this->load->view('header');
$this->load->view('profile', $data);
$this->load->view('footer');
} else {
$error = array(
'error' => $this->upload->display_errors()
);
$this->load->view('header');
$this->load->view('profile', $error);
$this->load->view('footer');
}
}

Related

Form not uploading file in codeigniter 2.0

I have tried every possible method but no success. Just trying to upload file using code igniter but not working the error I am getting
<pre>Array
(
[error] => <p>You did not select a file to upload.</p>
)
I have tried in normal core php at my local host that works fine but not working with code igniter. It is simply not picking the file. If I check with var_dump($_FILES['fileToUpload']); the result will be array(0).
Form Code
<form id="contact_form" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>Main/do_upload">
<input type="file" class="form-control" name="fileToUpload" id="fileToUpload">
</form>
Controller Code
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
echo "<pre>";
var_dump($data);
// $this->load->view('upload_success',$data);
}else{
$error = array('error' => $this->upload->display_errors());
echo "<pre>";
print_r($error);
}
config
$autoload['libraries'] = array("session", "email", "database");
$autoload['helper'] = array("url", "file", "form");
Is there anything I am not aware of ? Please guide I am stuck here.
You missed input file name in do_upload():
Use :
if(!$this->upload->do_upload('image_file'))
{
//$this->upload->display_errors()
}
else
{
//$this->upload->data()
}
Instead of:
if($this->upload->do_upload())
You miss parameters in $this->upload->do_upload Pleas check below code.
public function do_upload(){
$config = array(
'upload_path' => "assets/uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('fileToUpload'))
{
$data = array('upload_data' => $this->upload->data());
echo "<pre>";
var_dump($data);
// $this->load->view('upload_success',$data);
}else{
$error = array('error' => $this->upload->display_errors());
echo "<pre>";
print_r($error);
}
}
Pass your file upload name in $this->upload->do_upload('fileToUpload')

Uploading file in a form on codeigniter

I have tried all solutions but I can't tell what's wrong. Codeigniter keeps telling me that there's no file uploaded. I created the folder at the root of the project. I've seen other similar questions but I can't manage to make it work with their solutions.
This is my controller:
public function index() {
$this->form_validation->set_rules('name', 'name', 'required|trim|max_length[45]');
$this->form_validation->set_rules('type_id', 'type', 'required|trim|max_length[11]');
$this->form_validation->set_rules('stock', 'stock', 'required|trim|is_numeric|max_length[11]');
$this->form_validation->set_rules('price', 'price', 'required|trim|is_numeric');
$this->form_validation->set_rules('code', 'code', 'required|trim|max_length[45]');
$this->form_validation->set_rules('description', 'description', 'required|trim|max_length[45]');
$this->form_validation->set_rules('active', 'active', 'required|trim|max_length[45]');
$this->form_validation->set_rules('unit_id', 'unit', 'required|trim|max_length[45]');
$this->form_validation->set_rules('userfile', 'File', 'trim');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) { // validation hasn't been passed
$this->load->view('product/add_view');
} else { // passed validation proceed to post success logic
// build array for the model
$form_data = array(
'name' => set_value('name'),
'type_id' => set_value('type_id'),
'stock' => set_value('stock'),
'price' => set_value('price'),
'code' => set_value('code'),
'description' => set_value('description'),
'active' => set_value('active'),
'unit_id' => set_value('unit_id')
);
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
$this->upload->initialize($config); //Make this line must be here.
$imagen = set_value('userfile');
// run insert model to write data to db
if ($this->product_model->product_insert($form_data) == TRUE) { // the information has therefore been successfully saved in the db
if (!$this->upload->do_upload($imagen)) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('product/add_view', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('product/add_view', $data);
}
} else {
redirect('products/AddProduct', 'refresh');
// Or whatever error handling is necessary
}
}
}
This is my view (just showing the part that matters)
<?php // Change the css classes to suit your needs
$attributes = array('class' => '', 'id' => '');
echo form_open_multipart('products/AddProduct', $attributes); ?>
<p>
<label for="picture">Picture <span class="required">*</span></label>
<?php echo form_error('userfile'); ?>
<?php echo form_upload('userfile')?>
<br/>
</p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
EDIT: Applying the modification from the answer I get an error 500.
I tried to work on your code. and it worked on me after this changes
$config = array(
'upload_path' => "./uploads/",
'upload_url' => base_url()."uploads/", // base_url()."/uploads/", //added
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload'); //changed
$this->upload->initialize($config); //Make this line must be here.
$imagen = userfile; // $_FILES['userfile']['name'] //changed
If you get The localhost page isn’t working localhost is currently unable to handle this request. HTTP ERROR 500 possible error on your syntax, I just add 1 more } at the end of your code. You can check phpinfo() for other information. Also, there might be a problem on your file locations maybe the address you specified or the permissions if your running this on server.

File uploading is not working properly in codeigniter

I am using code-igniter to upload files with two input boxes .
But my code not uploading both the files i m getting same file two times.
please take a look my code -:
**//html**
<?php echo form_open_multipart('vipul/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='file' name='userfile1' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
in controller
public function do_upload(){
foreach ($_FILES as $keys=>$values){
if($keys == 'userfile1'){
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => false,
'max_size' => "20480000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768000",
'max_width' => "1024000"
);
$this->upload->initialize($config);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{ $data = array('upload_data' => $this->upload->data());}
else{ $error = array('error' => $this->upload->display_errors()); }
}
if($keys == 'userfile'){
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => false,
'max_size' => "20480000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768000",
'max_width' => "1024000"
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{ $data = array('upload_data' => $this->upload->data()); }
else{ $error = array('error' => $this->upload->display_errors()); }
}
}
Please change your PHP code and try this out
$this->upload->do_upload()
to
$this->upload->do_upload($field_name)
where $field_name is your input name attribute.
please check CodeIgniter's File Uploading Class

CodeIgniter uploading files no file

Ok try to upload file for hours but i get error,
You did not select a file to upload.
my code is in CI
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/uploads/",
'upload_url' => base_url()."uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $this->config);
if($this->upload->do_upload('logo'))
{
echo "file upload success";
}
else
{
echo $this->upload->display_errors();
}
in view i have
<input type="file" name="logo"/>
when i print_r $_POST i get
Array ( [name_srpski] => tyre [name_english] => Client nametre [logo] => cipele-plava_1.jpg )
Where could be error its very important
Try the following in config:
'upload_path' => FCPATH . "/uploads/", // or use "./uploads/" instead
'max_size' => "1000", // remove the kb from the string. it requires only the number
Try changing $this->load->library('upload', $this->config); to
$this->load->library('upload');
$this->upload->initialize( $this->config );
Also the form type should be multipart
<form method="post" action="some_action" enctype="multipart/form-data" />

Thumbs images upload error in codeigniter

<?php
$image = array(
'name' => 'userfile',
'id' => 'userfile',
);
$submit = array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Upload'
);
?>
<?php echo form_open_multipart('upload/upload_image', 'id=upload_file'); ?> <!-- must autoload form helper for this -->
<?php echo form_upload($image); ?>
<?php echo form_submit($submit); ?>
<?php echo form_close(); ?>
this is main.php in view folder
<?php
class Upload_model extends CI_Model{
var $original_path;
var $resized_path;
var $thumbs_path;
//initialize the path where you want to save your images
function __construct(){
parent::__construct();
//return the full path of the directory
//make sure these directories have read and write permessions
$this->original_path = realpath(APPPATH.'../uploads/original');
$this->resized_path = realpath(APPPATH.'../uploads/resized');
$this->thumbs_path = realpath(APPPATH.'../uploads/thumbs');
}
function do_upload(){
$this->load->library('image_lib');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path //upload directory
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
print_r($image_data);
if($image_data){
echo "uploaded";
}else {echo "not upload";}
//your desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 128,
'height' => 128
);
//this is the magic line that enables you generate multiple thumbnails
//you have to call the initialize() function each time you call the resize()
//otherwise it will not work and only generate one thumbnail
$this->image_lib->initialize($config);
$this->image_lib->resize();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->thumbs_path,
'maintain_ratio' => true,
'width' => 36,
'height' => 36
);
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}
this is my model
and controller only load main.php that allow me select image click submit and then controller call model above to load image
the problem is --> there are no error generate but image and thumbs not uploaded to directory why please?

Categories