无法使用codeigniter上传图片

时间:2013-01-16 18:56:01

标签: php codeigniter image-uploading

请帮帮我。这是我在php Codeigniter中的第一个项目。实际上我是一名Java开发人员。

我想将两个图像路径上传到我的表中,并将其图像上传到我的根文件夹中,例如上传我在同一个表中除了图像之外还有很多字段。我能够从我的表单中添加/编辑这些字段。(我使用codeigniter成功实现)

但目前我在图片上传方面遇到问题。我不确切知道如何使用codeigniter上传图像。我试着自己做了两天,但我无法解决我的问题

错误:我没有看到任何错误。只需将0值作为图像路径插入到我的db表中。 我认为我尝试上传图片的方式不正确。

myviews.php

 <? echo form_open_multipart('Booksetups/book'); ?>


                 <input type="file" name="img1" /> 
                 <input type="file" name="img2" />
               <?php          
                                               <br/>
                          <? echo  form_submit($submitbtn);   echo form_reset($resetbtn); ?>  
                 <? echo form_close(); ?>  

3 个答案:

答案 0 :(得分:2)

首先要记住的是CI不会将$_FILES添加到输入对象。您需要访问$_FILES['img1']等内容。 所以这些:

'img1'=>$this->input->post('img1'),//image path is not inserting But all other fields are inserting into db 
'img2'=>$this->input->post('img2'),//image path is not inserting

应该是这样的:

'img1'=>$_FILES['img1']['name'],//image path is not inserting But all other fields are inserting into db 
'img2'=>$_FILES['img2']['name'],//image path is not inserting

取决于您希望存储在数据库中的内容。您可以通过上传类重命名文件等。我建议阅读那些docs.

其次,您似乎没有调用实际的上传方法:

$this->upload->do_upload()

不确定你是否需要这个...... 如果您需要多个配置,如果您希望它们具有不同的路径,则必须重新定义多个文件的配置...

$config['upload_path'] = 'uploads/'; 
$config['allowed_types'] = 'gif|jpg|jpeg|png'; 
$config['max_size'] = '1000'; 
$config['max_width'] = '1920'; 
$config['max_height'] = '1280';  
$this->load->library('upload', $config);
$this->upload->do_upload("img1");

$config['upload_path'] = 'some_other_dir/'; 
$config['allowed_types'] = 'gif|jpg|jpeg|png'; 
$config['max_size'] = '1000'; 
$config['max_width'] = '1920'; 
$config['max_height'] = '1280';  
$this->upload->initialize($config);
$this->upload->do_upload("img2");

如果您不希望它们具有不同的路径,您可以像在示例中一样加载库,并在没有传递参数的情况下调用do_upload()

如果我错过了这一点,或者您需要更多信息,请告诉我,我可能会更新。

答案 1 :(得分:0)

问题是你在服务器端使用了html。 PHP不知道HTML而是php。客户端或浏览器都知道HTML。

<input type="file" name="img1" /> 
<input type="file" name="img2" />

使用合适的php方法生成html。

第二件事,您的文件上传参数与codeigniter userguide

不同
$config['upload_path'] = 'uploads/';
$config['upload_path'] = './uploads/'; 

错误的文件路径可能会导致您的其他问题

答案 2 :(得分:-1)

您似乎没有初始化上传类库:

$this->upload->initialize($config);

你也加载了这个库:

$this->load->library('upload');