php CodeIgniter - 通过表单上传图像,将图像的位置存储在数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/288076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter - Uploading an Image through a form, store the location of the image in database
提问by scrot
I'm trying to upload an image to my site through a form, however it's much more efficient to (rather than bog down the database) just store the location of the image in the database.
我正在尝试通过表单将图像上传到我的网站,但是(而不是使数据库陷入困境)仅将图像的位置存储在数据库中会更有效率。
I'm having trouble with my form and really don't know where to go:
我的表格有问题,真的不知道去哪里:
<?=form_open('bro/submit_new');?>
//other form data
Image: <input type="file" name="image" size="20" /> <br>
<input type="submit" value="Submit" />
</form>
Now the form itself works fine, the problem is that it's trying to store the image into the database field 'image' (which is type TEXT). What's the easiest way to tell it to just store the file, and give the file location to store in the 'image' field? (I tell it where to upload the file via the controller).
现在表单本身工作正常,问题是它试图将图像存储到数据库字段“图像”(类型为 TEXT)中。告诉它只存储文件并在“图像”字段中提供要存储的文件位置的最简单方法是什么?(我告诉它通过控制器上传文件的位置)。
Thanks
谢谢
Edit: controller code (for this part):
编辑:控制器代码(对于这部分):
function submit_new(){
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->db->insert('post', $_POST);
redirect('bro');
}
回答by victoriah
CodeIgniter's file uploading class will do this for you. The entry in their user guideexplains as well as I could, so I'm going to point you there.
CodeIgniter 的文件上传类会为你做这件事。他们的用户指南中的条目尽我所能进行了解释,所以我将指向您那里。
Essentially you'd just need to modify the controller that they have there to include a bit where you put the file URL in the database, which you can accomplish easily by using $this->upload->data() and extracting [full_path] from the resulting array, and then sending it to a model which handles the database input.
本质上,您只需要修改他们在那里的控制器,将文件 URL 放入数据库中的位置包含一点,您可以通过使用 $this->upload->data() 并提取 [full_path] 轻松完成从结果数组,然后将其发送到处理数据库输入的模型。
回答by victoriah
use form_open_multipart() not form_open()
使用 form_open_multipart() 而不是 form_open()
回答by abcdef
use the model pages to upload data to database
使用模型页面将数据上传到数据库

