更新时间:2023-01-09 12:28:49
您可以使用 codeigniter
upload
class在同一个表单中
$ config_logo_image = array(
'allowed_types'=>'jpg | jpeg | gif | png',
'upload_path'=> UPLOAD_PATH,
'max_size'= > 2000,
);
$ this->加载 - >库('upload',$ config_logo_image);
if($ this-> upload-> do_upload('logo_image')){
$ logo_image_data = $ this-> upload-> data();
}
并通过您的 $ data
array
$ data1 = array(
'Bedrijfsnaam'=> $ this-> input-> post('Bedrijfsnaam'),
'Postcode'=> $ this-> input-> post('Postcode'),
'Plaats'= < $ this-> input-> post('Plaats'),
'Telefoonnummer'=> $ this-> input-> post('Telefoonnummer'),
'Email '=> $ this-> input-> post('Email'),
'Website'=> $ this-> input-> post('Website'),
'Profiel'=> $ this-> input-> post('Profiel'),
'Adres'=> $ this-> input-> post('Adres'),
'logo'=> $ logo_image_data ['full_path'] //它是图片的完整路径
);
$ this-> db-> insert('bedrijven',$ data1);
'logo'=> $ logo_image_data ['file_name'] //只有文件名
希望它有道理
I have an form for adding companies to my database. I've added a field called Logo. I want to use this field so the user can upload an Logo for their company. I am using the Codeigniter upload class in my form.
My company table looks like this:
Companies
---------
id
companyname
address
postalcode
country
email
website
logo
My form looks liek this:
<tr>
<td><?= form_label('Bedrijfsnaam:');?></td>
<td><?= form_input('Bedrijfsnaam');?><small> (Spaties niet toegestaan)</small></td>
</tr>
<tr>
<td><?= form_label('Adres:');?></td>
<td><?= form_input('Adres');?></td>
</tr>
<tr>
<td><?= form_label('Postcode:');?></td>
<td><?= form_input('Postcode');?></td>
</tr>
<tr>
<td><?= form_label('Plaats:');?></td>
<td><?= form_input('Plaats');?></td>
</tr>
<tr>
<td><?= form_label('Telefoonnummer:');?></td>
<td><?= form_input('Telefoonnummer');?></td>
</tr>
<tr>
<td><?= form_label('Website:');?></td>
<td><?= form_input('Website');?></td>
</tr>
<tr>
<td><?= form_label('Email:');?></td>
<td><?= form_input('Email');?></td>
</tr>
<tr>
<td><?= form_label('Profiel:');?></td>
<td><?= form_textarea('Profiel');?></td>
</tr>
<tr>
<td><?= form_label('Categorieen:'); ?></td>
<td><?= form_dropdown('categorieen', $opties); ?></td>
</tr>
<tr>
<td><?= form_label('Logo:'); ?></td>
<td><input type="file" name="logo" size="20" /></td>
</tr>
<tr>
<td><?= form_submit('submit', 'Opslaan');?> <?= form_reset('reset', 'Reset');?></td>
</tr>
My model looks like this:
$data1 = array(
'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
'Postcode' => $this->input->post('Postcode'),
'Plaats' => $this->input->post('Plaats'),
'Telefoonnummer' => $this->input->post('Telefoonnummer'),
'Email' => $this->input->post('Email'),
'Website' => $this->input->post('Website'),
'Profiel' => $this->input->post('Profiel'),
'Adres' => $this->input->post('Adres'),
'logo' => $this->input->post('logo')
);
$this->db->insert('bedrijven',$data1);
I know this won't work but I can't figure out how to do this.
I have another uploadform but that's only for uploading images, not for the logo.
What's the best way to do this using the upload class?
EDIT:
I get an empty field when I add a new company.
My controller:
function addbedrijven()
{
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if($this->upload->do_upload('logo')){
$logo_image_data = $this->upload->data();
}
$this->members_model->addbedrijf();
redirect('members/index');
}
My Model:
function addbedrijf()
{
$logo_image_data = $this->upload->data();
$data1 = array(
'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
'Postcode' => $this->input->post('Postcode'),
'Plaats' => $this->input->post('Plaats'),
'Telefoonnummer' => $this->input->post('Telefoonnummer'),
'Email' => $this->input->post('Email'),
'Website' => $this->input->post('Website'),
'Profiel' => $this->input->post('Profiel'),
'Adres' => $this->input->post('Adres'),
'logo' => $logo_image_data['full_path']
);
$this->db->insert('bedrijven',$data1);
}
you can use the codeigniter
upload
class within the same form
$config_logo_image = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => UPLOAD_PATH,
'max_size' => 2000,
);
$this->load->library('upload', $config_logo_image );
if($this->upload->do_upload('logo_image')){
$logo_image_data = $this->upload->data();
}
And through this data in the your $data
array
$data1 = array(
'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
'Postcode' => $this->input->post('Postcode'),
'Plaats' => $this->input->post('Plaats'),
'Telefoonnummer' => $this->input->post('Telefoonnummer'),
'Email' => $this->input->post('Email'),
'Website' => $this->input->post('Website'),
'Profiel' => $this->input->post('Profiel'),
'Adres' => $this->input->post('Adres'),
'logo' => $logo_image_data ['full_path'] // it is full path of image
);
$this->db->insert('bedrijven',$data1);
'logo' =>$logo_image_data ['file_name'] //only file name
Hope it makes sense