使用 codeigniter 上传图像和文本
我正在尝试创建可以上传文本和图像的表单。但是,当我输入信息时,只会保存文本,但图像似乎不会出现在文件夹中。
我的控制器文件的一部分
$form_data = array(
'address' => set_value('address'),
'area' => set_value('area'),
'lat' => set_value('lat'),
'lng' => set_value('lng'),
'subject' => set_value('subject'),
'problem' => set_value('problem'),
'image' => '',//what to put here???
'time' => $now,
'register_id' =>set_value ('register_id'),
'category_id' => set_value('category_id'),
'city_city_id' => set_value('city_city_id'),
'status_status_id' => set_value('status_status_id')
);
if ($this->report_model->SaveForm($form_data) == TRUE)
{
redirect('report/success');
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
$this->load->library('upload', $config);
}
查看文件
$attributes = array('class' => '', 'id' => '');
echo form_open('report', $attributes); ?>
<p>
<label for="problem">problem detail:</label>
<?php echo form_error('problem'); ?>
<br /><input id="problem" type="text" name="problem" value="<?php echo
set_value('problem'); ?>" />
</p>
<p>Upload a image:
<?php echo form_open_multipart('report/do_upload');?>
<input type="file" name="image" size="20" />
请帮忙
i'm trying to create form which with which i can upload text and image both. But when i enter the information the texts only get saved but the image doesn't seem to appear the folder.
Part of my controller file
$form_data = array(
'address' => set_value('address'),
'area' => set_value('area'),
'lat' => set_value('lat'),
'lng' => set_value('lng'),
'subject' => set_value('subject'),
'problem' => set_value('problem'),
'image' => '',//what to put here???
'time' => $now,
'register_id' =>set_value ('register_id'),
'category_id' => set_value('category_id'),
'city_city_id' => set_value('city_city_id'),
'status_status_id' => set_value('status_status_id')
);
if ($this->report_model->SaveForm($form_data) == TRUE)
{
redirect('report/success');
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
$this->load->library('upload', $config);
}
View file
$attributes = array('class' => '', 'id' => '');
echo form_open('report', $attributes); ?>
<p>
<label for="problem">problem detail:</label>
<?php echo form_error('problem'); ?>
<br /><input id="problem" type="text" name="problem" value="<?php echo
set_value('problem'); ?>" />
</p>
<p>Upload a image:
<?php echo form_open_multipart('report/do_upload');?>
<input type="file" name="image" size="20" />
please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,上传的文件名为
userfile
将上传输入元素的
image
更改为userfile
。否则将
image
定义为上传类使用的名称/ID。参考: http://codeigniter.com/user_guide/ Librarys/file_uploading.html
实际上... 似乎您到处都是(多个表单?)
只需转到我提供的参考链接,然后启动一个表单,请遵循用户指南以获取最佳详细信息,我认为您只是对如何在 CI 下创建表单感到困惑。
By default the file uploaded is called
userfile
Change that
image
touserfile
for your upload input element.Otherwise define
image
as the name/id to use by the upload class.Ref: http://codeigniter.com/user_guide/libraries/file_uploading.html
ACTUALLY... seems you are all over the place (multiple forms?)
Just go to the reference link I provided, and start a single form, follow the user guide for best details, I think you are just confusing how to create a form under CI.