我正在尝试上传多张经过验证的图像,但无法执行此操作。
我的查看代码如下:-
<?php echo form_open("controller/action");?>
<ul>
<li>Category Image <input type="file" name="category_img"></li>
<li>Product Image <input type="file" name="product_img"></li>
<li>Slider Image <input type="file" name="slider_img"></li>
</ul>
<input type="submit" name="submit" class="btn" value="Submit" />
<?php echo form_close();?>
期待解决方案。
按照你的方式去做,你的形式应该是:
<?php
// success or error status in uploading for each file
if( !empty( $notification ) )
{
echo '
<p>Notifications : </p>
<p>'.$notification.'</p>';
}
?>
<!-- multipart form opening -->
<?php echo form_open_multipart("image/upload");?> <!-- "controller/action" -->
<ul>
<li>Category Image <input type="file" name="category_img"></li>
<li>Product Image <input type="file" name="product_img"></li>
<li>Slider Image <input type="file" name="slider_img"</li>
</ul>
<input type="submit" name="submit" class="btn" value="Submit" />
<?php echo form_close();?>
你的控制器应该是:
class Image extends CI_Controller {
private $data; // data member for storing status of the uploads
function __construct()
{
// some code
}
public function index()
{
$this->upload();
}
public function upload()
{
$this->data['notification'] = '';
if( $this->input->post('submit') )
{
// loading helpers
$this->load->helper(array('form', 'url'));
//setting the config array, for more options see Codeigniter docs
$config['upload_path'] = 'uploads/'; // upload path
$config['allowed_types'] = 'gif|jpg|jpeg|png'; // allowed file types
// loading upload library with config array
$this->load->library('upload', $config);
// uploading the files, lets_upload() is defined below
$this->lets_upload( 'category_img' );
$this->lets_upload( 'product_img' );
$this->lets_upload( 'slider_img' );
}
$this->load->view('form', $this->data); // form view is loaded along with success or error notification in the 'data' member variable
}
public function lets_upload( $field_name ) // '$field_name' refers to input field name
{
if ( ! $this->upload->do_upload( $field_name )) // if uploading failed
{
$this->data['notification'] .= $this->upload->display_errors(); // stored error in member variable 'data'
}
else // if uploading success
{
$upload_data = $this->upload->data(); // stored the file info in 'data'
$this->data['notification'] .= $upload_data['file_name']." is successfully uploaded.<br>"; // file name is displayed with 'success' message
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)