Php 将值从控制器传递到视图
我创建了一个表单,并从表单中传递了name和picture的值。通过上传控制器访问该值,如下所示:Php 将值从控制器传递到视图,php,codeigniter,file-upload,Php,Codeigniter,File Upload,我创建了一个表单,并从表单中传递了name和picture的值。通过上传控制器访问该值,如下所示: $data = array( 'title' => $this->input->post('title', true), 'name' => $this->input->post('name',true), 'picture' => $this->file_upload($_FILES['picture']) ); ret
$data = array(
'title' => $this->input->post('title', true),
'name' => $this->input->post('name',true),
'picture' => $this->file_upload($_FILES['picture'])
);
return $data;
<?php var_dump($title)?>
<?php var_dump($name)?
<?php var_dump($picture)?>
我需要将这些值传递给视图,因此,我将上述代码修改为:
class Upload extends CI_Controller
{
function __construct() {
parent::__construct();
}
public function input_values(){
$data = array(
'name' => $this->input->post('name',true),
'picture' => $this->file_upload($_FILES['picture'])
);
$this->load->view('documents', $data); }
function add(){
$data = $this->input_values();
if($this->input->post('userSubmit')) {
$this->file_upload(($_FILES['picture']));
if (!empty($_FILES['picture']['name'])) {
$config['upload_path'] = 'uploads/docs/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf|docx';
$config['file_name'] = $_FILES['picture']['name'];
$data['picture']=$this->file_upload($_FILES['picture']);
}
}
return $this->db->insert('files', $data);
}
//logo image upload
public function file_upload($file)
{
$this->my_upload->upload($file);
if ($this->my_upload->uploaded == true) {
$this->my_upload->file_new_name_body = 'file_' . uniqid();
$this->my_upload->process('./uploads/docs/');
$image_path = "uploads/docs/" . $this->my_upload->file_dst_name;
return $image_path;
} else {
return null;
}
}
}
但我只能得到头衔的价值。名称和标题都出现以下错误:
Message: Undefined variable: name
我从视图中访问了变量,如下所示:
$data = array(
'title' => $this->input->post('title', true),
'name' => $this->input->post('name',true),
'picture' => $this->file_upload($_FILES['picture'])
);
return $data;
<?php var_dump($title)?>
<?php var_dump($name)?
<?php var_dump($picture)?>
因此,这部分是获取post数据和加载视图(包含上载表单)的地方 然后这部分是处理上传表单的post请求:
function add() {
$data = $this->input_values();
if($this->input->post('userSubmit')) {
$this->file_upload(($_FILES['picture']));
if (!empty($_FILES['picture']['name'])) {
$config['upload_path'] = 'uploads/docs/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf|docx';
$config['file_name'] = $_FILES['picture']['name'];
$data['picture']=$this->file_upload($_FILES['picture']);
}
}
return $this->db->insert('files', $data);
}
这部分是你上传文件的地方
public function file_upload($file)
{
$this->my_upload->upload($file);
if ($this->my_upload->uploaded == true) {
$this->my_upload->file_new_name_body = 'file_' . uniqid();
$this->my_upload->process('./uploads/docs/');
$image_path = "uploads/docs/" . $this->my_upload->file_dst_name;
return $image_path;
} else {
return null;
}
}
当您调用add()函数时,它会调用input_values()函数,然后加载视图,这样就不会执行下一行代码(cmiiw)
所以,也许你想改变一下:
public function index() {
if ($this->input->post()) {
// then handle the post data and files tobe upload here
// save the post data to $data, so you will able to display them in view
} else {
// set the default data for the form
// or just an empty array()
$data = array();
}
// if the request was not a post, render view that contain form to upload file
$this->load->view('nameOfTheView', $data);
}
你能显示你试图读取/访问这些值的代码吗?我刚刚对这些变量进行了var_转储请检查我的编辑请显示整个控制器。您没有给我们足够的信息,无法从中找到解决方案。您的问题标题具有误导性。请检查我编辑的代码