【发布时间】:2012-01-16 12:12:01
【问题描述】:
我在 Google 中查找了此内容,并在 stackoverflow 上查找了不同的答案。并且可能其中有一个很好的答案,但我仍然不明白如何在我自己的代码中实现它。
我有自己的公共函数来上传图像,但现在我希望它是可选的。此时有人需要上传文件才能通过验证,我该如何设置这个可选?
我的功能:
public function _do_upload_image()
{
$config['upload_path'] = './company_images/';
$config['allowed_types'] = 'jpg|jpeg|png';
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$this->form_validation->set_message('_do_upload_image', $this->upload->display_errors());
}
else
{
$this->_upload_data = $this->upload->data();
}
}
提前致谢
--编辑--
对于其他人,答案有效,当没有上传图像时,我给我的 file_name 取了另一个名称。它看起来像这样:
public function _do_upload_image()
{
$config['upload_path'] = './company_images/';
$config['allowed_types'] = 'jpg|jpeg|png';
$returnArr = array();
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$returnArr = array('file_name' => 'leeg.jpg');
}
else
{
$returnArr = $this->upload->data();
}
$this->_upload_data = $returnArr;
}
【问题讨论】:
标签: php codeigniter upload