3 回答

TA貢獻(xiàn)1871條經(jīng)驗 獲得超13個贊
通過修改這種方式,我已成功顯示上傳錯誤(如果嘗試上傳,否則使用默認(rèn)圖像):create()
public function create() {
// Only logged in users can create posts
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$data = $this->get_data();
$data['tagline'] = "Add New Post";
if ($data['categories']) {
foreach ($data['categories'] as &$category) {
$category->posts_count = $this->Posts_model->count_posts_in_category($category->id);
}
}
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('desc', 'Short description', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if($this->form_validation->run() === FALSE){
$this->load->view('partials/header', $data);
$this->load->view('dashboard/create-post');
$this->load->view('partials/footer');
} else {
// Create slug (from title)
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug, null);
if ($slugcount > 0) {
$slug = $slug."-".$slugcount;
}
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
// Display upload validation errors
// only if a file is uploaded and there are errors
if (empty($_FILES['userfile']['name'])) {
$errors = [];
}
if (empty($errors)) {
$post_image = 'default.jpg';
} else {
$data['upload_errors'] = $errors;
}
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
if (empty($errors)) {
$this->Posts_model->create_post($post_image, $slug);
$this->session->set_flashdata('post_created', 'Your post has been created');
redirect('/');
} else {
$this->load->view('partials/header', $data);
$this->load->view('dashboard/create-post');
$this->load->view('partials/footer');
}
}
}
在create-post.php我看來:
<?php if(isset($upload_errors)){
foreach ($upload_errors as $upload_error) {
echo $upload_error;
}
}?>

TA貢獻(xiàn)1909條經(jīng)驗 獲得超7個贊
我在這里撿到了三樣?xùn)|西
1) 如前所述,沒有將 $data 傳遞給正確的視圖
2) 在視圖上期望數(shù)組而不是字符串,即錯誤的數(shù)據(jù)類型
3) 最后函數(shù) do_upload() 需要參數(shù)字符串 $field。缺少這就是為什么您只有 no upload selected 錯誤。如果設(shè)置了這個參數(shù),codeigniter 真的會拋出錯誤的文件類型錯誤。我這樣做是為了測試
在我看來
<form action="http://localhost:8000/welcome/create" method="post" enctype="multipart/form-data">
<input type="file" name="lname" ><br>
<input type="submit" value="Submit">
</form>
然后在我的控制器中
if(!$this->upload->do_upload("lname")){
上傳錯誤的文件類型以測試此錯誤。您可能需要額外的長度來檢測實際上傳文件的文件類型。

TA貢獻(xiàn)1860條經(jīng)驗 獲得超9個贊
您的上傳代碼看起來不錯,但您需要更新以下這些更改。
將數(shù)據(jù)傳遞給您的'dashboard/create-post'視圖,就像您傳遞給您的'partials/header'視圖一樣。您的'dashboard/create-post'視圖沒有收到任何上傳錯誤消息,所以它說'Undefined variable: uerrors'. 所以,你的上傳代碼應(yīng)該是這樣的 -
if(!$this->upload->do_upload()){
$data['uerrors'] = $this->upload->display_errors();
if ($data['uerrors']) {
$this->load->view('partials/header', $data);
$this->load->view('dashboard/create-post', $data);
$this->load->view('partials/footer');
} else {
$post_image = 'default.jpg';
}
} else {
$post_image = $this->upload->data('file_name');
}
正如CodeIgniter 文檔所說,'display_errors()' 返回字符串,而不是數(shù)組,您不必遍歷錯誤。只是在你的'dashboard/create-post'觀點(diǎn)上呼應(yīng)它。
為了您的方便,請以不同的方法制作您的上傳任務(wù),以便您也可以在更新方法中重新使用它。例如——
private function uploadFile(){
if ($_FILES['userfile']['name'] === '') {
return array(
'status' => TRUE,
'message' => 'No file selected.',
'file_name' => 'default.jpg'
);
}
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile')){
return array(
'status' => FALSE,
'message' => $this->upload->display_errors('<p class="text-danger ">', '</p>'),
'file_name' => ''
);
}else{
return array(
'status' => TRUE,
'message' => 'File uploaded successfully',
'file_name' => $this->upload->data('file_name')
);
}
}
然后你的整個創(chuàng)建方法應(yīng)該是這樣的 -
public function create() {
// Only logged in users can create posts
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$data = $this->get_data();
$data['tagline'] = "Add New Post";
if ($data['categories']) {
foreach ($data['categories'] as &$category) {
$category->posts_count = $this->Posts_model->count_posts_in_category($category->id);
}
}
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('desc', 'Short description', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if($this->form_validation->run() === FALSE){
$this->load->view('partials/header', $data);
$this->load->view('dashboard/create-post');
$this->load->view('partials/footer');
} else {
$upload = $this->uploadFile();
if($upload['status'] === FALSE){
$data['upload_error'] = $upload['message'];
$this->load->view('partials/header', $data);
$this->load->view('dashboard/create-post', $data);
$this->load->view('partials/footer');
}else{
// Create slug (from title)
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug, null);
if ($slugcount > 0) {
$slug = $slug."-".$slugcount;
}
$this->Posts_model->create_post($upload['file_name'], $slug);
$this->session->set_flashdata('post_created', 'Your post has been created');
redirect('/');
}
}
}
最后在您的視圖文件中添加這行代碼'dashboard/create-post',就在文件輸入按鈕之后。
<?php if(isset($upload_error)) echo $upload_error; ?>
我認(rèn)為一切都應(yīng)該工作。
- 3 回答
- 0 關(guān)注
- 136 瀏覽
添加回答
舉報