我正在尝试使用CodeIgniter 1.7.2创建图像上传表单
但是,当我尝试上传图片时,CI会通知我上传目录无效。
为了检查这一点,我将一个文件上传到名为uploads
的{{1}}目录,内容为test.txt
然后我回复了它的内容,并且看到它,你好。除此之外,我可以浏览目录并在浏览器中查看它的内容,因此目录肯定存在并且可以查看。
控制器:
Hello, World!
class Pictures extends Site_Controller {
// snip...
public function upload() {
$this->load->helper('form');
$this->_render('Do Upload','pictures/upload',array('msg'=>''));
}
public function do_upload() {
$this->load->helper('form');
$config['upload_path'] = base_url().'uploads/';
// test the directory:
// echo file_get_contents($config['upload_path'].'test.txt');
// should echo "Hello, World!"
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$data = array('msg' => $this->upload->display_errors());
$this->_render('Do Upload (Errors!)','pictures/upload',$data);
}
else
{
$uploaddata = $this->upload->data();
$this->load->model('pictures_Model','pictures');
$this->pictures->insertUploaded($uploaddata,$this->input->post('name'),$this->input->post('caption'));
$data['msg'] = 'Successful Upload!';
$this->_render('Do Upload (Success!)','pictures/upload',$data);
}
}
//...
}
只是一个快捷方式函数,它使指定数据在具有指定标题的布局中包装指定视图。
$this->_render()
查看:
pictures/upload
最后,也许是不必要的,模型插入:
<h1>Do Upload</h1>
<? if (isset($msg)) echo $msg; ?>
<?= form_open_multipart('pictures/do_upload');?>
<input type="file" name="userfile" />
<input type="text" name="name" />
<input type="text" name="caption" />
<br /><br />
<input type="submit" value="Upload" />
</form>
知道为什么我的上传目录无效?
答案 0 :(得分:6)
$config['upload_path'] = base_url().'uploads/';
您应该将其更改为:
$config['upload_path'] = './uploads/';
答案 1 :(得分:1)
CI在无法找到目录时甚至在目录不可读时生成该错误。首先尝试找出目录如下:
echo base_url().'uploads/';
如果有,请检查目录权限。 chmod到755。
如果即使目录权限也没问题,请尝试将以下行添加到上传路径中。
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/' . base_url().'uploads/';
通过回声确保您的路径正常。
希望有所帮助。