I have a single form on a page. It has 5 text fields and 3 upload file fields. I need to write the text and file paths to a database. I have seen many examples online but most are to upload a single file or to upload multiple files from the same upload field.
I am new to CodeIgniter so code snippets would be very helpful.
Many thanks in advance.
Another suggestion would be:
function upload()
{
$config['upload_path'] = $path; //$path=any path you want to save the file to...
$config['allowed_types'] = 'gif|jpg|png|jpeg'; //this is the file types allowed
$config['max_size'] = '1024'; //max file size
$config['max_width'] = '1024';//if file type is image
$config['max_height'] = '768';//if file type is image
$this->load->library('upload', $config);
foreach($_FILES as $Key => $File)
{
if($File['size'] > 0)
{
if($this->upload->do_upload($Key))
{
$data = $this->upload->data();
echo $data['file_name'];
}
else
{
// throw error
echo $this->upload->display_errors();
}
}
}
}
This will automatically work for ALL file inputs you post and it doesn't care about the name or quantity :)
HOpe this helps
$config['upload_path'] = $path; //$path=any path you want to save the file to...
$config['allowed_types'] = 'gif|jpg|png|jpeg'; //this is the file types allowed
$config['max_size'] = '1024'; //max file size
$config['max_width'] = '1024';//if file type is image
$config['max_height'] = '768';//if file type is image
//etc config for file properties, you can check all of them out on website
now suppose you have 3 files, which you want to save as 1.jpg, 2.jpg,3.gif, which are uploaded through 3 input fields, pic1, pic2, pic3 here is what you do
for($ite=1;$ite<=3;$ite++){
if(!empty($_FILES["pic".$ite]["name"])){ //if file is present
$ext = pathinfo($_FILES['pic'.$ite]['name'], PATHINFO_EXTENSION); //get extension of file
$config["file_name"]="$ite.$ext"; //rename file to 1.jpg,2.jpg or 3.jpg, depending on file number and its extension
$this->upload->initialize($config); //upload library of codeigniter initialize function with config properties set earlier
if(!$this->upload->do_upload("pic".$ite)){
//error code
}
}
}
Related
I am using codeigniter file uploading class to upload files and images in my local folder.I am using the code below, it's working fine.
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|txt|sql';
$config['max_size'] = 8048000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('Emp_details_view', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
Now, my question is how to remove previously added file when uploading new one.
You can use unlink() function to delete your previous image
unlink('image-path/image-name');
first save your filename in a database table then when unlinking take that value from database and unlink. here uploads is the foldername .
unlink("uploads/".$val['image']);
$val['image'] contains imagename from db.
I want to upload .blend or .blender file using codeigniter...
$config['allowed_types'] = 'gif|jpg|jpeg|png|psd|xls|xlsx|xml|doc|docx|csv|mpeg|mpg|mp3|mpga|pdf';
$config['max_size'] = '5024';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($field)) {
echo 'error';
}else{
print_r($this->upload->data());
}
Which Mimes type i m used for blender file extension...
I want upload image file on server & want to stored image in database.
How can I store image file in database & also get the image file from database. Please help to solved this problem.
i have done this code in project..
but when uploading image on server upload_path not found.
In controller
$config['upload_path'] = base_url().'aplication/upload/'; // the uploaded images path
$config['allowed_types'] = 'jpg|jpeg|png';/*types of image extentions allowed to be uploaded*/
$config['max_size'] = '3048';// maximum file size that can be uploaded (2MB)
$this->load->library('upload',$config);
if ( ! is_dir($config['upload_path']) ) /* this checks to see if the file path is wrong or does not exist.*/
{
echo( $config['upload_path']);
die("THE UPLOAD DIRECTORY DOES NOT EXIST"); // error for invalid file path
$this->load->library('upload',$config); /* this loads codeigniters file upload library*/
}
if ( !$this->upload->do_upload() )
{
$error=array('error'=>$this->upload->display_errors());
//echo "UPLOAD ERROR ! ".$this->upload->display_errors(); //image error
// $this->load->view('main_view',$error);
}
else
{
$file_data=$this->upload->data();
$data['img']=base_url().'.application/upload/'.$file_data['file_name'];
echo("Image upload successfully..");
//================================================
// $this->load->model('insert_model');
// $this->insert_model->submit_image();
//==========================================
//$this->insert_model->insert_images($this->upload->data());
// $this->load->model('insert_model');
//$this->insert_model->submit_image();
}
That is quite easy. Just create image upload function once in your controller. Here is one function I wrote:
function image_upload($path, $size, $width, $height, $new_name, $ext)
{
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = $size;
$config['file_name'] = time() . $new_name . "." . $ext;
$config['max_width'] = $width;
$config['max_height'] = $height;
$this->load->library('upload', $config); //Load codeigniter's file upload library
if (!$this->upload->do_upload())
{
return false;
} else
{
return $config['file_name'];
}
}
Now in the action function of form where you have file input field call this function as follows do as follows:
if ($_FILES['userfile']['name'] != "")//Check if file was selected by the user
{
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);//Get the extension of file
$picture_name = $this->image_upload("your/upload/directory/", 10000, 10000, 100000, "test", $ext);
//Now save this $picture_name in database
}
else
{
print_r($this->upload->display_errors());//Display errors if file does not get uploaded successfully
}
Thats it.
Read This, then you will get how to upload file.
Now how to store it into DB.!
In controller use this :
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$img_data = array('upload_data' => $this->upload->data());
$full_url = "/uploads/".$img_data['upload_data']['file_name'];
$reply = $this->model_name->upload_image($full_url);
}
In model using upload_image($full_url) function store image name with path, so that you can get back that path whenever needed.
I am currently using codeigniter 2.0 for a project and I have a little problem with thumb creation. I intend to allow users to upload picture and have the picture renamed to their last name then have anoda function from within the upload function create a thumbnail then send it to another directory inside the image directory (I named it avatar). The code does this perfectly, but now I intend to encrypt the last name before changing the uploaded pic name. That is where the problem sets in. The upload is carried out the name is changed but a thumbnail is not created ... please can any one tell me what the problem is here is the code sample
function set_avatar()
{
/*check if user is even logged in */
if($this->session->userdata('user_id'))
{
$last_name=$this->session->userdata('last_name');//this value is encrypted
/*parse the useful user values to the data array for use on this page */
$data['user_id']= $user_id;
$data['email']= $email;
$data['last_name']= $last_name;
$data['first_name']= $first_name;
/*assign an empty value to the error key of the data array to avoid error on the view page*/
$data['error'] = "";
/*the directory for uploading the file the number represents a mixture of birth date and month
af means all files u1 upload 1 avts avatar ..jst a path to make the path undetectable*/
$dir = "./path/avatar";
/*preparing the config settings for upload */
$config['file_name'] = $last_name; // the new file name the image should have
$config['upload_path'] = $dir; // the directory the image should go to,already specified
$config['allowed_types'] = 'gif|jpg|png'; // the file types that are allowed
$config['max_size'] = '10000'; //the maximum image size
$config['max_width'] = '300'; //the maximum image width
$config['max_height'] = '230'; // the maximum image height
$this->load->library('upload', $config); // retrieving the upload library with the specified settings
/*using the do upload settings upload the file if it fails stop upload and show errors*/
if ( ! $this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
$this->load->view("user/set_avatar",$data);
}
/*else retrieve the upload data from the file uploaded*/
else
{
/*pass all the upload data to the $img_data array*/
$img_data = $this->upload->data();
/*the file(image) extension type png or gif or jpg*/
$img_ext = $img_data['file_ext'];
/*thumb name without extension*/
$thumb = $last_name;
/*thumb name with extension*/
$thumb_name = $thumb.$img_ext;
/*create thumbnail using the thumb nail function*/
$this->create_thumb($thumb_name);
/*record avatar details in the db*/
$avatar_array = array(
"user_id" => $user_id,
"avatar" => $thumb,
"extension" => $img_ext
);
$this->craft_set_user_details->set_avatar($avatar_array);
/*start creat session for img_name and ext*/
$image_session = array(
"img_name"=>$thumb,
"img_ext"=>$img_ext
);
$this->session->set_userdata($image_session);
/*redirect to home page*/
redirect('home','location');
}//end if else $this->upload->do_upload
}//end check if user is logged in ($this->session->userdata('user_id'))
else
{
redirect("index");
}//end if eles user is logged in ($this->session->userdata('user_id'))
}//end function set avatar
/* this is the create_thumb() function */
///start the create thumb function this is used in the set avatar function to create the avatar thumb
function create_thumb($thumb_name)
{
/*check if user is logged in*/
if($this->session->userdata('user_id'))
{
//set the path to were the image is
$dir = "./path/avatar";
//set the path to were the thumb should be sent
$new = "./path/avatar/thumbs";
//set the configuration to be used for image resizing
$config['image_library'] = 'gd2';
$config['source_image'] = $dir;
$config['new_image'] = $new;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 35;
$config['height'] = 50;
//load the image library for image reszing
$this->load->library('image_lib', $config);
//resize image
$this->image_lib->resize();
}//end if eles user is logged in ($this->session->userdata('user_id'))
//else redirect to the home page
else
{
redirect('index','location');
}
}//end function to create
Your source image is a directory. It should be a file path.
hi its my first time here. i really dont know what's wrong with my codes. iam trying to upload file to database yet when i click the upload button, an error would occur.. (object not found) hope someone can help me...
btw, heres my code snippet
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt|pdf';
$config['max_size'] = '5000';
$config['max_width'] = '2500';
$config['max_height'] = '2500';
$config['remove_spaces']= 'true';
$this->load->library('upload', $config);
$data= array('userfile' => $this->upload->do_upload());
//DEFINE POSTED FILE INTO VARIABLE
$name= $data['userfile']['name'];
$tmpname= $data['userfile']['tmpname'];
$type= $data['userfile']['type'];
$size= $data['userfile']['size'];
//OPEN FILE AND EXTRACT DATA /CONTENT FROM IT
$fp = fopen($tmpname, 'r');
$content= fread($fp, $size($tmpname));
$content= addslashes($content);
fclose($fp);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
}
else
{
$data = array('userfile' => $this->upload->data());
$this->load->database();
$ret=$this->db->insert($name, $type, $size, $content);
unlink($tmpname);
//$this->load->database();
//$this->load->view('upload', $data);
}
}
The $this->db->insert() method does not work this way. It takes two arguments: the first one is the table into which you want to insert your data, the second is an array containing your data.
In your case, you should first put your file's data into an array :
$file_data=array('name'=>$name,'type'=>$type,'size'=>$size,'content'=>$content)
and then insert that into the appropriate table. I used files as an example. Use the one you actually need.
$ret=$this->db->insert('files',$file_data);
Please note though that except in some rare cases (file writing forbidden, etc...) it is generally a better idea to save files as ... files