【问题标题】:Fail on codeigniter uploadcodeigniter 上传失败
【发布时间】:2016-02-25 21:37:31
【问题描述】:

我正在尝试使用 codeigniter 上的上传类上传文件,但它给我一个错误,提示文件路径无效。

我打印了$this->upload->data,它没有显示文件数据,但是我设置的上传文件夹存在并显示在上面。

array(14) {
    ["file_name"]=> string(0) "" 
    ["file_type"]=> string(0) ""
    ["file_path"]=> string(36) "homologacao/assets/images/promocoes/" 
    ["full_path"]=> string(36) "homologacao/assets/images/promocoes/"  
    ["raw_name"]=> string(0) "" 
    ["orig_name"]=> string(0) ""  
    ["client_name"]=> string(0) ""  
    ["file_ext"]=> string(0) ""  
    ["file_size"]=> string(0) ""  
    ["is_image"]=> bool(false)  
    ["image_width"]=> string(0) ""  
    ["image_height"]=> string(0) ""  
    ["image_type"]=> string(0) ""  
    ["image_size_str"]=> string(0) ""  
}

但是当我得到$_FILESdata时,它会显示上传的文件数据

array(1) {
    ["imagem"]=> array(5) { 
        ["name"]=> string(68) "3bf514d6cbb31410ee47e8cbcc79c81588a6740fb4264209a1531077e0fcc0a9.jpg" 
        ["type"]=> string(10) "image/jpeg" 
        ["tmp_name"]=> string(14) "/tmp/phpomUcFo" 
        ["error"]=> int(0) 
        ["size"]=> int(3571) 
    } 
}

我仔细检查了我的代码,我认为我设置正确

/*code slice with the upload part */

$config['upload_path'] = 'homologacao/assets/images/promocoes/';
$config['allowed_types'] = 'png|jpg|gif';
$config['max_size'] = '1024';
$config['encrypt_name'] = true;

$this->load->library('upload', $config);

$field_name = 'imagem';
if ($this->upload->do_upload($field_name)) {
    /* do stuff */
} else {
    var_dump($this->upload->data());
    var_dump($this->upload->display_errors());
    var_dump($_FILES);

    die();
}

为什么文件上传不起作用,我该如何解决?

【问题讨论】:

  • 你在 display_errors() 中得到了什么
  • 类似“无效的上传文件路径”

标签: php codeigniter file-upload


【解决方案1】:

你的文件路径应该是这样的:

$config['upload_path'] = './homologacao/assets/images/promocoes/';

【讨论】:

  • 您的“homologacao/*”文件夹是否在网络根目录中?
  • 是的。另外,我尝试了$_SERVER['DOCUMENT_ROOT'] . '/homologacao/assets/images/promocoes/',但没有成功。
  • 您是否尝试设置权限 777?
  • 试试$config['upload_path'] = FCPATH . 'homologacao/assets/images/promocoes/';
【解决方案2】:

我做到了!

刚刚在上传路径上设置了DOCUMENT_ROOT

$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . 'homologacao/assets/images/promocoes/';

感谢所有帮助我解决问题的人。

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 2013-10-08
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多