How to Display Existing User Pictures In Codeigniter.? - php

I have a complete php script which i bought from codecanyon and the Author does not support costum work... it was made with Codeigniter framework and its working fine and perfectly but the script does not support
Ability for User to Upload Profile Image
Ability for uploaded image be shown in profile page.
3 Ability to ReUpload and or delete image.....
I have search google for it and could not find answers to this.
I have tried Creating >Upload-Controller > Upload View > Upload
Model, which only upload to a specified folder in a specific url e.g site.com/uploadimage.
I want to ask if there is any way to integrate this feature to the system... Am a php newbie.
Many Thank in Advance...

I hope this can help you.
Create this folder path for uploading the image : app_data/images/account/
// Your Controller
Class User extends CI_Controller
{
// This upload method
function upload_user_photo()
{
$path = 'app_data/images/account/';
if(!file_exists($path))
{
mkdir($path, 0777, true);
}
$file_name = increment_string('image_profile', '-'); //use codeigniter string helper
$config['upload_path'] = $path;
$config['file_name'] = $file_name;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
//Take an action if photo has been uploaded
if($this->upload->do_upload('image'))
{
$upload_data = $this->upload->data();
// View response from upload data
echo "<pre>";
print_r ($upload_data);
echo "</pre>";
// Array key is column name of your table
$data =
[
'user_id' => 'insert our user id',
'photo' => $upload_data['file_name']
];
//Take action save to your model
$this->load->model('your_model_name');
$this->your_model_name->save_profile_image($data);
}
}
}
#Model
Class Your_model_name extends CI_Model
{
function save_profile_image($data)
{
$this->db->insert($data);
}
}
Read more about codeigniter upload setting preferences

Related

Use do_upload function with hardcoded image location (Codeigniter)

My problem is following. I'm writing this test data seed file. I already have all functionalities wrote (creating thumbnalins, hd, watermarking img, etc...) so now I would like just to reuse this code, but with some hardcoded image data location.
$imgToUploadLocation = "../../static/img/test.jpg";
$_FILE['userfile'] = imgToUploadLocation;
$config['upload_path'] = '/pictures/';
$config['allowed_types'] = 'jpeg|jpg';
$config['remove_spaces'] = true;
$config['max_size'] = '25000';
$config['encrypt_name'] = true;
$this->upload->initialize($config);
if ($this->upload->do_upload()) {
echo "finaly!";
} else {
echo "error";
}
The problem is that however i specify my image location i will always get error you did not select any image to upload.
I wonder how to use upload library without view form? If you need any additional informations please let me know and I will provide. Thank you!
This is not the proper way of defining. Files are expected in a certain way on the $FILES global variable to be uploaded.
In order to define the file you want to upload do:
$_FILES['userfile']['name'] = 'what name you want';
$_FILES['userfile']['tmp_name'] = $imgToUploadLocation;
Read more about the $_FILES in https://secure.php.net/manual/en/features.file-upload.post-method.php

Upload File with Related Extension Filter

I have problems on my upload page. When I try to upload file, the extension that can be selected set by "all type", but I've tried to set my allowed type config to jpg. This is my controller :
public function upload_file($noArsip)
{
$config['upload_path'] = './assets/img/arsip/';
$config['allowed_types'] = 'jpg'; // --> how to make the allowed type only image extension?
$this->load->library('upload', $config);
$this->upload->do_upload('video');
$data_upload_files = $this->upload->data();
//proses database
$image = $data_upload_files['file_name'];
$data = array(
'video'=>$image
);
$this->daftar_arsip_m->updateData($noArsip,$data);
//$this->load->view("upload_foto_v", $data);
redirect('upload_foto');
}
i dont want to use validation when the file is selected, i just want it to filter the file for the related extension, like image or video related type. thanks for your help
Separate extensions by |
$config['allowed_types'] = 'gif|jpg|png';
PS: Extensions are not case sensitive, codeigniter will take care of Upper case formats.
Reference
To filter the browse window, just add accept="image/gif,image/jpeg,image/png" as attribute to the input tag.
Example:
<input type="file" accept="image/gif,image/jpeg,image/png">
Live demo

How to validate uploads as a group in CodeIgniter, so they all either upload or fail

