Welcome Guest, Not a member yet? Register   Sign In
Store img tags in database and retrieve it
#1

(This post was last modified: 05-14-2016, 12:40 PM by pb.sajjad.)

Hi to all developers...


When I developing an web application on localhost, every time that I want use <img> tag, I use base_url(); method to referring to image's path.

<img src="<?= base_url(); ?path/to/image/sample.jpg>" />

with this, when I migrate to real live server, everything is good. because using base_url() method.

Now I have some question:
1. I'm developing a CMS app that I write posts and store them in database table. I use CKeditor and in fact, store html tags in table. my question is how could I store and use images tags in table? As you know, I must prepare url (path) to reach images, and when I was developing app on localhost and write some posts, how could I handle this on live server? In fact, I must have a relative approach that there will be not any problems on different main urls. I could not store something like this 
<img src="<?= base_url(); ?path/to/image/sample.jpg>" /> into table. so what could I do?

2. I could use data-uri approach for storing images, with this I solve the problem, also, there will be less requests to server and in fact reduce http requests. but... Is this approach is good and efficient?

thanks to all experts that will be guide and answer to this questions...
Reply
#2

store just the image name in your database: sample.jpg
deal with the rest of the image path in a different way - helper, site wide variable, etc.
Reply
#3

(05-14-2016, 12:57 PM)cartalot Wrote: store just the image name in your database: sample.jpg
deal with the rest of the image path in a different way - helper, site wide variable, etc.

I think a misunderstanding occurred!!!
I have a column named "post_body" and hundreds of different tags will be insert in it. some of these tags are <img> tags. there is not just one image. In controller, I called a model function, retrieve related post info and passed to view. and all of the html tags will be printed.
Reply
#4

well.... is it consistent? if it is then you could just use string replace http://php.net/manual/en/function.str-replace.php
Reply
#5

I know these possible approaches:

1. If you know how, you may hack CKEditor to paste relative URI's. Long time ago I did such thing on the old FCKEditor, for CKEditor I have not researched how this could be done.

1.1. Then you can add in the head of your master HTML template the tag
Code:
<base href="<?php echo base_url() ?>" />
A complication with links like
Code:
<a href="#top">Top</a>
would need to be dealt, the current URI (with the query string, if any) should prepend them.

1.2. Or, without touching the HTML template, you can scan the HTML fragments from database for relative URI's of images and to prepend them with the base URL before outputting.

(1.) looks like a difficult way. I think, it worths if you create your own CMS that you are going to use multiple times for your projects.

2. The lazy way: You can leave CKEditor to paste absolute URI's, as it does this by default. Before moving your site on the production server, you can open the database dump with a text editor (choose one that is able to edit large files), and using search/replace you can adjust the absolute URI's as the should be in the new place.
Reply
#6

(This post was last modified: 05-15-2016, 06:24 AM by InsiteFX.)

You can read this post on CKEditor:

Image Path

I use this filemanager with CKEditor and the links are setup like above link.

Responsive Filemanager

The filemanager has an image path setting in its php config file.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(This post was last modified: 05-15-2016, 06:40 AM by pb.sajjad.)

(05-15-2016, 06:23 AM)InsiteFX Wrote: You can read this post on CKEditor:

Image Path

I use this filemanager with CKEditor and the links are setup like above link.

Responsive Filemanager

The filemanager has an image path setting in its php config file.

how this will help? If I insert hundreds of img tags into database while developing on localhost, how the img source path that stored in database, will be proper path on live server while outputting images?
Reply
#8

You are going to have write your own custom method to look for the image tag and then string replace it with the new link.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

If ever you've developed with WordPress, this will be a familiar issue. All <img> tags will contain the site's base URL, and it's stored in a couple of other places too.

Rather than hack around with the WYSIWYG content editor, when we migrate a site from development to production we simply run a few queries like:

UPDATE table SET field = REPLACE(field, "http://oldurl", "http://newurl");
Reply




Theme © iAndrew 2016 - Forum software by © MyBB