Database Reference Image Upload Wrong filename stored - php

I am teaching myself codeigniter so I used the image uploading class with storing the name in SQL and storing the image on a folder in the server. When you call the name from the server it will display from the file.
So that works fine, but I was curious on overwriting images so I checked the codeignter documentation on uploading files and they have a encrypt_file file function. So that would fix a overwrite issue, well now since I used it the file name that is stored in my database is the original file while, the image that is stored in my folder on the server has a encrypted file name, so there for the photo does no display.
I have went through codeigniter's documentation, trying to load another filename variable and so on. I tried also initializing the config each time also.
The controller the Library Upload is auto.
if($this->form_validation->run()=== FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}
else{
//Upload image
$config['upload_path'] = 'assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE; //TURN ON
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->upload->initialize($config);
if(!$this->upload->do_upload('userfile')){
$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->Post_Model->create_post($post_image);
redirect('http://localhost/CI/');
}
}
MODEL
public function create_post($post_image){
$slug = url_title($this->input->post('title'));
$data=array('title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'category_id' => $this->input->post('category_id'),
'post_image' =>$post_image
);
form
<img src="assets/images/posts/<?php echo $post['post_image'];?>">

First, Add the following configuration line of code to avoide overwrite $config['overwrite'] = false
Second, Give actual values to max_size, max_width and max_height not zero
$config['max_size'] = 1000;
$config['max_width'] = 430;
$config['max_height'] = 430;
$config['overwrite'] = false;
And where the file is uploading do some changes
if(!$this->upload->do_upload('userfile')){
$errors = array('error'=>$this->upload->display_errors());
$post_image = 'noimage.jpg';
}else {
$data = $this->upload->data();
$post_image = $data['file_name'];
}

Related

adding timestamp while uploading image file is not working

I am developing a ad site that anyone able to upload 3 images.
So I am coding to upload that 3 images by adding timestamp in front of their file name to make them unique. I use codeigniter framework and attaching the code below.
when I submitting form data, localhost server shows that images have been saved correctly with some code infornt of image filename. But problem is there are no images saved in relevant image folder in that name and I cannot retrieve that images in next preview advertisement page.
I don't know much about php or codeignitor. Your help is highly appreciated.
$config['upload_path'] = './assets/images/adsimages';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 5120;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if (!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'no_image.jpg';
}
else {
$data = array('upload_data' => $this->upload->data());
$post_image1 = time().$_FILES['userfile1']['name'];
$post_image2 = time().$_FILES['userfile2']['name'];
$post_image3 = time().$_FILES['userfile3']['name'];
}
$result = $this->post_model->adregister($post_image1,$post_image2,$post_image3);
You can append time to 'filename' in config of upload library
$config['file_name'] = time().$_FILES['userfile1']['name'];
Or if you want unique name for all files the simply add
$config['encrypt_name'] = TRUE;
then
$this->load->library('upload', $config);
Try this one:-
$path = pathinfo($_FILES["userfile1"]["name"]);
$image_path = $path['filename'].'_'.time().'.'.$path['extension'];
I've written a possible solution for your code. You haven't shared your full code so you'll have to fill in the gaps yourself and maybe make some changes here and there; Comments are mentioned wherever necessary. See if it helps you.
public function your_function_name(){
// your-code
// your-code
// check if file is uploaded in field1
if(!empty($_FILES['userfile1']['name'])){
// call function to upload file
$userfile1 = $this->upload_file('userfile1');
}
// check if file is uploaded in field2
if(!empty($_FILES['userfile2']['name'])){
$userfile2 = $this->upload_file('userfile2');
}
// check if file is uploaded in field3
if(!empty($_FILES['userfile3']['name'])){
$userfile3 = $this->upload_file('userfile3');
}
$result = $this->post_model->adregister($userfile1, $userfile2, $userfile3);
}
// function to upload file
function upload_file($filename){
$config['file_name'] = time().$_FILES[$filename]['name']; // append time to filename
$config['upload_path'] = './assets/images/adsimages';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';
$config['max_size'] = 5120;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$uploaded = $this->upload->do_upload($filename);
if ( ! $uploaded ){
$error = array('error' => $this->upload->display_errors());
$file = 'no_image.jpg'; // default file
}else{
$upload_data = $this->upload->data();
$file = $upload_data['file_name']; // uploaded file name
}
return $file; // return filename
}

How to encrypt image name and save into database?

I can save the image name into database without any problem but when I try to encrypt the name only the image path gets the encrypted name but the database not
public function fileUpload(){
$clientes = $this->pm->getAllProducts();
foreach ($clientes as $cliente) {
$teste = $cliente->id;
}
if(!empty($_FILES['file']['name'])){
// Set preference
$config['upload_path'] = "uploads/";
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '5024'; // max_size in kb
$config['encrypt_name'] = TRUE;
$config['file_name'] = $_FILES['file']['name'];
$this->upload->initialize($config);
//Load upload library
$this->load->library('upload',$config);
$data = array(
'pub_id' => $teste,
'url' => $_FILES['file']['name']
);
$this->pm->addProduct($data);
// File upload
if($this->upload->do_upload('file')){
// Get data about the file
$uploadData = $this->upload->data();
}
}
}
You can encrypt file name with config
$config['encrypt_name'] = TRUE;
Or
$file= uniqid().time().$_FILES["file"]['name'];
$config['file_name'] = $file;

image file not showing in upload folder

i have spend days trying to make image file i uploaded shows in its folder but when i upload an image file it goes to database but not displaying in folder i dont know why.
public function additems()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 106660;
$config['max_width'] = 1026664;
$config['max_height'] = 76668;
$this->load->library('upload', $config);
$this->load->view('templates/dashboard_head.php');
$this->load->view('admin/create_product');
$this->load->view('templates/admin_sidebar');
$this->load->view('templates/admin_footer');
$this->load->library('upload', $config);
if($this->input->post('submit'))
{
//get form's data and store in local varable
$name=$this->input->post('name');
$price=$this->input->post('price');
$description=$this->input->post('discription');
$code = $this->input->post('code');
$data = array('upload_data' => $this->upload->data());
$image = $_FILES['image']['name'];
//call saverecords method of Hello_Model and pass variables as parameter
$this->admin->insert_products($name, $price, $description,$code,$image);
echo "<script>
window.alert('added succesfully');
</script>";
}
The image gets assigned a temporary name on upload. You have to move it using something like this:
$uploadDirectory = './uploads';
move_uploaded_file( $_FILES['image']['tmp_name'], $uploadDirectory );

Codeigniter Upload Files/image only works when offline

I made a code to upload files on the web, when I was still using xampp, it always going well, but after I upload to hosting, file uploading not working..
here's my code :
MODEL (for updating database record)
function edit_logo_web()
{
$timestamp = date('YmdHi');
$img = $_FILES['userfile']['name'];
$logo = $timestamp."_".$img;
$data = array (
'logo' => $logo
);
$id = 1;
$this->db->where('site.id', $id);
if($this->upload->do_upload()){
$this->db->update('site', $data);}
else{
}
}
CONTROLLER (for uploading img/file to web directory)
function logo_web()
{
$timestamp = date('YmdHi');
$img = $_FILES['userfile']['name'];
$config['file_name'] = $timestamp."_".$img;
$config['upload_path'] ='asset/img/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1600';
$config['max_height'] = '1200';
$config['remove_spaces'] = FALSE;
$this->load->library('upload', $config);
$data = array('upload_data' => $this->upload->data());
$this->m_cmm_bimbel->edit_logo_web();
echo "<script>
alert('Success !!');
window.location.href='logo_web';
</script>";
}
it Does very well in xampp localserver..
not in the hostingserver..
Any tips?

codeigniter: How to get error or succeded data from do_upload function and display on same view

if a image is choosed then i want to upload that image and store filename to db else i want to store in db filename as "default_brand.png" and no file is going to upload . in following do_upload function if errors occured then i want to display that error on same view, if no error and image uploaded successfully i want to display success message. what changes i have to do for following code.
function index()
{
//some statements.
if (empty($_FILES['brand_image']['name'])) {
$filename = 'default_brand.png';
}
else {
$filename = $this->do_upload();
}
echo $filename;
}
function do_upload()
{
$config['upload_path'] = './assets/public_image/brand';
$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('brand_image'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->model('get_data_model');
$resultset = $this->get_data_model->get_only_child_categories();
$data['resultset'] = $resultset;
$this->layouts->view('brand_view', array('left_sidebar' => 'sidebar/left_sidebar','right_sidebar' => 'sidebar/right_sidebar'),$data,true);
return "default_brand.png";
}
else
{
$data = array('upload_data' => $this->upload->data());
$filename = $data['file_name'];
return $filename;
}
}
I have modified your original code to use a single function. It will load the index page on initial page load and will only execute the upload process if a user is actually posting data to the page (uploading a file).
I set the same variables for use within the view. You can use them to display success message or error. (This code is also assuming you have a custom "layouts" library you are using to render your views which it looks like that's what is going on. If not then that line will need to be changed to use codeigniter's default view rendering process)
function index(){
$post = $this->input->post();
$this->load->library('upload', $config);
$this->load->model('get_data_model');
if(!$post){ //your initial page load. user not submitting file since not posting data
$data = array(
'error' => FALSE,
'file_name' => '',
'resultset' => $this->get_data_model->get_only_child_categories()
);
$this->layouts->view('brand_view', array('left_sidebar' => 'sidebar/left_sidebar','right_sidebar' => 'sidebar/right_sidebar'),$data,true);
} else { //this is hit if user posting data (uploading file)
$config['upload_path'] = './assets/public_image/brand';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$data = array(
'error' => $this->upload->do_upload('brand_image') ? FALSE : $this->upload->display_errors('',''),
'file_name' => isset($upload_data['file_name']) ? $upload_data['file_name'] : 'default_brand.png',
'resultset' => $this->get_data_model->get_only_child_categories()
);
$this->layouts->view('brand_view', array('left_sidebar' => 'sidebar/left_sidebar','right_sidebar' => 'sidebar/right_sidebar'),$data,true);
}
}

Categories