Welcome Guest, Not a member yet? Register   Sign In
uploading and using images
#34

[eluser]M4rc0[/eluser]
Ok so here i am Smile

Less talk more code this time, since i have lots to show.

I choose Imagination to upload (temp place) and rename and upload (correct place). I'm not using CI upload helper since that would be one extra unnecessary upload. So i had to add a function to rename the picture (random hash) and to display a validation error in case of invalid file type.

I'm not caring about max width or max height since imagination takes care of resizing it according to it's ratio (rocks). Size validation can be important but that can wait Smile
So i only care about file type now, since i was successfully adding with .zip files (or any other extension).

Here's my controller code:
Code:
function add()
{
if($this->validation->run() == FALSE OR $this->resize_img($_FILES['cover']['name'], $_FILES['cover']['tmp_name'],'public/images/covers/') == FALSE)
         {
            $this->template->write_view('content','movie/add');
            $this->template->render();
         }
         else
         {
            $this->success();
           }        
}

function resize_img($name,$path,$dest=FALSE)
    {        
        $this->load->library('imagination');    
        if($image->valid == TRUE){
            $image->resize(400,400);
            return $image->new_name; //to add on the db
        }else{ return FALSE; } //means invalid file type
    }

Now the imagination changes:
I have on the top of the variables a "public $new_name;"

Added the function encrypt_name:
Code:
public function encrypt_name()
        {
            mt_srand();
            $this->new_name = md5(uniqid(mt_rand())).'.'.$this->type;

            return $this->new_name;            
        }

And on your resize function, there's an if in the beginning, i just added the else code:
Code:
if($this->valid)
{ //(...)
}else{$CI->validation->error_string = "Invalid file type.";}

So it works! Not perfectly but it works.

If i choose a file other then a valid image type, it shows the form again but without the "Invalid file type" error.
But at least it doesn't go Big Grin

My trouble is with $CI variable that i see you get with "$CI =& get_instance();"
I have never worked with that before, so i tought it would set on my controller (CI instance?) the validation error but it's not setting it. Maybe i have to use the return of the resize function for this. What you think?


Messages In This Thread
uploading and using images - by El Forum - 09-14-2008, 08:24 AM
uploading and using images - by El Forum - 09-14-2008, 07:36 PM
uploading and using images - by El Forum - 09-16-2008, 01:51 PM
uploading and using images - by El Forum - 09-16-2008, 05:38 PM
uploading and using images - by El Forum - 09-17-2008, 03:00 AM
uploading and using images - by El Forum - 09-17-2008, 09:24 AM
uploading and using images - by El Forum - 09-17-2008, 09:39 AM
uploading and using images - by El Forum - 09-17-2008, 10:11 AM
uploading and using images - by El Forum - 09-17-2008, 10:55 AM
uploading and using images - by El Forum - 09-17-2008, 11:05 AM
uploading and using images - by El Forum - 09-17-2008, 11:19 AM
uploading and using images - by El Forum - 09-17-2008, 11:42 AM
uploading and using images - by El Forum - 09-17-2008, 12:16 PM
uploading and using images - by El Forum - 09-18-2008, 11:28 AM
uploading and using images - by El Forum - 09-19-2008, 06:20 AM
uploading and using images - by El Forum - 09-19-2008, 06:36 AM
uploading and using images - by El Forum - 09-19-2008, 06:38 AM
uploading and using images - by El Forum - 09-19-2008, 07:00 AM
uploading and using images - by El Forum - 09-19-2008, 07:08 AM
uploading and using images - by El Forum - 09-19-2008, 07:09 AM
uploading and using images - by El Forum - 09-19-2008, 07:11 AM
uploading and using images - by El Forum - 09-19-2008, 07:12 AM
uploading and using images - by El Forum - 09-19-2008, 07:19 AM
uploading and using images - by El Forum - 09-19-2008, 07:21 AM
uploading and using images - by El Forum - 09-19-2008, 09:13 AM
uploading and using images - by El Forum - 09-20-2008, 12:54 PM
uploading and using images - by El Forum - 09-20-2008, 02:46 PM
uploading and using images - by El Forum - 09-20-2008, 03:11 PM
uploading and using images - by El Forum - 09-20-2008, 03:28 PM
uploading and using images - by El Forum - 09-20-2008, 03:59 PM
uploading and using images - by El Forum - 09-20-2008, 04:10 PM
uploading and using images - by El Forum - 09-20-2008, 04:32 PM
uploading and using images - by El Forum - 09-20-2008, 05:41 PM
uploading and using images - by El Forum - 09-22-2008, 01:35 PM
uploading and using images - by El Forum - 09-22-2008, 01:59 PM
uploading and using images - by El Forum - 09-22-2008, 02:01 PM
uploading and using images - by El Forum - 09-23-2008, 02:57 AM
uploading and using images - by El Forum - 09-23-2008, 03:02 AM
uploading and using images - by El Forum - 09-23-2008, 03:19 AM
uploading and using images - by El Forum - 09-23-2008, 03:24 AM
uploading and using images - by El Forum - 09-23-2008, 03:38 AM
uploading and using images - by El Forum - 09-23-2008, 03:47 AM
uploading and using images - by El Forum - 09-23-2008, 04:16 AM
uploading and using images - by El Forum - 09-23-2008, 04:21 AM
uploading and using images - by El Forum - 09-23-2008, 10:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB