I made a file upload in codeigniter, but in the controller I set $ directory = './assets/images/'; unreadable on vmware.
please help, here is my controller
public function tambahfotoproses()
{
$variable = 'foto'; //variable name dari form
$directory = './assets/images/'; //direktori
$allowed_file = 'gif|jpg|jpeg|png|JPG|GIF|PNG|JPEG'; //file yang diizinkan dibatasi dengan tanda
$upload['detail'] = $this->m_admin->uploadfile($variable,$directory,$allowed_file); //proses dengan modul insertfile
var_dump($upload); die();
/* to upload file */
$filename = $upload['detail']['file_name'];
if ($filename != "")
{
$data = array(
'foto' => $filename,
'person_nm' => $this->input->post('person_nm'),
'pengupload' => $this->input->post('pengupload'),
'tgl' => $this->input->post('tgl')
);
$insert = $this->db->insert('eregister_foto',$data);
} else {
$data = array(
'foto' => $filename,
'person_nm' => $this->input->post('person_nm'),
'pengupload' => $this->input->post('pengupload'),
'tgl' => $this->input->post('tgl')
);
$insert = $this->db->insert('eregister_foto',$data);
}
redirect('index.php/admin/foto_dr');
}
I deliberately gave var_dump () to find out the results of $ upload
the result is array (1) {["detail"] => int (0)}
photo not uploaded
and here is my modals.php
/AKSI UPLOAD FILE/
function uploadfile($var,$dir,$all){
$new_name = $this->input->post('person_nm');
$config2=array(
'image_library' => 'gd2',
'upload_path' => $dir."/dokter/", //lokasi gambar akan di simpan
'allowed_types' => $all, //ekstensi gambar yang boleh di unggah
'create_thumb' => TRUE,
'max_size' => '2048', //batas maksimal ukuran gambar
'file_name' => $new_name
);
$this->load->library('upload');
$this->upload->initialize($config2);
if ($this->upload->do_upload($var))
{
return $this->upload->data();
}
else
{
return 0;
}
}
i've try to rewrite folder "dokter" too, and still can't work
Related
This is my controller
<?php
class Avatar extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->config('avatar');
$this->load->library('upload');
}
public function index()
{
// Prepare data
$data = array(
'avatar' => $this->user->getAvatar($this->user->getId()),
'max_size' => $this->config->item('max_size'),
'max_width' => $this->config->item('max_width'),
'max_height' => $this->config->item('max_height'),
'allowed_types' => $this->config->item('allowed_types'),
);
// Load the avatar page
$content = $this->template->loadPage("avatar.tpl", $data);
$title = breadcumb(array(
"ucp" => lang("ucp"),
"ucp/avatar" => lang("change_avatar", "ucp")
));
// Put it in a content box
$this->template->box($title, $content, true, "modules/ucp/css/avatar.css");
}
public function upload()
{
$this->load->model("avatar_model");
// Prepare data
$config = array(
'max_size' => $this->config->item('max_size'),
'max_width' => $this->config->item('max_width'),
'max_height' => $this->config->item('max_height'),
'allowed_types' => $this->config->item('allowed_types'),
'encrypt_name' => $this->config->item('encrypt_name'),
'xss_clean' => $this->config->item('xss_clean')
);
$this->upload->initialize($config);
if (!$this->upload->do_upload('avatar'))
$data = array('error' => $this->upload->display_errors());
else
{
$this->avatar_model->setAvatar(array('avatar' => $this->upload->data('file_name')));
$data = array('error' => 'Success');
}
$title = breadcumb(array(
"ucp" => lang("ucp"),
"ucp/avatar" => lang("change_avatar", "ucp"),
"ucp/avatar/upload" => lang("upload_avatar", "ucp"),
));
$page = $this->template->loadPage("upload.tpl", $data);
$this->template->box($title, $page, true);
}
}
and this is the model
<?php
class Avatar_model extends CI_Model
{
public function setAvatar($avatar)
{
$this->db->update('account_data', $avatar, array('id' => $this->user->getId()));
}
}
I have no idea what is wrong, the configs are just my preference right?
<?php
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 512; // KB
$config['max_width'] = 500;
$config['max_height'] = 500;
$config['encrypt_name'] = true;
$config['xss_clean'] = false;
this is what i get when i try to upload the image
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: mysqli/mysqli_driver.php
Line Number: 612
A Database Error Occurred
Error Number: 1054
Unknown column 'Array' in 'field list'
UPDATE account_data SET avatar = Array WHERE id = '11'
Filename: C:\xampp\htdocs\system\database\DB_driver.php
Line Number: 328
Attempting to generate thumbnails with Image_moo & CodeIgniter framework. Image_moo does not output any errors, however, thumbnail images are never generated.
dir structure
app
- controllers/
Admin.php
...
- libraries/
Image_moo.php
...
- models/
Admin_photos_model.php
Admin.php
public function photo_upload() {
$rules = [
[
'field' => 'caption',
'label' => 'Caption'//,
//'rules' => 'required'
],[
'field' => 'description',
'label' => 'Description'//,
//'rules' => 'required'
],[
'field' => 'series',
'label' => 'Series',
'rules' => 'required'
]
];
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == FALSE) {
$this->load->view('admin/photos/upload');
} else {
$series = str_replace(' ', '', strtolower($_POST['series']));
$upload_path = './img/photos/'.$series.'/';
$config = [
'upload_path' => $upload_path, //'./img/photos/'.$series.'/',
'allowed_types' => 'gif|jpg|jpeg|png'
];
$this->load->library('upload', $config);
if (!file_exists($upload_path)) { //check if series dir exists
mkdir($upload_path, 0777, true); // create dir if !exist
$num = 1; //init
} else {
$num = $this->db->where('series', $series)->count_all_results('photos') + 1;
};
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/photos/upload', $error);
} else {
$file = $this->upload->data();
$caption = $_POST['caption'];
$description = $_POST['description'];
$data = [
'filename' => $file['file_name'],
'series' => $series,
'num' => $num,
'caption' => $caption,
'description' => $description
];
$this->Admin_photos_model->upload($data);
$this->load->library('image_moo'); //create thumbnail, upload
$file_raw_name = $this->upload->data('raw_name');
$file_ext = $this->upload->data('file_ext');
$file_width = $this->upload->data('image_width');
$file_height = $this->upload->data('image_height');
$file_uploaded = $upload_path.$data['filename']; //$field_info->upload_path.'/'.$uploader_response[0]->name;
if ($file_width > 1024 && $file_height > 720) {
$this->image_moo->load($file_uploaded)
->resize_crop(1024,720)->save($upload_path.$file_raw_name.'_thumb_xl'.$file_ext)
->resize_crop(800,562)->save($upload_path.$file_raw_name.'_thumb_lg'.$file_ext)
->resize_crop(640,450)->save($upload_path.$file_raw_name.'_thumb_med'.$file_ext)
->resize_crop(450,316)->save($upload_path.$file_raw_name.'_thumb_sm'.$file_ext)
->resize_crop(222,156)->save($upload_path.$file_raw_name.'_thumb_xs'.$file_ext);
$data = [
'has_thumb_xl' => 1,
'has_thumb_lg' => 1,
'has_thumb_med' => 1,
'has_thumb_sm' => 1,
'has_thumb_xs' => 1,
'thumb_xl_filename' => $file_raw_name.'_thumb_xl'.$file_ext,
'thumb_lg_filename' => $file_raw_name.'_thumb_lg'.$file_ext,
'thumb_med_filename' => $file_raw_name.'_thumb_med'.$file_ext,
'thumb_sm_filename' => $file_raw_name.'_thumb_sm'.$file_ext,
'thumb_xs_filename' => $file_raw_name.'_thumb_xs'.$file_ext
];
};
if ($this->image_moo->error) {
print $this->image_moo->display_errors();
};
$this->Admin_photos_model->thumbnails($data);
$this->session->set_flashdata('message','file uploaded: '.$file_uploaded.'New image has been added..'.'series dir: '.$series.'last num of series: '.$num.'thumb:'.$file_raw_name.'_thumb_xl'.$file_ext.'errors: '.$this->image_moo->display_errors());
redirect('admin/photos');
};
Admin_photos_model
<?php
class Admin_photos_model extends CI_Model {
public function __construct(){
$this->load->database();
}
public function upload($data) {
try {
$this->db->insert('photos', $data);
return true;
} catch (Exception $e) {
echo $e->getMessage();
};
}
public function thumbnails($data) {
try {
$this->db->insert('photos', $data);
return true;
} catch (Exception $e) {
echo $e->getMessage();
};
}
}
Attempting to generate thumbnails, I'm separating the photos by series. If the series hasn't started, a new dir is created. Ideally, uploading 'waterfall.jpg' with series 'nature' would yield:
app
...
public_html/
img/
photos/
nature/
waterfall.jpg
waterfall_thumb_xl.jpg
waterfall_thumb_lg.jpg
waterfall_thumb_med.jpg
waterfall_thumb_sm.jpg
waterfall_thumb_xs.jpg
Any help would be appreciated.
Reading the image_moo documentation, the save function needs to have an overwrite=FALSE/TRUE. Doing so seemed to fix it.
"save($x,$overwrite=FALSE) - Saved the manipulated image (if
applicable) to file $x - JPG, PNG, GIF supported. If overwrite is not
set file write may fail. The file saved will be dependant on
processing done to the image, or a simple copy if nothing has been
done."
i've error when upload and store an image to mysql without active record codeigniter.
It show
Message: Illegal string offset 'file_ext'
Not only file_ext, but file_size too. This problem only happen when i don't use active record like this code below:
<?php
if ($_FILES['userfile']['error'] <> 4)
{
$nmfile = $this->input->post('name');
$config['upload_path'] = './assets/images/user/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '2048'; // 2 MB
$config['max_width'] = '2000'; //pixels
$config['max_height'] = '2000'; //pixels
$config['file_name'] = $nmfile;
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$this->create();
}
else
{
$userfile = $this->upload->data();
$thumbnail = $config['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/images/user/'.$userfile['file_name'].'';
// membuat thumbnail
$config['create_thumb'] = TRUE;
// rasio resolusi
$config['maintain_ratio'] = FALSE;
// lebar
$config['width'] = 150;
// tinggi
$config['height'] = 150;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$id = $this->input->post('id');
$name = $this->input->post('name');
$username = $this->input->post('username');
$psw1 = $this->input->post('psw1');
$psw2 = $this->input->post('psw2');
$usertype = $this->input->post('usertype');
$userfile = $nmfile;
$userfile_type = $userfile['file_ext'];
$userfile_size = $userfile['file_size'];
$sql = $this->db->query("INSERT INTO user (id, name, username, psw1, psw2, usertype, userfile, userfile_type, userfile_size)
VALUES ('$id', '$name', '$username', password('$psw1'), password('$psw2'), '$usertype', '$userfile', '$userfile_type', '$userfile_size') ");
$this->session->set_flashdata('message', '<div class="alert alert-success alert">Data berhasil dibuat</div>');
redirect(site_url('auth/user'));
}
}
?>
any help will be so appricated, thank you
I believe you can achieve your results in a lovely style like below:
if (!empty($_FILES)) {
$data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'username' => $this->input->post('username'),
'psw1' => $this->input->post('psw1'),
'psw2' => $this->input->post('psw2'),
'usertype' => $this->input->post('usertype')
);
$upload = $this->imageUpload();
if (!$upload) {
$this->create();
} else {
$data['userfile'] = $upload['upload_data']['file_name'];
$data['userfile_type'] = $upload['upload_data']['file_ext'];
$data['userfile_size'] = $upload['upload_data']['file_size'];
$this->db->insert('user', $data);
$this->session->set_flashdata('message', '<div class="alert alert-success alert">Data berhasil dibuat</div>');
redirect(site_url('auth/user'));
}
}
Use these functions to upload and resize your image
function imageUpload() {
$config = array(
'upload_path' => "uploads",
'allowed_types' => "*",
'overwrite' => true,
'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "3000",
'max_width' => "3000"
);
$this->upload->initialize($config);
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile')) {
$response = array('upload_data' => $this->upload->data());
$this->doResize($response['upload_data']['file_name']);
return $response;
} else {
$error = array('error' => $this->upload->display_errors());
return false;
// var_dump($error); die(); // check errors
}
}
function doResize($filename) {
$sourcePath = './assets/images/user/' . $filename;
$targetPath = './assets/images/user/thumb_' . $filename;
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $sourcePath,
'new_image' => $targetPath,
'maintain_ratio' => true,
'width' => 100,
'height' => 100
);
$this->image_lib->initialize($config_manip);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
// Check errors
echo $this->image_lib->display_errors();
die();
}
}
I am trying to upload files with Persian name like نام فایل but the file uploads and stores with unknown chars name like تقسیم_وظای٠it really stuck me I don't know what to do.
This is the controller code for uploading the file:
$files = Input::file('files');
$errors = "";
$file_data = array();
if(Input::hasFile('files'))
{
foreach($files as $file)
{
// validating each file.
$rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
$validator = Validator::make(
[
'file' => $file,
'extension' => Str::lower($file->getClientOriginalExtension())
],
[
'file' => 'required',
'extension' => 'required|in:jpg,jpeg,bmp,png,pdf,doc,docx,xls,xlsx,zip'
]
);
if($validator->passes())
{
// path is root/uploads
$destinationPath = 'uploads/docs/';
$filename = $file->getClientOriginalName();
$temp = explode(".", $filename);
$extension = end($temp);
$lastFileId = $object_id;
$lastFileId++;
$filename = $temp[0].'_'.$object_id.'.'.$extension;
$upload_success = $file->move($destinationPath, $filename);
if($upload_success)
{
$data = array(
'file_name' => $filename,
'meeting_id' => $object_id,
'user_id' => Auth::user()->id
);
//call the model function to insert the data into upload table.
meetingModel::uploadFiles($data);
}
else
{
// redirect back with errors.
return Redirect::back()->withErrors($validator);
}
}
else
{
// redirect back with errors.
return Redirect::back()->withErrors($validator);
}
}
}
Hello all im working on a admin system that can create news with a image but i cant find out how to send the image name from my model file to my controller,
this is my model file:
function uploadImg()
{
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000,
'encrypt_name' => true
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 200,
'height' => 200,
'encrypt_name' => true,
'max_size' => 2000
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
# Ret profil billed navn #
$file_array = $this->upload->data('file_name');
return $billed_sti['billed_sti'] = $file_array['file_name'];
//$this->db->where('username', $this->input->post('username'));
//$this->db->update('users', $profilBilledNavn);
}
This is my controller:
function opret() {
$this->form_validation->set_rules('overskrift', 'overskrift', 'required');
$this->form_validation->set_rules('description', 'description', 'required');
$this->form_validation->set_rules('indhold', 'indhold', 'required');
if($this->form_validation->run() == true)
{
$this->load->model('admin/nyheder_model');
$billed_sti = $this->nyheder_model->uploadImg();
$data = array(
'overskrift' => $this->input->post('overskrift'),
'description' => $this->input->post('description'),
'indhold' => $this->input->post('indhold'),
'billed_sti' => $billed_sti,
'brugernavn' => $this->session->userdata('username'),
'godkendt' => 'ja'
);
$this->db->insert('nyheder', $data);
redirect('admin/nyheder/index');
} else {
$this->index();
}
}
I do the image processing in the controller rather than the model.
"Models are PHP classes that are designed to work with information in your database."
from: http://codeigniter.com/user_guide/general/models.html
What you need to do is move the code for uploading the image to the controler.
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);
}
}
Once you did that,
You can insert the name of the file from the $data variable created in this line:
$data = array('upload_data' => $this->upload->data());
and you can get the value like this:
$data['file_name']
The file will upload the the folder you configured, and you will insert the filename to the DB From the controller.
I hope it helps.
Please use the upload function in your controller as the model classes are used to handle the database information. Please check the code below
//Controller Class
function upload_image()
{
//Check for the submit
// Submit Name refers to the name attribute on the submit input tag.
// $filename refers to the name attribute of the file input tag.
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$submit = $this->input->post('submit');
if($submit == "Submit Name")
{
//Load the relevant classes and libraries
$this->load->library('upload');
$this->load->model('admin/nyheder_model','nmodel');
$filename = "image_file";
//Define the config array
$config = array();
$config['upload_path'] = $this->gallery_path;
$config['allowed_types'] = "jpg|gif|png";
$config['max_size'] = 0; //0 is for no limit
$this->upload->initalize($config);
if(!$this->upload->do_upload("$filename"))
{
echo $this->upload->display_errors();
}
else
{
$file_data = $this->upload->data();
$filename_1 = $file_data['file_name'];
$insert_array = array('filename'=>"$filename_1");
$this->nmodel->insert_data($insert_array);
} // end of the else statement
} // end of the isset statement
} // end of the outer conditional statement
Now you have the value of the filename in the $filename_1 variable which you can pass to the model class and can store the value in the database.
Thanks
J