在Codeigniter中上传图像显示错误上传路径似乎无效 [英] Uploading an image in Codeigniter shows error The upload path does not appear to be valid
本文介绍了在Codeigniter中上传图像显示错误上传路径似乎无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$config['upload_path'] = site_path().'photos/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$this->data['alert'] = $this->upload->display_errors();
$this->load->view('profile/photo', $this->data);
}
else
{
$upload_data = $this->upload->data();
$filename = $upload_data['file_name'];
$width = $upload_data['image_width'];
$height = $upload_data['image_height'];
$config1 = array();
$this->load->library('image_lib');
$config1['source_image'] = site_path().'photos/'.$filename;
$this->remove_existing_file($this->session->userdata('user_id'));
$this->Profile_model->savephoto('Edit', $filename );
redirect('/profile/photo');
}
我收到此错误:
上传路径似乎无效。
The upload path does not appear to be valid.
推荐答案
发生此错误只有几个原因:
There's only a few reasons for this error to occur:
- 目录
site_path 'photos /'
不存在,请尝试运行is_dir()
以确保它。 - 该目录存在,但不可写。确保您已对目录设置了适当的权限。尝试运行
is_writable()
以确保。 - 您要使用的目录存在,上传库。请尝试使用带有正斜杠的绝对路径,类似于用户指南中的示例。
- The directory
site_path().'photos/'
does not exist, try runningis_dir()
to ensure that it does. - The directory exists but is not writable. Make sure you have set the appropriate permissions on the directory. Try running
is_writable()
to make sure. - The directory you want to use exists, but you have not represented it properly to the Upload library. Try using an absolute path with a trailing forward slash, similar to the example in the User Guide.
除此之外,没有我能想到的解释。这是验证路径(上传类的一部分)的CI代码:
Beyond that, there is no explanation I can think of. Here is the CI code that validates the path (part of the Upload class):
public function validate_upload_path()
{
if ($this->upload_path == '')
{
$this->set_error('upload_no_filepath');
return FALSE;
}
if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE)
{
$this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
}
// This is most likely the trigger for your error
if ( ! @is_dir($this->upload_path))
{
$this->set_error('upload_no_filepath');
return FALSE;
}
if ( ! is_really_writable($this->upload_path))
{
$this->set_error('upload_not_writable');
return FALSE;
}
$this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/", $this->upload_path);
return TRUE;
}
更新:
Update:
Per your comments, try this instead and let's see what happens before moving to the next step of debugging:
$config['upload_path'] = './community/photos/';
这篇关于在Codeigniter中上传图像显示错误上传路径似乎无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文