I'll try to upload an Images into two different path, but it always uploaded in single path only, here's my code in a model :
public function save()
{
$post = $this->input->post();
$this->product_id = uniqid();
$this->name = $post["name"];
$this->price_awal = $post["price_awal"];
$this->price = $post["price"];
$this->image = $this->_uploadImage();
$this->image2 = $this->_uploadImage2('./upload/');
$this->jenis_produk = $post["jenis_produk"];
$this->warna = $post["warna"];
$this->description = $post["description"];
$this->db->insert($this->_table, $this);
}
The upload function (also in the model)
private function _uploadImage()
{
$config['upload_path'] = './upload/product/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
private function _uploadImage2($path)
{
$config['upload_path'] = $path . '/' . $this->product_id;
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
if (!is_dir($path . $this->product_id)) {
mkdir($path . $this->product_id, 0777, TRUE);
}
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
Only the _uploadImage function worked, but the _uploadImage2 not work
Can you tell me what's wrong?
The problem is that you have already initialize the upload library, so you have to re-initialize it.
Plus you should try to nest the functions, so they will be called in chain, or you don't want just use $this->upload->initialize($config); in the second function:
private function _uploadImage($path)
{
$config['upload_path'] = './upload/product/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->_uploadImage2($path);
}
return "default.jpg";
}
private function _uploadImage2($path)
{
$config['upload_path'] = $path . '/' . $this->product_id;
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
if (!is_dir($path . $this->product_id)) {
mkdir($path . $this->product_id, 0777, TRUE);
}
$this->upload->initialize($config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
Related
I try to save the uploaded file in 2 paths, one inside a folder and contain the id and one outside the folder
Here's my code :
private function _uploadImage()
{
$config['upload_path'] = './upload/'.$this->product_id;
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
$config['upload_path'] = './upload/product';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
if (!is_dir('upload/'.$this->product_id)) {
mkdir('./upload/' . $this->product_id, 0777, TRUE);
}
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
When I run that code, the result is stucked
Can you help me what's wrong?
You set the upload_path key twice, which just overwrites itself. You'll need to process the upload wholly twice, with your two directories different.
Since you're using a private function, you could for example add a parameter that distinguishes the path
private function _uploadImage($path)
{
$config['upload_path'] = $path . '/' . $this->product_id;
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
if (!is_dir($path . $this->product_id)) {
mkdir($path . $this->product_id, 0777, TRUE);
}
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
Now when you call your function, just include the paths:
$this->_uploadImage('./upload');
$this->_uploadImage('./upload/product');
I want to replace/overwrite my image upload, which works. But the file have copied end up with another extension.
This is my controller
function add(){
if (isset($_POST['submit'])) {
$id = $this->input->post('nisn', TRUE);
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 1024;
$config['file_name'] = $id;
$config['overwrite'] = TRUE;
$this->load->library('upload', $
$this->upload->do_upload('userfile');
$upload = $this->upload->data();
print_r($upload);
die;
}
currently I'm working with codeigniter framework but I'm confused how to resize the image. I have followed the documentation but I'm still confused. This is my code
public function create(){
//Check login
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Create Post';
$data['categories'] = $this->BlogModel->get_categories();
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('content','content','required');
if($this->form_validation->run() === FALSE){
$this->load->view('frontend/header');
$this->load->view('frontend/navbar');
$this->load->view('frontend/blog/create',$data);
$this->load->view('frontend/footer');
} else{
//Upload Image
$config['upload_path'] = './assets/images/posts/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->BlogModel->create_post($post_image);
//set message
$this->session->set_flashdata('post_created','Your post has been created');
redirect('blog');
}
}
Please advice how to resize the image.
Thanks!
If you want to do different things like resize the original image directly, rather than creating a thumb set create_thumb to false, and do not use new_image. The only difference between create_thumb and new_image is that new_image allows you to specify your own path and name. Whereas the most you can do with create_thumb is change the thumb_marker. Which is all in the docs...
//Upload Image
$config['upload_path'] = './assets/images/posts/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
//$errors = array('error' => $this->upload->display_errors());
exit($this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$up_data = $this->upload->data();
$post_image = $up_data['name'];
$config = array(); // clear prev config
$config['image_library'] = 'gd2';
$config['source_image'] = $up_data['full_path'];
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_thumbnail';
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) {
exit($this->image_lib->display_errors());
}
$thumb_path = $up_data['file_path'] . $up_data['raw_name'] . $config['thumb_marker'] . $up_data['file_ext'];
}
The exits you can remove later, but leave them for debugging until you figure out a way to properly message your errors (I suggest session vars). These functions should also be moved to a model.
I have a problem with my multiple upload with codeigniter,
using image
another using pdf
When I uploaded the file uploaded twice and how to call different path to uploaded to database. this my code
Controller
public function upload(){
$catalog='catalog';
$userfile='userfile';
//for cover
$config['upload_path'] = './file/book/'; //Use relative or absolute path
$config['allowed_types'] = 'gif|jpg|png|';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$c=$this->upload->do_upload($userfile);
//for catalog
$config['upload_path'] = './file/book/pdf/'; //Use relative or absolute path
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$cat=$this->upload->do_upload($catalog);
if(!$this->upload->do_upload($catalog) && $this->upload->do_upload($userfile)){
$error = array('error' => $this->upload->display_errors());
$this->load->view('uploadds', $error);
}else{
$this->load->model("book_model");
$this->book_model->addnewbook();
redirect('book/book');
}
}
This model
function addnewbook(){
$fcat=array('upload_data' => $this->upload->data($userfile));
$fcatalog=array('upload_dataa' => $this->upload->data($catalog));
}
You need to handle multiple uploads independently. For this, you have to create separate custom objects for both uploads while loading the upload library. (See the code comments)
public function upload() {
// Cover upload
$config = array();
$config['upload_path'] = './file/book/';
$config['allowed_types'] = 'gif|jpg|png|';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config, 'coverupload'); // Create custom object for cover upload
$this->coverupload->initialize($config);
$upload_cover = $this->coverupload->do_upload('cover');
// Catalog upload
$config = array();
$config['upload_path'] = './file/book/pdf/';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->load->library('upload', $config, 'catalogupload'); // Create custom object for catalog upload
$this->catalogupload->initialize($config);
$upload_catalog = $this->catalogupload->do_upload('catalog');
// Check uploads success
if ($upload_cover && $upload_catalog) {
// Both Upload Success
// Data of your cover file
$cover_data = $this->coverupload->data();
print_r($cover_data);
// Data of your catalog file
$catlog_data = $this->catalogupload->data();
print_r($catlog_data);
} else {
// Error Occured in one of the uploads
echo 'Cover upload Error : ' . $this->coverupload->display_errors() . '<br/>';
echo 'Catlog upload Error : ' . $this->catalogupload->display_errors() . '<br/>';
}
}
Use the data on $cover_data['full_path'] and $catlog_data['full_path'] to update your database
$config['upload_path'] = 'frontend_assets/images/hospital';
$config['allowed_types'] = 'gif|jpg|png|jpeg|JPEG||JPG|PNG';
$this->load->library('upload', $config);
if($_FILES['logo']['name']){
$config['file_name'] = time() . $_FILES["logo"]['name'];
if (!$this->upload->do_upload('logo')) {
$this->upload->display_errors();
} else {
$upload = $this->upload->data();
$insert_data['logo'] = $config['upload_path'] . '/' . $upload['file_name'];
}
}
if($_FILES['bulding_photo']['name']){
$config['file_name'] = time() . $_FILES["bulding_photo"]['name'];
if (!$this->upload->do_upload('bulding_photo')) {
$this->upload->display_errors());
} else {
$upload = $this->upload->data();
$insert_data['bulding_photo'] = $config['upload_path'] . '/' . $upload['file_name'];
$this->image_size_fix($insert_data['bulding_photo'], $width = 200, $height = 200);
}
}
I have the below code but I am unsure how I can do the upload errors due to having more then one $this->do_upload
$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';
$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);
$concepts = array('conceptOne','conceptTwo');
$logo = $folderName.'_logo';
if(!is_dir($location.$folderName))
{
mkdir($location.$folderName);
chmod($location.$folderName, 0777);
}
//Set File Settings
$config['upload_path'] = $location.$folderName;
$config['allowed_types'] = 'jpg|png|pdf';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
// Upload Concepts
foreach($concepts as $concept)
{
$config['file_name'] = $concept;
$this->upload->initialize($config);
$this->upload->do_upload($concept);
}
//Upload Logo
$config['file_name'] = $logo;
$this->upload->initialize($config);
$this->upload->do_upload('logo');
// Upload Concepts
foreach($concepts as $concept)
{
$config['file_name'] = $concept;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($concept))
{
$error = array('error' => $this->upload->display_errors());
break;
}
}