图片无法上传

时间:2015-12-01 10:21:46

标签: php codeigniter

我正在尝试在codigniter中上传图像,但在我尝试时无法这样做会向我显示错误数组到字符串转换

<?php 
public function do_upload() {
$config['upload_path'] = base_url().'assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';

$this->load->library('upload', $config);
$this->upload->do_upload();
if (!$this->upload->do_upload()) {
    $error = array('error' => $this->upload->display_errors());

    $this->session->set_flashdata('success_msg', $error);
        redirect('admin/theme');
} else {
    $data = array('upload_data' => $this->upload->data());

    $this->load->view('upload_success', $data);
}
}
?>

3 个答案:

答案 0 :(得分:2)

base_url()是返回HTTP路径而不是目录路径,您应该使用目录路径来上传内容。

变化

$config['upload_path'] = base_url().'assets/uploads/';

$config['upload_path'] = getcwd().'assets/uploads/';

您可以使用getcwd()本机PHP函数来获取项目根目录,CI也为根目录提供常量变量FCPATH

答案 1 :(得分:1)

你的文件正在升级两次。在第一次上传后,每件事都消失了,你没有任何上传的内容

[
  {
    "id": "test2",
    "updated": "2015-11-30T16:51:52.125Z",
    "status": "approved",
  },
  {
    "id": "test3",
    "updated": "2015-11-30T16:51:50.469Z",
    "status": "approved",
  }
]

只需评论

$this->upload->do_upload();// first time
if (!$this->upload->do_upload()) {// second time
    $error = array('error' => $this->upload->display_errors());

    $this->session->set_flashdata('success_msg', $error);
        redirect('admin/theme');
}

编辑

您可以将邮件发送到set_flashdata()

// $this->upload->do_upload();// comment this line

要读取flashdata变量:

if (!$this->upload->do_upload('file_name')) {// add file name here
    $error = $this->upload->display_errors();// remove array from here

    $this->session->set_flashdata('success_msg', $error);
        redirect('admin/theme');
}

答案 2 :(得分:1)

当您尝试回显数组时,通常会显示数组到字符串转换。

所以看来问题出在这一行:

echo validation_errors();

要避免此错误,但仍使用Foreach()打印出数组中的值。

本准则应解决您的问题:

foreach (validation_errors() as $error) { 
    //I assume you get an array as a return value
    echo $error;
}

修改

这个答案不再有用,因为作者编辑了他的问题