Firstly, I'm not asking how to perform a multi-file upload using CodeIgniter, but rather how to prevent any/all files from being uploaded if the last one in line fails (due to filetype, size, etc.). Basically I'm looking to use CI's file validation up front (before anything is uploaded), and if everything looks good, perform all uploads.
I have a form that uploads two files. If the first file passes validation and the do_upload() method returns TRUE, but the second file fails, the first file was still uploaded -- how can I prevent this?
Here's my code:
// Controller
function upload_files()
{
// Configure first file upload
$config['upload_path'] = 'public/mp3/';
$config['overwrite'] = FALSE;
$config['allowed_types'] = 'mp3';
$config['max_size'] = '20000';
$config['remove_spaces'] = TRUE;
$this->upload->initialize($config);
// Perform first file upload
if ($this->upload->do_upload('mp3'))
{
// Store upload file info for later use
$mp3_info = $this->upload->data();
// Configure second file upload
$config['upload_path'] = 'public/doc/';
$config['overwrite'] = FALSE;
$config['allowed_types'] = 'doc|docx';
$config['max_size'] = '3000';
$config['remove_spaces'] = TRUE;
$this->upload->initialize($config);
// Perform second file upload
// Even if this fails, the first file was still uploaded
if ($this->upload->do_upload('doc'))
{
// Store upload file info for later use
$doc_info = $this->upload->data();
// Perform database interactions here
}
else
{
echo $this->upload->display_errors();
}
}
else
{
echo $this->upload->display_errors();
}
}
No dice! You can't validate an upload from php or codeigniter unless you execute the upload. How can php validate something it doesn't yet have access to?
You have to do some validation up-front with javascript. If all files pass the js validation, then upload the files and complete validation with php/codeigniter.

codeigniter: loading variables and view from within model

I'm building an image uploading script (for the first time) in Codeigniter and the way I have it is that if the image upload form gets validated it performs the following code in a model:
public function upload($id) //function to handle the initial image upload before crop
{
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_path'] = 'images/uploads/temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$file_data = $this->upload->data();
if ( ! $this->upload->do_upload() )//the default parameter of do_upload() is 'userfile' (meaning the name of the form input)
{
$error = '<p>Upload failed!</p> <p>Please make sure the image is in a .jpg, .gif or .png format and that it\'s no larger than 1028 x 768 pixels in dimension. Also it can\'t be more than 2MBs in size. If this problem persists, please contact webmaster#dreamsy.org.</p>';
$this->session->set_flashdata('error', $error);
redirect(base_url().'community/upload_image');
}
else
{
$image_path = base_url().'images/uploads/temp/'.$file_name;
//$data = array( 'upload_data' => $this->upload->data() );
//$this->load->view('community/upload_success');
$this->load->helper('form', 'url');
$vars['id'] = $this->uri->segment(3);
$vars['$image_path'] = $image_path;
$vars['$file_data'] = $file_data;
$this->load->vars($vars);
$this->template->write_view('content', 'community/upload_success');
$this->template->render();
}
}>template->render(); //template library
But when I call the variables that are loaded (via load->vars()) they don't get loaded and when the page loads I get "undefined variable" errors. I suspect it's not possible to pass variables from a model to the view even when the view is loaded from within the model as I have done above. Or maybe I'm just doing something incorrectly (as I'm a bit of a n00b).
Would you even take this route? Would it make more sense to pass the variables to the controller and then to load the view from the controller? Or something else I haven't considered at all? lol
Any help is greatly appreciated. Thanks in advance.
*edit:
I also have the $image_path variable inside of an
Would you even take this route? Would it make more sense to pass the variables to the controller and then to load the view from the controller? Or something else I haven't considered at all?
Yeah, generally speaking it is somewhat accepted to have your views talk directly to the model and vice versa but only when using a presentation layer. In the case of CI, I think it's best to have all the data loaded in from your controller to your view when you render the view like this:
$this->load->view('your-view', $data);
To pass the vars from the model to the controller you can call methods on your model like this:
// Your_Model is the class name of your model
// your_model is the variable name it gets injected into
$this->load->model('Your_Model','your_model');
$data['some_data'] = $this->your_model->get_some_data();
To define those getters in your model, you define a function like this:
function get_some_data()
{
return $this->some_data;
}
That way you create an API into your data, and you can have the getter retrieve whatever you will need in the format you will need it in.
Assuming the line:
$this->load->vars($vars);
is where you want to load your view, then it should be:
$this->load->view('nameofview', $vars);

Problem with Gallery Uploading

I've got an image uploader that doesn't upload images. I'm not receiving any errors and i've checked the folder permissions and even on 777 no images are getting uploaded.
You can see the code here: http://pastebin.com/gvH1dKh9
The value for gallery_path is /home/domain/public_html/assets/images/gallery
The value for gallery_path_url is http://domain.com/assets/images/gallery/
I have used this same code on another site with zero problems. I'm not sure what the problem is with this site?
Use
$this->load->library('upload', $config);
instead of
$this->upload->initialize($config);
In my case it was helpful
Models are intended for interaction with databases. Try moving your upload code into the controller, then if needed take the returned data ($this->upload->data();) and pass that to a model for insertion to a database.
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => '/uploads',
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$this->Gallery_model->insertImageData($image_data);
}
}
Try doing some error checking on your upload:
if(! $this->upload->do_upload('Filedata')){
echo $this->upload->display_errors();
}
$upload_info = $this->upload->data();
echo var_dump($upload_info);

Categories