Php CodeIgniter图像上传功能
当我试图同时保存其他数据时,如何上载图像?表单提交时,会点击保存功能:Php CodeIgniter图像上传功能,php,codeigniter,Php,Codeigniter,当我试图同时保存其他数据时,如何上载图像?表单提交时,会点击保存功能: function save() { $this->save_data($_POST, $_FILES); } function save_data($post_data, $file_data) { // if theres an image if(!empty($file_data['image']['size'])) { $path = '/images';
function save() {
$this->save_data($_POST, $_FILES);
}
function save_data($post_data, $file_data) {
// if theres an image
if(!empty($file_data['image']['size'])) {
$path = '/images';
$this->upload_image($path);
}
}
function upload_image($path) {
// CI is looking for $_FILES super global but I want to pass that data in
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->data('image');
$this->upload->do_upload('image');
}
我不知道如何将文件数据实际传递给另一个函数。我看到的所有示例都显示了表单提交到一个函数,该函数直接从表单上传函数。不过我想从另一个函数中执行上载操作。如果您试图检查文件是否已实际上载,请执行以下操作
//this is optional
if (empty($_FILES['userfile']['name'])) {
$this->form_validation->set_rules('userfile', 'picture', 'required');
}
if ($this->form_validation->run()) { //if using validation
//validated
if (!empty($_FILES['userfile']['name'])) {
//picture is beeing uploaded
$config['upload_path'] = './files/pcitures';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
//$error = array('error' => $this->upload->display_errors());
} else {
//no error, insert/update in DB
$tmp = $this->upload->data();
echo "<pre>";
var_dump($tmp);
echo "</pre>";
}
} else { ... }
}
//这是可选的
if(空($_文件['userfile']['name'])){
$this->form_validation->set_规则('userfile','picture','required');
}
如果($this->form\u validation->run()){//if使用验证
//验证
如果(!空($\u文件['userfile']['name'])){
//图片正在上传
$config['upload_path']='./files/pcitures';
$config['allowed_types']='gif | jpg | png | jpeg';
$config['encrypt_name']=TRUE;
$this->load->library('upload',$config);
如果(!$this->upload->do_upload('userfile')){
//$error=array('error'=>$this->upload->display_errors());
}否则{
//无错误,在数据库中插入/更新
$tmp=$this->upload->data();
回声“;
$save_data = $this->save_model->save_data($_POST, $_FILES);
var_dump($tmp);
回声“;
$save_data = $this->save_model->save_data($_POST, $_FILES);
}
}否则{…}
}
我的错误与文件夹权限有关
对于希望将上载功能拆分为多个功能的用户:
控制器:
function save_data($data, $file) {
// check if theres an image
if (!empty($file['image']['size'])) {
// where are you storing it
$path = './images';
// what are you naming it
$new_name = 'name_' . random_string('alnum', 16);
// start upload
$result = $this->upload_image($path, $new_name);
}
}
function upload_image($path, $new_name) {
// define parameters
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
// upload the image
if ($this->upload->do_upload('image')) {
// success
// pass back $this->upload->data() for info
} else {
// failed
// pass back $this->upload->display_errors() for info
}
}
型号:
function save_data($data, $file) {
// check if theres an image
if (!empty($file['image']['size'])) {
// where are you storing it
$path = './images';
// what are you naming it
$new_name = 'name_' . random_string('alnum', 16);
// start upload
$result = $this->upload_image($path, $new_name);
}
}
function upload_image($path, $new_name) {
// define parameters
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
// upload the image
if ($this->upload->do_upload('image')) {
// success
// pass back $this->upload->data() for info
} else {
// failed
// pass back $this->upload->display_errors() for info
}
}
从另一个函数(通过Codeigniter或其他方式)执行上载时从未遇到任何问题。当您直接从submit函数使用上述代码时,它是否有效?