You did not select a file to upload CodeIgniter - php

$this->upload->data() result is
Array
(
[file_name] => 72f59510f9bbf05933c89e4951acc29d
[file_type] =>
[file_path] => ./inst/public/uploads/
[full_path] => ./inst/public/uploads/72f59510f9bbf05933c89e4951acc29d
[raw_name] => 72f59510f9bbf05933c89e4951acc29d
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
error:
Array
(
[error] => You did not select a file to upload.
)
upload function
function upload(){
if(isset($_POST['userfile']) AND !empty($_POST['userfile']))
{
$Info = $this->login();
if(#$Info)
{
$config['upload_path'] = './inst/public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '230';
$config['max_height'] = '280';
$config['min_width'] = '220';
$config['min_height'] = '270';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['file_name'] = md5(uniqid("100_ID", true));
$this->load->library('upload', $config);
$Setting = $this->Setting;
$this->load->view('header',$Setting);
if ( ! $this->upload->do_upload("userfile"))
{
$response['error'] = array('error' => $this->upload->display_errors());
echo '<pre>';
print_r( $this->upload->data());
$this->load->view('upload_done', $response);
}
else
{
$response['success'] = array('upload_data' => $this->upload->data());
$this->load->view('upload_done', $response);
}
}
}
}
form code
<?php
echo form_open('/Home/upload');
?>
<br><div class="form-group"><input class ='form-control' placeholder="<?php echo lang('fileu'); ?>" type="file" name="userfile" size="20" /></div>
<div class="alert alert-info"><?php echo lang('filetext'); ?></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><?php echo lang('Close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo lang('uploadsub'); ?></button>
<?php echo form_close(); ?>

you have to use below code to upload files. You are missing multipart attribute in your form.
echo form_open_multipart('/Home/upload');

First change your helper function to form_open_multipart(). If you still get the error after changing to the correct function, it could be your maxsize property as well .If an uploaded file is larger than the allowable size, the FILES variable will be empty.
Try changing...
$config['max_size'] = '1000';
...to something like...
$config['max_size'] = '30000';
php.net File Upload

Related

How to send multiple file name to database and move file to folder? Codeigniter

I want to upload multiple images. But I don't know what I send data to database and how to move files to folder.
This is my view :
<div class="form-group">
<div class="custom-file">
<input type="file" class="form-control custom-file-input" name="uploadFile[]" id="uploadFile" multiple>
</div>
</div>
And this is my controller :
public function add_event() {
// Our calendar data
$judul = $this->input->post('judul', TRUE);
$waktu_mulai = $this->input->post('mulai', TRUE);
$waktu_berakhir = $this->input->post('berakhir', TRUE);
$lokasi = $this->input->post('lokasi', TRUE);
$scope = $this->input->post('scope', TRUE);
$kategori = $this->input->post('kategori', TRUE);
$satuan_kerja = $this->input->post('satuan_kerja', TRUE);
$keterangan = $this->input->post('keterangan', TRUE);
$agenda_pimpinan = $this->input->post('agenda_pimpinan', TRUE);
// var_dump (count($upload_file)); die;
for($i = 0; $i < count($upload_file); $i++) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '500';
$this->load->library('upload', $config);
$this->KalenderModel->addEvent(array(
'judul' => $judul,
'mulai' => $waktu_mulai,
'berakhir' => $waktu_berakhir,
'lokasi' => $lokasi,
'scope' => $scope,
'satuan_kerja' => $satuan_kerja,
'keterangan' => $keterangan,
'kategori' => $kategori,
'lampiran' => $uploadfile,
'tampilkan_agenda_pimpinan' => $agenda_pimpinan
));
}
redirect('agendakerja/kalender');
} //end function
Can somebody help?
To get all infos you need about files, you can use
$_FILES;
Here is how you can use it.
Then to move your file, there is the function :
bool move_uploaded_file ( string $filename , string $destination );
Here is more infos.
if you want to upload the file in particular folder then use
$this->upload->do_upload($upload_file[0])
and define the path in $config
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '500';
$this->load->library('upload', $config);
$this->upload->initialize($config);
for($i = 0; $i < count($upload_file); $i++) {
if($this->upload->do_upload('bot_logo'))
{
$this->KalenderModel->addEvent(array(
'judul' => $judul,
'mulai' => $waktu_mulai,
'berakhir' => $waktu_berakhir,
'lokasi' => $lokasi,
'scope' => $scope,
'satuan_kerja' => $satuan_kerja,
'keterangan' => $keterangan,
'kategori' => $kategori,
'lampiran' => $uploadfile,
'tampilkan_agenda_pimpinan' => $agenda_pimpinan
));
}
else
{
echo this->upload->display_errors();
}
}

File name not grabbed when using CodeIgniter upload library

Firstly can I request this is NOT marked as duplicate. I have read the other posts on SO regarding issues with the CodeIgniter upload library and sadly they do not cover this. I have also extensively read the CI documentation and everything suggests this should work correctly.
I am using a very simple form to grab the file, which is uploaded correctly to the images folder. The full_path of the file is also written successfully to a db table called images. The filename however is blank.
My form:
<?php echo form_open_multipart('image_upload/do_upload');?>
<input type="file" name="userfile" size="20" multiple="true" />
<br /><br />
<input type="submit" value="upload" />
</form>
My Controller function:
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$image_data = $this->upload->data();
echo '<pre>'; print_r($image_data); echo '</pre>';
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}
I am using
echo print_r($image_data); echo;
To display the associated image data but this is all that is returned:
Array
(
[file_name] =>
[file_type] =>
[file_path] => c:/wamp/www/honest/images/
[full_path] => c:/wamp/www/honest/images/
[raw_name] =>
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
I can't work out why it is not grabbing the file name and other details - can someone help spot what is hopefully a simple error?
Many thanks,
DP.
You are requesting your uploaded data before you actually upload anything. That's why your $image_data only contains your configuration values. Move your $image_data = $this->upload->data(); call to after actually performing the do_upload() (into your else block):
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$image_data = $this->upload->data();
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}

CodeIgniter And Image Display in Table

I'm using codeIgniter and uploading image path along with form data in database. The problem that i'm facing is they're not displaying in table. Images are successfully uploaded in the folder and the path is also saved in db but it's not displaying the images.
Code that i've written is below.
In view file:
<?php echo form_open_multipart('welcome/insertion'); ?>
<input type="file" name="userfile" id="userfile" size="40" maxlength="90" tabindex="3"/>
<input name="saveForm" type="submit" value="Insert Record" class="iconSave" />
In controller i write:
public function insertion()
{
$this->load->model('data_maintenance','',TRUE);
$this->data_maintenance->insertion();
}
And finally in model:
public function insertion()
{
$config['upload_path'] = 'application/uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
echo $this->upload->display_errors();
}
else
{
//$file_data = $this->upload->data('image');
$file_data = $this->upload->data();
$new_staff = array(
'agenda_id' => $this->input->post('agenda_id'),
't_name' => $this->input->post('t_name'),
'exp' => $this->input->post('exp'),
'leaves_allow_monthly' => $this->input->post('leaves_allow_monthly'),
'leaves_allow_annualy' => $this->input->post('leaves_allow_annualy'),
'gender' => $this->input->post('gender'),
'cell_no' => $this->input->post('cell_no'),
'dob' => $this->input->post('dob'),
'file' => $file_data['file_name']
);
}
$d="/SampleOOP/application/uploads/" . $new_staff['file'];
$this->db->set('image', $d);
//$this->db->insert('teachers_record');
//$d="/SampleOOP/application/uploads/".$new_staff['file'];
$this->db->set('teacher_name', $new_staff['t_name']);
//$this->db->set('image',$d);
$this->db->insert('teachers_record');
}
try print_r($file_data) and see what comes back, you can also try: $file_data['orig_name']
Or:
$file_name = $file_data['file_name'];
$new_staff = array(
'agenda_id' => $this->input->post('agenda_id'),
't_name' => $this->input->post('t_name'),
'exp' => $this->input->post('exp'),
'leaves_allow_monthly' => $this->input->post('leaves_allow_monthly'),
'leaves_allow_annualy' => $this->input->post('leaves_allow_annualy'),
'gender' => $this->input->post('gender'),
'cell_no' => $this->input->post('cell_no'),
'dob' => $this->input->post('dob'),
'file' => $file_name
);
UPDATE, Try:
$config['upload_path'] = 'application/uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$this->load->library('upload', $config);
Or
$this->load->library('upload');
$config['upload_path'] = 'application/uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$this->upload->initialize($config);

