雖然我選擇了要上傳的文件,但試圖上傳文件時收到錯誤消息You did not select a file to upload.
。我正在使用使用bootstrap的樣式的輸入文件。這是問題嗎?我按照Codeigniter用戶指南中的說明進行文件上傳。任何人都可以幫助我,並確定我的代碼中的錯誤?在codeigniter中上傳文件
謝謝
控制器
public function create()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if (! $this->upload->do_upload('userfile'))
{
echo "Upload failed";
}
else
{
$data = array('upload_data' => $this->upload->data());
}
$data = array(
'category' => $this->input->post('category');
'name' => $this->input->post('recipename'),
'ingredients' => $this->input->post('ingredients'),
'directions' => $this->input->post('directions'),
'date' => date("Y/m/d")
);
$this->model->insert($data);
redirect('', 'refresh');
}
查看
<form action="<?php echo base_url();?>home/create" method="POST" role="form" multipart>
<legend>New Recipe</legend>
<div class="form-group">
<label for="">Category</label>
<select name="category" id="input" class="form-control">
<?php foreach ($category as $value) {?>
<option value="<?php echo $value->category; ?>"><?php echo $value->category;?></option>
<?php } ?>
</select>
<label>Name</label>
<input type="text" name="recipename" class="form-control" id="" placeholder="Recipe Name">
<label>Image</label>
<div class="input-group">
<label class="input-group-btn">
<span class="btn btn-primary">
Browse… <input type="file" name="userfile" style="display: none;" multiple>
</span>
</label>
<input type="text" class="form-control" readonly>
</div>
<label>Ingredients</label>
<textarea name="ingredients" id="input" class="form-control" required="required"></textarea>
<label>Directions</label>
<textarea name="directions" id="input" class="form-control" required="required"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<script type="text/javascript">
$(function() {
// We can attach the `fileselect` event to all file inputs on the page
$(document).on('change', ':file', function() {
var input = $(this),
numFiles = input.get(0).files ? input.get(0).files.length : 1,
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [numFiles, label]);
});
// We can watch for our custom `fileselect` event like this
$(document).ready(function() {
$(':file').on('fileselect', function(event, numFiles, label) {
var input = $(this).parents('.input-group').find(':text'),
log = numFiles > 1 ? numFiles + ' files selected' : label;
if(input.length) {
input.val(log);
} else {
if(log) alert(log);
}
});
});
});
</script>
你通過ajax上傳文件? –
不,我不是。我只是使用PHP。 – Beldion
@Beldion將此適當的窗體屬性'enctype =「multipart/form-data」'添加到View中。 此外,輸入文件元素有多個文件選擇,但在你的控制器將不支持多個文件上傳檢查這個幫助你[鏈接](https://stackoverflow.com/a/20138535/5589166) – Sovary