从文件输入中获取名称

时间:2015-01-02 14:05:14

标签: php codeigniter

当我上传文件图片时,此!$this->upload->do_upload($name)将无效。

它没有拿起我的字段name="banner_image[ + image_row + ]['image']"

image_row生成行ID号。

Var转储

array(1) { ["banner_image">=> array(5) { ["name">=> array(1) { [1]=> array(1) { ["image">=> string(10) "10-dec.jpg" } } ["type">=> array(1) { [1]=> array(1) { ["image">=> string(10) "image/jpeg" } } ["tmp_name">=> array(1) { [1]=> array(1) { ["image">=> string(24) "C:\Xampp\tmp\phpC1F6.tmp" } } ["error">=> array(1) { [1]=> array(1) { ["image">=> int(0) } } ["size">=> array(1) { [1]=> array(1) { ["image">=> int(261032) } } } }

在控制器do_upload函数上如何才能使其正确选择字段名称?

控制器功能

function do_upload() {
    $config['upload_path'] = './upload/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '3000';
    $config['max_width']  = '*';
    $config['max_height']  = '*';

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

    $this->upload->initialize($config);
    // this below is same as whats on set_rules 
    $name = "banner_image[' + image_row + '][image]"; // + image_Row + is id as shown on view

    if (!$this->upload->do_upload($name)) {

        $this->form_validation->set_message('do_upload', $this->upload->display_errors());
        return false;

    } else {

        return true;

    }
}

查看

<script type="text/javascript">
var image_row = <?php echo $image_row; ?>;


function addImage() {
  html = '<tr id="image-row' + image_row + '">';
  html += '<td class="text-left">';
  <?php foreach ($languages as $language) { ?>
  html += '<div class="input-group" style="margin-bottom: 20px;">';
  html += '<span class="input-group-addon"><img src="<?php echo base_url("image/flags");?>/<?php echo $language["image"]; ?>"></span><input type="text" name="banner_image[' + image_row + '][banner_image_description][<?php echo $language["language_id"]; ?>][title]" value="" placeholder="Name Of Banner" class="form-control" />';
  html += '</div>'; 
  <?php } ?>  
  html += '</td>';
  html += '<td class="text-left">';
  html += '<input type="text" name="banner_image[' + image_row + '][link]" value="" placeholder="Website Page Url" class="form-control" />';
  html += '</td>';
  html += '<td class="text-left">';
  html += '<div class="imagePreview" class="img-thumbnail"></div>';
  html += '<input id="idFile" type="file" name="banner_image[' + image_row + '][image]" onchange="previewImage(this,[256],4);" />';
  html += '</td>';
  html += '<td class="text-left">';
  html += '<input type="text" name="banner_image[' + image_row + '][sort_order]" value="" placeholder="Sort Order" class="form-control">';
  html += '</td>';
  html += '<td class="text-left">';
  html += '<button type="button" onclick="$(\'#image-row' + image_row  + '\').remove();" data-toggle="tooltip" title="Remove" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button>';
  html += '</td>';
  html += '</tr>';
  $('#images tbody').append(html);
  image_row++;
}

</script>

2 个答案:

答案 0 :(得分:0)

我建议您参考Codeigniter用户指南文件上传类。 https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

您在表单中使用了 text 类型输入。使用文件类型输入。只是给出一个选择。

无论如何,该链接将引导您完成简单的文件上传练习。

要获取上传文件的文件名,您可以在控制器中使用它。 $ filename = $ this-&gt; upload-&gt; data(&#39; file_name&#39;);

然后从控制器您可以将它发送到任何模型。

希望这会有所帮助。

答案 1 :(得分:0)

尝试更改此行

name="banner_image[ + image_row + ]['image']"

name="banner_image[ '+ image_row +' ]['image']"

你错过了''

和脚本

html = '<tr id="image-row' + image_row + '">';

html += '<tr id="image-row' + image_row + '">';