【问题标题】:CodeIgniter file upload says "You did not select a file to upload"CodeIgniter 文件上传显示“您没有选择要上传的文件”
【发布时间】:2015-11-20 12:33:47
【问题描述】:

我浏览了一堆类似的问题,但似乎都没有答案。

我正在尝试在 CodeIgniter 中上传单个音频文件,但不断收到“您没有选择要上传的文件”作为错误消息。

我的代码如下:

HTML

<form class="form-horizontal" id="mp3form" action="<?=base_url();?>comp/comp_sadsc/save_entry_music/<?= $comp_location.'/'.$entry_id; ?>" method="post" enctype="multipart/form-data">
    <div class="control-group" id="load-music">
        <label for="grouping_style" class="control-label">Load MP3 Music</label>
        <div class="controls">
            <input type="file" name="userfile" id="userfile" onchange="sadsc_select_music()">
        </div>
    </div>

    <div class="control-group" id="feedback" style="display:none;">
        <label for="grouping_name" class="control-label">Status</label>
        <div class="controls">
            <p id="responsemsg"></p>
        </div>
    </div>
</form>

<div class="progress progress-striped active" id="progressbar" style="display:none;">
    <div class="bar" style="width: 0%;"></div >
    <div class="percent">0%</div>
</div>

<div id="status"></div>

JS

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');
var progressbar = $('#progressbar');

$('#mp3form').ajaxForm({
    beforeSubmit : function(data) {
        check = data[0].value;
    },
    beforeSend: function() {
        progressbar.show();
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
         return $('#mp3form').valid();
    },

    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
    },

    complete: function(xhr) {
        if (xhr.response == '"success"')
        {
            $.magnificPopup.close();
        }
        else
        {               
            alert("There was an error updating the music, please try again or contact the administrator. "+xhr.responseText);
        }
    }
});

控制器

public function save_entry_music($location, $location_entry_id)
{

$foldername = "./comps/sadsc/music/$location";

        if (!file_exists($foldername))
        {
            mkdir($foldername, 0777, true);
        }

        $config['upload_path'] = $foldername.'/';
        $config['allowed_types'] = 'mp3';
        $config['max_size'] = '0';
        $config['file_name'] = $location_entry_id.'.mp3';
        $config['overwrite'] = TRUE;

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

        if (!$this->upload->do_upload())
        {
            $errors = array('error' => $this->upload->display_errors());
            $return = ' - error - ';
            foreach ($errors as $error)
            {
                $return .= $error.' ';
            }
        }
        else
        {
            $return = 'success';
        }

        $this -> output -> set_output(json_encode($return));
}

不确定这是否有帮助,但这是我的 htaccess 文件:

    RewriteEngine on
RewriteCond $1 !^(index\.php|img|css|vendor|data|fonts|files|js|comps|favicon\.ico)
RewriteRule ^(.+)$ index.php?$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
#RewriteCond %{HTTP_HOST} - --website_url_removed--
#RewriteRule (.*) --website_url_removed--/$1 [R=301,L]

我真的希望这只是我错过的一些小事。

【问题讨论】:

  • 你的onchange="sadsc_select_music()函数在哪里??
  • 在全局 JS 文件中,该函数所做的只是删除必须开始上传的按钮的禁用属性
  • 我看到短标签 (=) :O
  • 哈哈,在服务器上启用了不会造成问题
  • PHP max_upload_filesize 限制是多少?文件是否超过这个大小?

标签: php .htaccess codeigniter


【解决方案1】:

改变

$this->upload->do_upload()

$this->upload->do_upload('userfile')

这里的 userfile 是您的文件字段的名称。

如果您仍然面临问题更改

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

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

函数参考中有明确提及

https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

【讨论】:

  • 我做了上述更改,起初它说文件超出了 PHP 配置中的限制,我将 Upload 库的配置更改为 $config['max_size'] = '99999999999'; 但它仍然是第一个错误
  • 检查 php.ini 中的 MAX_UPLOAD_SIZE 和 MAX_POST_SIZE
  • 我找不到 php.ini,但我确实将 # vi /home/httpd/html/.htaccess php_value upload_max_filesize 10M php_value post_max_size 20M php_value memory_limit 32M 添加到我的 htaccess 中,它看起来可以正常工作!
猜你喜欢
  • 2014-03-05
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 2015-07-08
相关资源
最近更新 更多