无法使用Codeigniter上传图片 [英] unable to upload image using codeigniter

查看:92
本文介绍了无法使用Codeigniter上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Please help me. This is my first project in php Codeigniter. Actually i am a Java developer.

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

I want to upload two image path into my table and its image into my root folder eg uploads I have many fields in the same table other then images. I am able to add/edit those fields from my form.(i implemented succussfully using codeigniter)

但是目前我在图像上传中面临问题。我不知道如何使用Codeigniter上传图片。自两天以来,我一直试图自己做,但是我无法解决我的问题

But currently I am facing problem in image upload. I don't know exactly how to upload images using codeigniter. I tried to do it by myself since Two days but i could not solve my problem

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

Error : I am not seeing any error. simply it inserts 0 values into my db table as image path. I think the way i am try to upload image is not correct.

myviews.php

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(); ?>  


推荐答案

首先要记住的是CI不会添加 $ _ FILES 到输入对象。您将需要访问诸如 $ _ FILES ['img1'] 之类的文件。
这样的话:

First thing to remember is CI does not add $_FILES to the input object. You will need to access those like $_FILES['img1'] etc. So these:

'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

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

depending on what you expect to be storing in the database. You can rename files, etc through the upload class. I would suggest reading over those docs.

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

Secondly, you don't appear to be calling the actual upload method:

$this->upload->do_upload()

不确定是否需要此功能,但是...
如果您想要多个配置,则如果它们想要具有不同的路径,则必须重新定义多个文件的配置...

Not sure if you needed this but... If you want multiple configs, you have to redefine the config for multiple files if you want them to have different paths...

$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()且不传递任何参数。

and if you don't want them to have different paths, you can just load in the library as you do in your example and call do_upload() with no params passed.

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

If i missed the point or you need more info let me know and I may update.

这篇关于无法使用Codeigniter上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