Welcome Guest, Not a member yet? Register   Sign In
Image upload problem
#1

[eluser]haris244808[/eluser]
Hi there...

i am trying to update an image upload together with some other info...

If other fileds(ex: name, surname) are posted and the image is not posted...i just want to update(insert) those fields only to database...
if the image is uploaded...i want to delete the old image from the folder together with the image name from the database and update the new one

here is what i have done so far: (i made some explanations through php comments also)
it works well....but i want that when i dont choose an image...not to retrieve the old image name from another query...is there any other solution...
A solution that dont require a query to retrieve the old name...any other solution that will not overlap the performance...
Code:
$picture = 'profile_pic';

  $users = $this->users_model->select_user_by_id($user_id);
  
  foreach ($users as $results) {
   $pic_name = $results->profile_pic; //here i get the old pictures name
  }

  if($this->form_validation->run() == TRUE){

  if($_FILES[$picture]['name'] && $_FILES[$picture]['size'] > 0 ){ //here i check if the Image is uploaded or not

    $picture_path = FCPATH.'uploads/profile_pictures/original/'.$pic_name; //retrieve old image path
    $thumb_path = FCPATH.'uploads/profile_pictures/171x214/'.$pic_name;

    unlink($picture_path); //delete old image from the path
    unlink($thumb_path);

    $config['upload_path'] = FCPATH.'uploads/profile_pictures/original/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
     //when you change this, the upload_max_filesize in php.ini file must be changed too
    $config['max_size'] = '2000';
    $config['remove_spaces'] = TRUE;
    
    $this->load->library('upload', $config);
    if($this->upload->do_upload($picture)){ //if upload of picture is successfull

     $data = $this->upload->data(); //get all the uploads file data(name, path...)

     $image = $data['full_path'];

     $this->load->model('images_model'); //load images_model

     if ($this->images_model->image_resize($image)  == TRUE) { //resize image
      
      $profile_pic_name = $data['file_name']; //get the name of the picture

     }
    }
   }  

   $pic = $profile_pic_name ? $profile_pic_name : $pic_name; //if $profile_pic_name (which is new name) is set assign that to $pic otherwise assign the old picture name to $pic

   $user_query['user_data'] = $this->users_model->update_user($user_id, $pic);//pass picture name to update in database

#2

[eluser]TheFuzzy0ne[/eluser]
The way you're doing it is going to be problematic. It assumes that:

a) A user will always upload a profile picture with the same name as the file they uploaded before, and
b) A user will never upload an image with the same name as another user's profile image.

You could name the file using the user's username or ID. That way you should be able to delete the previous file without ascertaining the file name first (since I assume it will be in the user's session), although I don't see why it would be a problem to perform an extra database query.
#3

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1363956515"]The way you're doing it is going to be problematic. It assumes that:

a) A user will always upload a profile picture with the same name as the file they uploaded before, and
b) A user will never upload an image with the same name as another user's profile image.

You could name the file using the user's username or ID. That way you should be able to delete the previous file without ascertaining the file name first (since I assume it will be in the user's session), although I don't see why it would be a problem to perform an extra database query.[/quote]

a)Actually...if i upload another picture...it stores the new name in database not the old pictures name...
b) Thats ok...why do i need to upload an image with the same name as another users profile image?

#4

[eluser]TheFuzzy0ne[/eluser]
Sorry, I thought that the upload library overwrites existing images by default. It turns out that it doesn't. Even still, my idea should still work. Just use the user's ID for the image name, which you should be storing in the session anyway. Then you know what to delete, you just need to figure out the extension.

If you only want the query to execute if an image has been uploaded, then why are you executing it before you perform validation? Also, if you're only expecting a single result (since you're grabbing a user by their ID), why are you looping through the results? That should only be returning a single result, or FALSE. I assume this is as obvious to you as it is to me, so perhaps I'm misunderstanding the question?
#5

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1363961677"]Sorry, I thought that the upload library overwrites existing images by default. It turns out that it doesn't. Even still, my idea should still work. Just use the user's ID for the image name, which you should be storing in the session anyway. Then you know what to delete, you just need to figure out the extension.

