“上传路径似乎不是有效的”。 Codeigniter文件上传不工作 [英] "The upload path does not appear to be valid". Codeigniter file upload not working
问题描述
我已经停留在这一点了很长时间,即使在阅读了一些帖子后,我还是找不到一个解决方案。
我我做一个测验的接口,在管理界面,我需要上传图像到一个文件夹,并将图像名称上传到数据库。
请看下面的代码。
表单
<?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 /'));
/ *
*上传图片
* /
$ 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'));
这也会返回相同的错误。
我不知道我在哪里错了。
更新< h2>
我的目录结构是
/ application /
/ public /
css /
fonts /
images /
js /
/ system /
/ uploads /
我正在操作Windows机器并使用WAMP。
找出了愚蠢的问题。
我正在自动加载库和一些如何当我试图初始化配置 $ this-> load-> library('upload',$ config);
而是把我的配置文件放在 config / upload.php
p>
另一种方法是 $ this-> upload-> initialize($ config);
I have been stuck at this point for a long time and even after reading a number of posts, I haven't been able to find a solution.
I am making an interface for a quiz, and in the admin interface, I need to upload images to a folder and upload the image name to the database. I can handle to other things but the image upload is bugging me alot for a long time.
Please take a look at my code below.
The Form
<?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();
?>
I have tried running a js which returns the filename in the upload box, and it does return it.
The Controller
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');
}
For my upload path, I have tried a number or combinations and the only one which returned a TRUE
value was var_dump(is_dir('/wamp/www/quark_edorado/uploads'));
but this also returned the same error.
I do not know where I am going wrong.
Update
My directory structure is
/application/
/public/
css/
fonts/
images/
js/
/system/
/uploads/
I am operating a Windows machine and using WAMP. Does that make a difference?
Figured out the idiotic problem.
I was autoloading the library and some how when I was trying to initialize the configuration by $this->load->library('upload', $config);
it wouldn't do so.
Instead I put my config files in config/upload.php
The other method to do so would have been $this->upload->initialize($config);
这篇关于“上传路径似乎不是有效的”。 Codeigniter文件上传不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!