Hi,
When we insert an image using FCKEditor and IMCE, the path is set as "/sites/default/files/myimage.jpg". Now if we move the site to sub-directory, it should be "sub-directory/sites/default/files/myimage.jpg". Updating one by one all nodes not possible. What is the fastest way?

Thanks

Comments

3cwebdev’s picture

Perform a search and replace query in your phpMyAdmin panel.

I am learning’s picture

Not sure how to do this? An example will be really helpful.
Thanks

mahyarsbt’s picture

if image urls inside node content

i know a manually method :
this way: export data > replace all X > import data

if you access to phpmyadmin and can export

go to your drupal DB > node_revisions
then click export data
uncheck save file

php my admin export your node data
select all at:
INSERT INTO `ak_node_revisions` (`nid`, `vid`, `uid`, `title`, `body`, `teaser`, `log`, `timestamp`, `format`) VALUES
...
..
.
to end

then copy and past in editor like notepad

use find/replace
find: "/sites/default/files/
replace: "sub-directory/sites/default/files/

replace all

then copy all

in phpmyadmin "node_revisions" empty data

click SQL top of phpmyadmin menu
in "Run SQL query/queries on database" delete all query

and paste changed data that you copied from notpad
INSERT INTO `ak_node_revisions` (`nid`, `vid`, `uid`, `title`, `body`, `teaser`, `log`, `timestamp`, `format`) VALUES
...
..
.
to end

I am learning’s picture

This sounds good, can we write an update sql statement for the same?

schnippy’s picture

You've got htaccess / clean_urls enabled, right? If you modify your htaccess file you can do this with a rewrite rule:

RewriteRule ^sites/default/files/(.*)$ sub-directory/sites/default/files/$1 [L]

You want to add this in your htaccess file right after the RewriteEngine On command:

<IfModule mod_rewrite.c>
  RewriteEngine on
  
  RewriteRule ^sites/default/files/(.*)$ sub-directory/sites/default/files/$1 [L]

  # If your site can be accessed both with and without the 'www.' prefix, you ... 
I am learning’s picture

Thanks, its a great solution but I would like to update this in DB directly.
Regards

mahyarsbt’s picture

REPLACE(str,from_str,to_str)
Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str.

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);

i think you must run it:

update XX_node_revisions set body = replace(body, ‘/sites/default/files/’, ‘sub-directory/sites/default/files/’);

Note: XX table prefix

I am learning’s picture

Great, thank you so much.