onaftersave running before file upload finishes

edited December 2011 in Feature Requests
Hi,
I'm trying to run a getfilesize on an uploaded image, by putting it in the onaftersave() hook of my module to store the width and height in the database. I'm running something like:

$size = getimagesize(realpath(dirname($_SERVER['SCRIPT_FILENAME'])) . '/assets/images/content/' . $new_film_image);

However this generates a "failed to open stream" error when I try and run it. I know the path is right--if I run it again with the image already there, it works. This is probably outside the scope of fuel, but I was wondering if you had any suggestions?
Thanks,
Jordan

Comments

  • edited 2:12PM
    Try using the on_after_post hook on your model instead.
  • edited 2:12PM
    That's what I was looking for. You rock! Thanks!
  • edited 2:12PM
    Hi,
    This code was running fine except for creating new rows. I used $this->db-> insert_id() to get the last row inserted or updated, and then tried to run another update query. insert_id() would work on both inserts and updates (saves). However both fuel's update() method and codeigniter's active record update() method did weird things inside the on_after_post hook. Even though I would only pass two fields (width and height) in an array to update(), the update query was updating every column in the table (and bizarrely setting the row id to blank, even though I set auto_increment to false in this model). Does on_after_post run in the middle of an active record query? Anyway I was able to fix this with a manual db query.
    Thanks,
    Jordan
  • edited 2:12PM
    What was the where condition used in the update statement or better yet, can you post what you put into the on_after_save method?
  • edited 2:12PM
    The where code was basically result_id = $this->db->insert_id(), result_id being my primary key in my quiz_answers table. For on_after_save I just had a blank override method in case I would use it later.
    function on_after_save($values) { $CI =& get_instance(); return $values; }
    This is what I was trying in on_after_post:
    function on_after_post($values) { $id = $this->db->insert_id(); if(isset($id) && $id != "") { $CI =& get_instance(); $image = (!empty($this->normalized_save_data['image'])) ? $this->normalized_save_data['image'] : ""; if(!empty($image)) { $extensions = array(".JPG",".jpg",".GIF",".gif",".PNG",".png",".JPEG",".jpeg"); $size = array(); $size = @getimagesize(realpath(dirname($_SERVER['SCRIPT_FILENAME'])) . '/assets/images/content/games/' . $image); //$data2 = array('width' => @$size[0], 'height' => @$size[1] ); //$this->update($data2, "result_id = '$id'"); //this updates EVERYTHING and sets result_id = '' //this activerecord call also failed: //$this->db->where("result_id = '$id'"); //$this->db->update('quiz_results', $data2); //so I had to do this: $query = $this->db->query("UPDATE quiz_results SET width = '" . @$size[0] . "', height = '" . @$size[1] . "' WHERE result_id = '$id'"); } } }
    Thanks,
    Jordan
  • edited 2:12PM
    Try getting the id in the first line using this:
    $id = $values['id'];
Sign In or Register to comment.