Php 如何返回要插入数据库的图像上载文件名?
有人知道如何获取刚刚上传的文件名,然后将其传递给index()函数,以便将其插入数据库中吗 它是用codeigniter编写的,下面是来自表单控制器的代码 谢谢Php 如何返回要插入数据库的图像上载文件名?,php,database,forms,codeigniter,function,Php,Database,Forms,Codeigniter,Function,有人知道如何获取刚刚上传的文件名,然后将其传递给index()函数,以便将其插入数据库中吗 它是用codeigniter编写的,下面是来自表单控制器的代码 谢谢 public function index() { $this->load->library('form_validation'); $this->form_validation->set_rules('nominee', 'Nominee', 'required'); $this-&
public function index() {
$this->load->library('form_validation');
$this->form_validation->set_rules('nominee', 'Nominee', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[self_nominees.email]');
$this->form_validation->set_rules('location', 'Location', 'required');
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
$this->form_validation->set_rules('imageupload', 'Image Upload', 'callback__image_upload');
$this->form_validation->set_rules('videoupload', 'Video Upload', 'callback__video_upload');
if ($this->form_validation->run() == FALSE)
{
//Load the form
$this->template('form.php');
}
else
{
//Run code to add into the database
$formdata = $this->input->post(NULL, TRUE); // this prevent from XSS attacks
$this->load->model('users');
$this->users->self_nominate($formdata['nominee'], $formdata['email'], $formdata['location'], $formdata['category'], $formdata['description'], 'image', 'video');
//Load the thankyou page
$this->template('thankyou.php');
}
}
/*----------------------------------------------------------------
Image Upload Function
----------------------------------------------------------------*/
function _image_upload()
{
$this->load->library('upload');
// Check if there was a file uploaded
if (!empty($_FILES['imageupload']['name']))
{
// Specify configuration for File 1
$config['upload_path'] = 'uploads/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Initialize config for File 1
$this->upload->initialize($config);
// Upload file 1
if ($this->upload->do_upload('imageupload'))
{
$data = $this->upload->data();
return true;
}
else
{
$imageerrors = $this->upload->display_errors();
$this->form_validation->set_message('_image_upload', $imageerrors);
return false;
}
}
}
从文件中找到的 这是一个帮助函数,它返回一个数组,其中包含与上载的文件相关的所有数据。以下是阵列原型:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
$this->upload->data() 从文档中找到
这是一个帮助函数,它返回一个数组,其中包含与上载的文件相关的所有数据。以下是阵列原型:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
$this->upload->data() 是的,我用过这个(不是在上面的脚本中)。它工作得很好,我想这就是我获取数据的方式。但我的问题是。如何将它从_image_upload()函数传递回index(),以便插入它?我很困惑。image\u upload()
函数在哪里?它是模型吗?如果它是一个模型,只需返回
值并让控制器按如下方式获取:$new\u file=$this->your\u model->image\u upload()
请注意,文件上载通常由控制器处理,在这种情况下,使用CodeIgniter的$this->session
类传输文件名。image_upload()也在控制器中。下面是video_upload(),我只是没有在上面的代码中包含它,因为它是一样的。我尝试过这样的方法:$data=array('upload_data'=>this->upload->data())$image_name=$data['upload_data']['file_name'];只需获取未定义的变量错误。这应该可以。。。但是你确定你正在执行$this->upload->do_upload()
?如果变量不存在,那么上传可能不会发生<代码>$this->upload->display_errors()代码>应该能帮你找到答案。是的,我用过这个(不是在上面的脚本中)。它工作得很好,我想这就是我获取数据的方式。但我的问题是。如何将它从_image_upload()函数传递回index(),以便插入它?我很困惑。image\u upload()
函数在哪里?它是模型吗?如果它是一个模型,只需返回
值并让控制器按如下方式获取:$new\u file=$this->your\u model->image\u upload()
请注意,文件上载通常由控制器处理,在这种情况下,使用CodeIgniter的$this->session
类传输文件名。image_upload()也在控制器中。下面是video_upload(),我只是没有在上面的代码中包含它,因为它是一样的。我尝试过这样的方法:$data=array('upload_data'=>this->upload->data())$image_name=$data['upload_data']['file_name'];只需获取未定义的变量错误。这应该可以。。。但是你确定你正在执行$this->upload->do_upload()
?如果变量不存在,那么上传可能不会发生<代码>$this->upload->display_errors()代码>应该可以帮助您找到答案。