how can i upload images in Codeigniter

i have problem when upload images in codeigniter, So i have controller that upload images like this :-
public function index()
{
$this->load->model('blog');
$type = "text";
if (isset($_POST['post'])) {
if (isset($_POST['type']) && $_POST['type'] == "image") {
$type = "image";
}
if (strlen($_FILES['inputUpProfile']['name']) > 0) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('inputUpProfile')) {
$error = $this->upload->display_errors();
if (is_array($error)) {
foreach ($error as $er) {
$this->errors[] = $er;
}
} else {
$this->errors[] = $error;
}
} else {
$updata = $this->upload->data();
$imagePath = './uploads/' . $eventpic;
if (file_exists($imagePath)) {
#unlink($imagePath);
}
$eventpic = $updata['raw_name'] . $updata['file_ext'];
}
}
$result = $this->blog->addPost($_SESSION['user_id'], $type, $this->input->post('post'), $eventpic);
}
$result = $this->blog->getPosts($_SESSION['user_id'], 0, 10);
$this->template->build("home_view", array("response" => $result));
}
and view is like this :-
<div class="textstatus">
<input id="inputUpProfile" name="inputUpProfile"
class="inputUpProfile hidefile" type="file"/>
<input type="button" id="PicUpProfile" class="sentpic" value="addpic">
<input name="post" type="text" id="text" placeholder="message ...">
<input type="submit" id="sent" value="Send">
</div>
</form>
</div>
when i upload thee images in my site the error is display like :-
Severity: Notice
Message: Undefined index: inputUpProfile
Filename: controllers/home.php
Line Number: 47
and
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: eventpic
Filename: controllers/home.php
Line Number: 74
and
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\sys\system\core\Exceptions.php:185)
Filename: core/Common.php
Line Number: 442
so where are the problem in my code.
/****************
edit :-
Array ( [upload_data] => Array ( [file_name] => 67429_133961013479569_306349156_n3.jpg [file_type] => image/jpeg [file_path] => D:/AppServ/www/d5n/rashaqa2/uploads/ [full_path] => D:/AppServ/www/d5n/rashaqa2/uploads/67429_133961013479569_306349156_n3.jpg [raw_name] => 67429_133961013479569_306349156_n3 [orig_name] => 67429_133961013479569_306349156_n.jpg [client_name] => 67429_133961013479569_306349156_n.jpg [file_ext] => .jpg [file_size] => 34.05 [is_image] => 1 [image_width] => 720 [image_height] => 540 [image_type] => jpeg [image_size_str] => width="720" height="540" ) )
i need from this array [file_name] to save in DB, how can i read this.
if($_FILES['nameofinputtype']['error'] == UPLOAD_ERR_OK)
{
// A file was uploaded
$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);
$imagen = $this->upload->do_upload();
}

codeigniter form

i was tired after 3 day searching.. can somebody tell me or show me an example how to upload text with image?
my table:
name -> varchar
lastname -> varchar
position -> varchar
pass_id -> int
image_url -> varchar
this is a form_view file
<h2>Create</h2>
<?php echo form_open('main/add'); ?>
<p>
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
</p>
<p>
<label for="lastname">Lastname:</label>
<input type="text" name="lastname" id="lastname" />
</p>
<p>
<label for="position">Position:</label>
<input type="text" name="position" id="position" />
</p>
<p>
<label for="name">Passport ID:</label>
<input type="text" name="pass_id" id="pass_id" />
</p>
<input type="file" name="userfile" size="20" />
<input type="submit" value="Submit" />
<?php echo form_close(); ?>
this is a controller file:
public function add()
{
$subject = array(
'name' => $this->input->post('name'),
'lastname' => $this->input->post('lastname'),
'position' => $this->input->post('position'),
'pass_id' => $this->input->post('pass_id')
);
$this->main_model->add($subject);
$this->index();
}
and a model file:
public function add($object)
{
$this->db->insert('stuf', $object);
return;
}
how to upload image url in the db.
i'm newbie in CI. thanks
use form_open_multipart("main/add") instead of form_open("main/add").
And modify your controller a bit:
$this->load->library('upload'); // See File Uploading in Code Igniter Documentation
$this->load->helper(array('form', 'url')); // See form, and url helper
$my_path = "your_uploads_folder"
$config['upload_path'] = './'. $my_path .'/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->do_upload('userfile', $config);
$subject = array(
'url' => $url . '/' . $my_path // Dont forget to create the table field
'name' => $this->input->post('name'),
'lastname' => $this->input->post('lastname'),
'position' => $this->input->post('position'),
'pass_id' => $this->input->post('pass_id'),
);
$this->main_model->add($subject);
redirect('/main/index/', 'refresh'); // Use redirect instead
Note: It has not been tested yet
$this->upload->data()
This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
Source
ok this is a solution :)
public function new_record()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile'))
{
echo $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
$full_path = 'uploads/' . $data['file_name'];
$subject = array(
'name' => $this->input->post('name'),
'lastname' => $this->input->post('lastname'),
'position' => $this->input->post('position'),
'pass_id' => $this->input->post('pass_id'),
'image_url' => $full_path
);
$this->main_model->add($subject);
redirect('/main/index/', 'refresh');
}

Categories