Welcome Guest, Not a member yet? Register   Sign In
Problem with uploading an image ?
#1

[eluser]Madtrooper[/eluser]
Hey guys!

I have another question Smile

I'm trying to upload an image, and all works well (using the user guide - file uploading class). Except for 1 thing. I set the following config:

Code:
$config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1024';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

And indeed, when I upload an image that is within these limits, it uploads the file to my upload folder. When it exceeds one or more of these limits, it does not get uploaded in the upload folder, awesome!

BUT :O

When I do the following:

Code:
if ( ! $this->upload->do_upload('screenshot'))
{
$error = array('error' => $this->upload->display_errors());
return $error;
}
else
{
$data = array('upload_data' => $this->upload->data());
return TRUE;
}

It always returns true... even when the file is too big & doesnt get uploaded, it still returns true. So I never know when an error has occured.

Any ideas? Is this a known bug? Or am I just being stupid?

Thanks!


#2

[eluser]Madtrooper[/eluser]
I found the problem.

It seems it's not a good idea to return a boolean or an array in the same function.

when checking the return value of do_upload, it was always true because even tho the array with errors got returned, it still was seen as 'true'.
#3

[eluser]boltsabre[/eluser]
yes, when checking return values, if you can return an array or boolean, you want to check it with the triple equals sign ===
It means same datatype (ie, boolean) AND same value. So you could do it like this

Code:
if($functionReturnValue === true){
    // your return value was a boolean AND is true
}elseif(is_array($functionReturnValue)){
   // your return value was NOT a true boolean, but is an array
}else{
   // return value was NOT a true boolean, was NOT an array, but something else, perhaps a boolean false...
}

Just checking "$functionReturnValue == true" is basically just saying: "if $functionReturnValue is set to any value"




Theme © iAndrew 2016 - Forum software by © MyBB