Php Codeigniter上载可选

Php Codeigniter上载可选,php,codeigniter,upload,Php,Codeigniter,Upload,我在谷歌上找到了这个,在stackoverflow上找到了不同的答案。也许它们中有一个很好的答案,但我仍然不知道如何在我自己的代码中实现它 我有自己的公共函数来上传图像,但现在我希望它是可选的。此时有人需要上传一个文件以通过验证,我如何使其成为可选的 我的职能: public function _do_upload_image() { $config['upload_path'] = './company_images/'; $config['allowed_types'] =

我在谷歌上找到了这个,在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();
    }

}
提前谢谢

--编辑--

对于其他人来说,答案是有效的,当没有上传图像时,我给我的文件名起了另一个名字。看起来是这样的:

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;
}

如果您的意思是需要将上载功能设置为可选功能,那么您可以执行以下操作:


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  = $this->upload->data();
    }
    return $returnArr; //just return the array, empty if no upload, file data if uploaded
}

希望这是有意义的

如果您的意思是需要将上载功能设置为可选功能,那么您可以执行以下操作:


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  = $this->upload->data();
    }
    return $returnArr; //just return the array, empty if no upload, file data if uploaded
}
希望这是有道理的

codeigniter文件上传可选…工作完美…:)

----------控制器---------

我只是用这条线,这使它有选择性

if($_FILES['userfile']['error'] != 4)
{
 return false;
}

$_FILES['userfile']['error'] != 4 is for file required to upload.
您可以使用
$\u文件['userfile']['error']使其不必要4
,则它会将此错误传递给所需文件和 如果有其他类型的错误,请使用返回false, 希望它对你有用

codeigniter文件上传可选…工作完美…:)

----------控制器---------

我只是用这条线,这使它有选择性

if($_FILES['userfile']['error'] != 4)
{
 return false;
}

$_FILES['userfile']['error'] != 4 is for file required to upload.
您可以使用
$\u文件['userfile']['error']使其不必要4
,则它会将此错误传递给所需文件和 如果有其他类型的错误,请使用返回false,
希望它能为你工作……

这是可行的,但是有没有一种方法,如果没有上传图像,我可以给['file_name']值:leeg.jpg?嗯,nvm,我用我想要的所有功能解决了这个问题。我会更新我的帖子!这是可行的,但是有没有一种方法,如果没有上传图像,我可以给['file_name']一个值:leeg.jpg?嗯,nvm,我用我想要的所有函数修复了这个问题。我会更新我的帖子!