If you only want the query to execute if an image has been uploaded, then why are you executing it before you perform validation? Also, if you're only expecting a single result (since you're grabbing a user by their ID), why are you looping through the results? That should only be returning a single result, or FALSE. I assume this is as obvious to you as it is to me, so perhaps I'm misunderstanding the question?[/quote]

im looping through each result because i return an array:

here is the model i user to return the results:
Code:
function select_user_by_id($id){

  $sql = "SELECT * FROM users WHERE id = ?";
  $user_query = $this->db->query($sql, array($id));

  if ($user_query->num_rows() == 1) {

   return $user_query->result();
  }
  else{

   return false;
  }
}

so to get only the picture name i have to loop throug results in controller...

if you know a better and faster way, pls tell me... i will appriciate that Smile
#6

[eluser]TheFuzzy0ne[/eluser]
Sure.

Code:
// Set a default to prevent an error being thrown if no ID is passed.
function select_user_by_id($id = 0)
{
    // Always use single quotes if you don't need to parse variables within
    // the string. It gives a performance boost.
    $q = $this->db->query(
        'SELECT * FROM users WHERE id = ?',
        array($id),
    );

    // Ta-daaa! Now the query returns a single object, instead of an array
    // of the little buggers...
    return ($q->num_rows() == 1) ? $q->row() : false;
}

Hope this helps.
#7

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1363983199"]Sure.

Code:
// Set a default to prevent an error being thrown if no ID is passed.
function select_user_by_id($id = 0)
{
    // Always use single quotes if you don't need to parse variables within
    // the string. It gives a performance boost.
    $q = $this->db->query(
        'SELECT * FROM users WHERE id = ?',
        array($id),
    );

    // Ta-daaa! Now the query returns a single object, instead of an array
    // of the little buggers...
    return ($q->num_rows() == 1) ? $q->row() : false;
}

Hope this helps.[/quote]

and how to retreieve a specific field from returned row...? (ex; i want to retrieve from the controller only 'pic' field from the returned row )

THnx btw
#8

[eluser]TheFuzzy0ne[/eluser]
Instead of:
Code:
$users = $this->users_model->select_user_by_id($user_id);
foreach ($users as $results) {
    $pic_name = $results->profile_pic; //here i get the old pictures name
}

You'd just do:
Code:
$user = $this->users_model->select_user_by_id($user_id);
$pic_name = $user->profile_pic; //here i get the old pictures name

If you don't know how to access the property of an object, you could do with brushing up on it. We're here to help with CodeIgniter, not to teach you PHP. There are lots of good decent PHP tutorials available online.

http://lmgtfy.com/?q=php+tutorial
#9

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364027929"]Instead of:
Code:
$users = $this->users_model->select_user_by_id($user_id);
foreach ($users as $results) {
    $pic_name = $results->profile_pic; //here i get the old pictures name
}

You'd just do:
Code:
$user = $this->users_model->select_user_by_id($user_id);
$pic_name = $user->profile_pic; //here i get the old pictures name

If you don't know how to access the property of an object, you could do with brushing up on it. We're here to help with CodeIgniter, not to teach you PHP. There are lots of good decent PHP tutorials available online.

http://lmgtfy.com/?q=php+tutorial[/quote]

i know how to access the property od an object...and i tried that but i didnt work because i frogot to tell that i call
Code:
$this->users_model->select_user_by_id($user_id);
in other places too and i was passing the results to the view by;
Code:
$data['users_query'] = $this->users_model->select_user_by_id($user_id); $this->load->view('some_page', $data);

so now all those places show mee that i am : Trying to get property of non-object...it is probably because i am storing in an array variable...i tried to store in a variable but still doesnt work...

how to solve this too??
#10

[eluser]TheFuzzy0ne[/eluser]
Again, your model is now passing back a single object, and not an array of objects, so now, instead of looping through an array, you can work with the object directly. If this is what you're doing, and you're still having trouble, then you should be checking to see if the model is returning false, and acting accordingly.

If you're still having trouble, I need to see what is in your views and controllers where you're getting this error.




Theme © iAndrew 2016 - Forum software by © MyBB