Welcome Guest, Not a member yet? Register   Sign In
File uploading: Optional fields?
#1

[eluser]RobbieL[/eluser]
When using the file uploading class in CI, is it possible to to set a field to be optional, and not required, like you can with the validation class?

I have a multiform file uploader than is working a treat, apart from the fact that it keeps spitting out "You did not select a file" error. I'd like the user to have the option of how many files they upload.

Any feedback would be greatly appreciated ... I'm going bald over this. Smile
#2

[eluser]LuckyFella73[/eluser]
Hi,

in your controller you can check if the user has selected
a file - for example:
Code:
if ( isset($_FILES['userfile_0']) AND strlen($_FILES['userfile_0']['name'])>0 )
{
# ok, the user has selected a file (input fild with name="userfile_0")
$config['upload_path'] = 'abc';
$config['allowed_types'] = 'abc';
$config['max_size']    = 'abc';

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

if ( ! $this->upload->do_upload($fieldname="userfile_0") )
{
# somthing wrong
}else
# file has beeing uploaded
}
}

You can extend this depending on how many files the user
is able to choose. So you would go on with "userfile_1",
"userfile_2" and so on.
#3

[eluser]RobbieL[/eluser]
Big apologies on the late reply. Finally managed to grab a spare minute to try out your example LuckyFella73, and it worked a treat! Huge thanks!

I had something similar before hand, but my if statement wrapped around the $this->upload->do_upload() statement. With the addition of a foreach loop and your example, it came out perfect.

Thanks again! Smile
#4

[eluser]LuckyFella73[/eluser]
no worries about replying late and nice to
hear that you got your script to work
like meant to!




Theme © iAndrew 2016 - Forum software by © MyBB