在 Codeigniter 中上传图片
Upload images in Codeigniter
我是 Codeigniter 的新手,我在使用图片上传功能时遇到了问题。
错误提示 - 您没有 select 要上传的文件。
谁能检查我的代码并告诉我该怎么做?
这是我的控制器
这是我处理数据的控制器。
public function add_data() {
$this->load->view('admin/header.php');
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'name', 'required');
$this->form_validation->set_rules('price', 'price', 'required');
$this->load->helper(array('form', 'url'));
if ($this->form_validation->run() == TRUE)
{
$this->load->view('admin/add_view.php');
$config['upload_path'] = './files/';
$config['allowed_types'] = 'jpg';
$this->load->library('upload', $config);
$this->upload->do_upload('picture');
$this->upload->data();
$data = array(
'name' => $this->input->post('name'),
'info' => $this->input->post('info'),
'gorod' => $this->input->post('gorod'),
'price' => $this->input->post('price'),
'amount' => $this->input->post('amount'),
'age' => $this->input->post('age'),
'status' => $this->input->post('status'),
'minbal' => $this->input->post('minbal'),
'contacts' => $this->input->post('contacts'),
'email' => $this->input->post('email'),
'alias' => $this->input->post('alias'),
'filename' => $this->input->post('picture')
);
$this->Adminmodel->add_record($data);
}
else
{
$this->load->view('admin/formnotsuccess');
}
}
VIEW
<form method="post" action="add_data" role="form" style="padding: 30px">
<div class="row">
<div class="form-group col-md-6 ">
<label>Название университета</label></br>
<input type="text" name="name" class="form-control" size="20">
</div>
</div>
<div class="row">
<div class="form-group col-md-10">
<label>Информация об университете</label></br>
<textarea id="textarea" name="info"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Город</label></br>
<select class="form-control" id="gorod" name="gorod">
<option value="Алматы">Алматы</option>
<option value="Астана">Астана</option>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Стоимость обучения</label>
<input type="text" name="price" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Количество студентов</label>
<input type="text" name="amount" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Возраст университета</label>
<input type="text" name="age" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Статус университета</label></br>
<select class="form-control" id="status" name="status">
<option value="Государственный">Государственный</option>
<option value="Частный">Частный</option>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Минимальный балл для поступления</label>
<input type="text" name="minbal" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Контактные данные</label>
<textarea id="textarea2" name="contacts"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>E-mail</label></br>
<input type="text" name="email" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Alias</label></br>
<input type="text" name="alias" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Картинка заднего фона</label></br>
<input type="file" name="picture" class="form-control">
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
你不见了
enctype="multipart/form-data"
形式
And Upload code should be like bellow. Its easy to track your errors
if(!$this->upload->do_upload()) # do_upload('picture');
{
echo $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
print_r($data);
}
我是 Codeigniter 的新手,我在使用图片上传功能时遇到了问题。 错误提示 - 您没有 select 要上传的文件。 谁能检查我的代码并告诉我该怎么做?
这是我的控制器
这是我处理数据的控制器。
public function add_data() {
$this->load->view('admin/header.php');
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'name', 'required');
$this->form_validation->set_rules('price', 'price', 'required');
$this->load->helper(array('form', 'url'));
if ($this->form_validation->run() == TRUE)
{
$this->load->view('admin/add_view.php');
$config['upload_path'] = './files/';
$config['allowed_types'] = 'jpg';
$this->load->library('upload', $config);
$this->upload->do_upload('picture');
$this->upload->data();
$data = array(
'name' => $this->input->post('name'),
'info' => $this->input->post('info'),
'gorod' => $this->input->post('gorod'),
'price' => $this->input->post('price'),
'amount' => $this->input->post('amount'),
'age' => $this->input->post('age'),
'status' => $this->input->post('status'),
'minbal' => $this->input->post('minbal'),
'contacts' => $this->input->post('contacts'),
'email' => $this->input->post('email'),
'alias' => $this->input->post('alias'),
'filename' => $this->input->post('picture')
);
$this->Adminmodel->add_record($data);
}
else
{
$this->load->view('admin/formnotsuccess');
}
}
VIEW
<form method="post" action="add_data" role="form" style="padding: 30px">
<div class="row">
<div class="form-group col-md-6 ">
<label>Название университета</label></br>
<input type="text" name="name" class="form-control" size="20">
</div>
</div>
<div class="row">
<div class="form-group col-md-10">
<label>Информация об университете</label></br>
<textarea id="textarea" name="info"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Город</label></br>
<select class="form-control" id="gorod" name="gorod">
<option value="Алматы">Алматы</option>
<option value="Астана">Астана</option>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Стоимость обучения</label>
<input type="text" name="price" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Количество студентов</label>
<input type="text" name="amount" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Возраст университета</label>
<input type="text" name="age" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Статус университета</label></br>
<select class="form-control" id="status" name="status">
<option value="Государственный">Государственный</option>
<option value="Частный">Частный</option>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Минимальный балл для поступления</label>
<input type="text" name="minbal" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Контактные данные</label>
<textarea id="textarea2" name="contacts"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>E-mail</label></br>
<input type="text" name="email" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Alias</label></br>
<input type="text" name="alias" class="form-control">
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>Картинка заднего фона</label></br>
<input type="file" name="picture" class="form-control">
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
你不见了
enctype="multipart/form-data"
形式
And Upload code should be like bellow. Its easy to track your errors
if(!$this->upload->do_upload()) # do_upload('picture');
{
echo $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
print_r($data);
}