【发布时间】:2012-01-29 16:31:14
【问题描述】:
我在这一点上卡了很长时间,即使在阅读了很多帖子之后,我也无法找到解决方案。
我正在制作一个测验的界面,在管理界面中,我需要将图像上传到文件夹并将图像名称上传到数据库。我可以处理其他事情,但图片上传困扰了我很长时间。
请看我下面的代码。
形式
<?php echo form_open_multipart('admin/update_question'); ?>
<?php echo form_hidden('questionid', $question->level); ?>
<?php echo form_label('Comment', 'comment'); ?>
<div class="input">
<?php
$arr_comment = array(
'name' => 'comment',
'id' => 'comment',
'value' => $question->comment
);
echo form_textarea($arr_comment);
?>
</div>
<br/>
<?php echo form_label('Answer', 'answer'); ?>
<div class="input">
<?php
$arr_answer = array(
'name' => 'answer',
'id' => 'answer',
'size' => '10000',
'value' => $question->answer
);
echo form_input($arr_answer);
?>
</div>
<br/>
<?php echo form_label('Image', 'userfile'); ?>
<div class="input">
<?php
$arr_image = array(
'name' => 'userfile',
'id' => 'userfile',
'value' => ''
);
echo form_upload($arr_image);
?>
</div>
<br/>
<?php
$arr_button = array(
'name' => 'submit',
'value' => 'Update Question',
'class' => 'btn primary large'
);
?>
<div class="input">
<?php echo form_submit($arr_button); ?>
</div>
<br/>
<?php
echo form_close();
if ($error != '')
echo '<div class="alert-message error">'. $error.' </div>';
echo validation_errors();
?>
我试过运行一个js,它返回上传框中的文件名,它确实返回了它。
控制器
public function update_question() {
$comment = $this->input->post('comment');
$answer = $this->input->post('answer');
echo var_dump(is_dir(base_url().'uploads/'));
/*
* Uploading image
*/
$config['upload_path'] = base_url().'/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['max_size'] = 0;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
else
{
$arr_image = array('upload_data' => $this->upload->data());
print_r($arr_image);
}
$questionid = $this->input->post('questionid');
$question_data = array(
'comment' => $comment,
'answer' => $answer
);
$this->load->model('adminmodel', 'admin');
$question_data = $this->admin->update_question_data($questionid, $question_data);
//redirect('admin/questions', 'location');
}
对于我的上传路径,我尝试了一个数字或组合,唯一返回 TRUE 值的是 var_dump(is_dir('/wamp/www/quark_edorado/uploads'));,但这也返回了相同的错误。
我不知道我哪里错了。
更新
我的目录结构是
/application/
/public/
css/
fonts/
images/
js/
/system/
/uploads/
我正在操作一台 Windows 机器并使用 WAMP。这有什么不同吗?
【问题讨论】:
-
你试过把上传文件夹放在应用程序和公共文件夹中
-
@Hemant 将上传文件夹放在应用程序中会使其在逻辑上无法访问。是的,我曾尝试将其公开,但没有成功。
-
尝试从 $config['upload_path'] 中删除
base_url()。