我正在尝试在codeigniter中上传图片。
这是我的视图文件代码。
<form action="<?php echo site_url('pages/data_submitted') ?>" method="get" enctype="multipart/form-data">
Image: <input type="file" name="image"/>
<button>Submit</button>
</form>
这是我的控制器代码。
class Pages extends CI_Controller
{
public function data_submitted(){
$config['upload_path'] = "img/";
$this->load->library('upload',$config);
$this->upload->do_upload();
$finfo=$this->upload->data();
$data = $this->upload->display_errors();
$this->load->model('user_model');
$this->user_model->insert_item($data);
}
}
这是我的模型代码
<?php
class User_model extends CI_Model {
function __construct(){
/* Call the Model constructor */
parent::__construct();
}
public function insert_item($item){
print_r($item);
}
}
?>
此代码有什么问题...... 这里我传递$ data只是为了检查是否发生了任何错误。 它显示&#34;您没有选择要上传的文件。&#39;甚至我选择一个文件。 请帮帮我。
答案 0 :(得分:1)
参考此代码。这肯定会对你有用
public function uploadImage() {
$this->load->helper(array('form', 'url'));
$config['upload_path'] = 'assets/images/b2bcategory';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['width'] = 75;
$config['height'] = 50;
if (isset($_FILES['catimage']['name'])) {
$filename = "-" . $_FILES['catimage']['name'];
$config['file_name'] = substr(md5(time()), 0, 28) . $filename;
}
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$field_name = "catimage";
$this->load->library('upload', $config);
if ($this->input->post('selsub')) {
if (!$this->upload->do_upload('catimage')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], $dat['upload_data']['file_name']);
}
$ip = $_SERVER['REMOTE_ADDR'];
if (empty($dat['upload_data']['file_name'])) {
$catimage = '';
} else {
$catimage = $dat['upload_data']['file_name'];
}
$data = array(
'ctg_image' => $catimage,
'ctg_dated' => time()
);
$this->b2bcategory_model->form_insert($data);
}
}
答案 1 :(得分:0)
改变这个:
<button>Submit</button>
到此:
<input type="submit" value="Submit" name="submit">
在控制器中
class Pages extends CI_Controller
{
public function data_submitted(){
$this->load->helper(array('form', 'url'));
$config['upload_path'] = "./img/";
$config['allowed_types'] = 'gif|jpg|png'; # Changed
$this->load->library('upload',$config);
if(!$this->upload->do_upload())
{
$data = $this->upload->display_errors();
}
else{
$finfo = $this->upload->data();
$this->load->model('user_model');
$this->user_model->insert_item($finfo);
}
# Load the view on here
}
}
答案 2 :(得分:0)
您必须在do_upload方法中提及字段名称。
<form method="post" enctype="multipart/form-data">
<input type="file" name="field_name"/>
</form>
<?php
$this->upload->do_upload('field_name');
?>
答案 3 :(得分:0)
试试这个:
查看页面
<?php echo echo form_open_multipart(base_url('Pages/data_submitted'),['name' => 'form', 'id' => 'form']);
//or <form action="<?php echo base_url('Pages/data_submitted') ?>" method="post" enctype="multipart/form-data">
Image: <input type="file" name="image"/>
<button id="button" name="button">Submit</button>
</form>
<强>控制器强>
class Pages extends CI_Controller
{
public function data_submitted(){
$config['upload_path'] = getcwd().'/img/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 2500;
$config['remove_space'] = TRUE;
$this->load->library('upload',$config);
$this->load->model('user_model');
if($this->upload->do_upload(image))
{
$data = $this->upload->data();
$this->user_model->insert_item($data);
}
else
{
$this->upload->display_errors();
}
}
}
答案 4 :(得分:0)
使用$this->upload->do_upload('image');
代替$this->upload->do_upload();
您必须将文件输入名称作为do_upload()
中的参数传递。如果您没有传递字段名称,那么默认情况下它将需要userfile
。这就是为什么它会给出You did not select a file to upload.
错误
答案 5 :(得分:0)
在codeigniter中上传文件要容易得多。在这里,我正在编写视图文件和控制器文件的代码。
查看文件
<html>
<body>
<?php if(isset($error)){echo $error;} ?>
<?php if(isset($success)){echo $success;} ?>
<?php echo form_open_multipart('upload_controller/do_upload');?>
<input type='file' name='userfile' size='20' />
<input type='submit' name='submit' value='upload' />
</form>
</body>
</html>
控制器文件
要启用文件上传,我们必须加载一个库“upload”
<强> $这 - &GT;负载&GT;库( '上传'); 强>
<?php
class Upload_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index(){
$this->load->view('fileupload_view');
}
public function do_upload(){
if (!is_dir('/upload')) {
mkdir('./upload',777,0);
}
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{
$data['upload_data'] = $this->upload->data();
$data['success']= 'File Successfully Uploaded';
$this->load->view('fileupload_view',$data);
}
else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('fileupload_view', $error);
}
}
}
?>