Can not insert the data with image upload - php

I made insert data along upload images into the database , when the run was successful , but when the insert data and upload images contained errors.
The path to the image is not correct.
Your server does not support the GD function required to process this type of image.
how to cope if I insert the data without uploading images is not error ? and the image database went into default image ?
This my controllers
public function save(){
$this->load->library('image_lib');
//$nama_asli = $_FILES['userfile']['name'];
$id = $this->input->post('id',TRUE);
$config['file_name'] = $id ;//'_'.'_'.$nama_asli;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$files = $this->upload->data();
$fileNameResize = $config['upload_path'].$config['file_name'];
$size = array(
array('name' => 'thumb','width' => 100, 'height' => 100, 'quality' => '100%')
);
$resize = array();
foreach($size as $r){
$resize = array(
"width" => $r['width'],
"height" => $r['height'],
"quality" => $r['quality'],
"source_image" => $fileNameResize,
"new_image" => $url.$r['name'].'/'.$config['file_name']
);
$this->image_lib->initialize($resize);
if(!$this->image_lib->resize())
die($this->image_lib->display_errors());
}
}
else
{
$data = array('upload_data' => $this->upload->data());
$get_name = $this->upload->data();
$nama_foto = $get_name['file_name'];
$this->mcrud->savealat($nama_foto);
redirect('instrument/detailalat');
}
}
This my model
function savealat($nama_foto) {
$data = array(
'id' => $this->input->post('id'),
'namaalat' => $this->input->post('namaalat'),
'dayalistrik' => $this->input->post('dayalistrik'),
'merk' => $this->input->post('merk'),
'namasupplier' => $this->input->post('namasupplier'),
'nokatalog' => $this->input->post('nokatalog'),
'noseri' => $this->input->post('noseri'),
'category' => $this->input->post('category'),
'lokasi' => $this->input->post('lokasi'),
'pengguna' => $this->input->post('pengguna'),
'status' => $this->input->post('status'),
'jadwalkal' => $this->input->post('jadwalkal'),
'manual' => $this->input->post('manual'),
'dateinput' => $this->input->post('date'),
'foto' => $nama_foto
//'created' => $tanggal
);
$this->db->insert('tbdetail', $data);
}

public function save(){
$this->load->library('image_lib');
//$nama_asli = $_FILES['userfile']['name'];
$id = $this->input->post('id',TRUE);
$config['file_name'] = $id ;//'_'.'_'.$nama_asli;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$files = $this->upload->data();
$fileNameResize = $config['upload_path'].$config['file_name'];
$size = array(
array('name' => 'thumb','width' => 100, 'height' => 100, 'quality' => '100%')
);
$resize = array();
foreach($size as $r){
$resize = array(
"width" => $r['width'],
"height" => $r['height'],
"quality" => $r['quality'],
"source_image" => $fileNameResize,
"new_image" => base_url().$r['name'].'/'.$config['file_name']
);
$this->image_lib->initialize($resize);
if(!$this->image_lib->resize())
die($this->image_lib->display_errors());
}
$data = array('upload_data' => $this->upload->data());
$get_name = $this->upload->data();
$nama_foto = $get_name['file_name'];
$this->mcrud->savealat($nama_foto);
redirect('instrument/detailalat');
}
else
{
//Moved your code up there
}
}
If I'm right the problem is that you put the upload in else. Try to move the code and tell me if it works

MaY be this answer can help you out. There are few more suggestion in comment part which can figure some way out for you. Your server does not support the GD function required to process this type of image.Ci

Related

Codeigniter Image Upload PathInfo Extension 'The filetype you are attempting to upload is not allowed'

I am uploading multiple images and I need to get their different file format.
But the problem is i only got the first file image format and there's an error when I do resize images because of the incorrect file format.
I am having an error The filetype you are attempting to upload is not allowed. which occur when I put the PATHINFO_EXTENSION when getting file.
Here's my code
for($i = 0; $i < $count_upload; $i++) {
$_FILES['userfile']['name'] = pathinfo($_FILES['product_image']['name'][$i], PATHINFO_EXTENSION);
$_FILES['userfile']['type'] = $_FILES['product_image']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['product_image']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['product_image']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['product_image']['size'][$i];
$file_name = $file_name;
$config = array(
'file_name' => $file_name,
'allowed_types' => 'jpg|jpeg|png',
'max_size' => 3000,
'overwrite' => TRUE,
'encrypt_name' => FALSE,
'remove_spaces' => TRUE,
'upload_path'
=> $directory
// => './assets/img/products'
);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$response = array(
'status' => false,
'environment' => ENVIRONMENT,
'message' => $error['error'],
'csrf_name' => $this->security->get_csrf_token_name(),
'csrf_hash' => $this->security->get_csrf_hash()
);
echo json_encode($response);
die();
}else {
$file_name = $this->upload->data()['file_name'];
echo $file_name;
echo '<br>';

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 to code multiple file upload using CodeIgniter?

I want to have a multiple file upload code.
For example:
Koala.jpg
Penguins.jpg
Jellyfish.jpg
There is input text where the user can set the new name of the image.
The user will now upload the images and the inputted text for new image name is "Animals"
Now, what I want is when this uploaded the output should be Animals1.jpg, Animals2.jpg, Animals3.jpg.
The problem is when I tried to upload all these images, only one image is uploading.
I tried to make research and applied some codes on my program, but still not working.
Controller
public function do_upload() {
$config = array(
'image_library' => 'gd2',
'file_name' => $this->input->post('finame'),
'upload_path' => './public/img/uploads',
'upload_url' => base_url().'public/img/uploads',
'allowed_types' => 'gif|jpg|jpeg',
'max_size' => '1024KB',
'max_width' => '1024',
'max_height' => '768',
'maintain_ratio'=> TRUE,
'overwrite' => false,
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error_msg = "<div class='alert alert-error'>".$this->upload->display_errors()."</div>";
$error = array('error' => $error_msg);
}
else {
$upload_data = $this->upload->data();
$data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
$file_array = array(
'image' => $data['thumbnail_name'],
'image_name' => $upload_data['file_name'],
//'description' => "",
'date_created' => date('Y-m-d H:i:s', now()),
'date_modified' => date('Y-m-d H:i:s', now()),
'author' => $this->session->userdata('username'),
'size' => $upload_data['file_size'],
'type' => $upload_data['image_type'],
'width' => $upload_data['image_width'],
'height' => $upload_data['image_height'],
//'document_name' => $field,
//'department' => $field2,
//'notes' => "",
);
$this->session->set_userdata('image_print', $file_array);
$this->load->database();
$this->db->insert('tbl_image', $file_array);
$data = array('upload_data' => $this->upload->data());
$user_level['records']=$this->user_model->get_records();
$this->load->view('v_dashboard/page/header_view', $user_level);
$this->load->view('v_document/upload/upload_result_view', $data);
$this->load->view('v_dashboard/page/footer_view');
}
}
I have this on my HTML
<label for="file"><strong>Select File To Upload:</strong></label>
<input type="file" name="userfile[]" multiple class="btn transcolor btn-file"/>
<br/><br/>
By the way, I'm using BLOB on my database.
I tried to refer to this links
Ellislab
GitHub
StackOverflow
StackOverflow
CodingLikeASir
You have to run the for loop till the count of the uploaded image file.
Like this:
for ($i=0; $i < count($_FILES['userfile']['name']); $i++)
{
// function to add the image name one by one into database.
}

Categories