简体   繁体   English

无法使用Codeigniter上传图片

[英]unable to upload image using codeigniter

Please help me. 请帮我。 This is my first project in php Codeigniter. 这是我在php Codeigniter中的第一个项目。 Actually i am a Java developer. 其实我是Java开发人员。

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. 我不知道如何使用Codeigniter上传图片。 I tried to do it by myself since Two days but i could not solve my problem 自两天以来,我一直试图自己做,但是我解决不了问题

Error : I am not seeing any error. 错误:我没有看到任何错误。 simply it inserts 0 values into my db table as image path. 只需将0值作为图像路径插入到我的数据库表中即可。 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(); ?>  

First thing to remember is CI does not add $_FILES to the input object. 要记住的第一件事是CI不会将$_FILES添加到输入对象。 You will need to access those like $_FILES['img1'] etc. So these: 您将需要访问$_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

should be something like: 应该是这样的:

'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");

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. 如果您不希望它们具有不同的路径,则可以像在示例中一样加载库,并在不传递任何参数的情况下调用do_upload()

If i missed the point or you need more info let me know and I may update. 如果我错过了重点,或者您需要更多信息,请告诉我,我可能会更新。

Problem is you have use html in serverside. 问题是您在服务器端使用了html。 PHP doesn't know HTML but php. PHP不懂HTML,但不懂PHP。 Client side or browser knows HTML. 客户端或浏览器知道HTML。

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

Use suitable php method to generate html. 使用适当的php方法生成html。

second thing, Your file upload parameter is different fromo codeigniter userguide 第二件事,您的文件上传参数不同于Codeigniter用户指南

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

wrong file path may cause your otherproblems 错误的文件路径可能会导致其他问题

It seems that you are not initializing the upload class library: 看来您没有初始化上传类库:

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

Also do you load the library: 您是否还要加载库:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM