Welcome Guest, Not a member yet? Register   Sign In
Edit Uploaded image and edit the image file name
#1

[eluser]Neoraj3.0[/eluser]
Hi I am trying to do a mysql update to the image name (the actually file upload to the folder works fine) but the name does not edit. Here is my code:

Model::
Code:
function update_project($projectID, $data)
  {
  $this->db->where('projectid', $projectID);
  $this->db->update('projects', $data);

  }

Controller::
Code:
function editprojectimage($projectID)  //edit project page

{    

  //get project by id number
  $data['indiv_project'] = $this->project->single_project($projectID);

  $this->load->view('templates/header');
  $this->load->view('edit-project-image', $data);
  $this->load->view('templates/footer');
}

function editprojectimage_pro($projectID)  
{

  $config['upload_path'] = './assets/images/projectImages';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '2000';


  $this->load->library('upload', $config);

  if ( ! $this->upload->do_upload())
  {
   //check for errors with the upload
   $error = array('error' => $this->upload->display_errors());
   $this->load->view('templates/header');
   $this->load->view('edit-project-image');
   $this->load->view('templates/footer');
  }
  else
  {
   //upload the new image
   $upload_data = $this->upload->data();
   $image_name = $upload_data['file_name'];

   if($_POST){

   $data = array(
    'image'=>$image_name
    
   );

                        //update
   $this->project->update_project($data);
   }
  
   redirect(base_url().'projects/allprojects');

  }

}


View::
Code:
<form class="form-horizontal form-register" enctype="multipart/form-data" action="<?=base_url()?>projects/editprojectimage_pro/<?=$indiv_project['projectid']?>" method="post">

        <h2 class="form-signin-heading">Edit Project Image</h2>      

        &lt;!-- Text input--&gt;
        <div class="form-group">
          <label class="col-md-4 control-label lbl-adj" for="textinput">Project Image Cover</label>  
          <div class="col-md-4">
                  
                    &lt;!-- displays the current image in the upload button  --&gt;
                    <button id="btnfile">
                      <img src="&lt;?=base_url()?&gt;assets/images/projectImages/&lt;?= $indiv_project['image'] ?&gt;" alt="Image" class="media-object img-rounded thumb48"  width="42" height="42">
                    </button>
                    &lt;input type="file" id="uplodfile" name="userfile" /&gt;
          </div>
        </div>
  
    

        <button class="btn btn-lg btn-primary" type="submit">Edit Image</button>

        &lt;/form&gt;
#2

[eluser]InsiteFX[/eluser]
Code:
$data = $this->upload->data();

// array data contains
Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

If you take a look at the ./system/libraries/upload.php you will see that all the variables are public so you can change the ones you need to.

But on a upload everything is stored in a $_FILES[$field] array.

#3

[eluser]Neoraj3.0[/eluser]
Thanks I saw the file but I am not sure if I need to change anything in upload.php because in my example I am using "file_name" in the controller and placing it into a variable $image_name which should then update the database. Am I missing something?

Code:
//upload the new image
   $upload_data = $this->upload->data();
   $image_name = $upload_data['file_name'];


   if($_POST){

  $data = array(
    'image'=>$image_name
    
   );

            //update the new post
    $this->project->update_project($data);
  }
#4

[eluser]Neoraj3.0[/eluser]
Ok I figured out my 2 codeigniter noob errors so now it works (minus some good old fashion upload errorchecking):

1) I did not specify the ID so:

Code:
$this->project->update_project($data);

became:

Code:
$this->project->update_project($projectID, $data);


2) the redirect came to soon on the logic and was messing up the update:

Code:
redirect(base_url().'projects/allprojects');

//this is now below the last load view footer




Theme © iAndrew 2016 - Forum software by © MyBB