简体   繁体   中英

How to replace white spaces with %20 in folder names containing images?

I'm getting error in W3 Validator on my friends opencart page:

Bad value http://myserver.com/cache/data/product/MY PRODUCT/product1-290x214.JPG for attribute src on element img: Whitespace in path component. Use %20 in place of spaces.

That indicates that user created folder with white spaces (MY PRODUCT).

Where code need to be modyfied to convert white spaces in folder names to %20?

Use urlencode . This will solve the issue.

A simple str_replace() should do the trick if you have the full URL with you. Something like this:

echo str_replace(' ','%20',$full_link);

Otherwise, you may do as suggested by one commenter to use urlencode() . This is useful if you have a path to the site already and are just adding something like product name:

echo 'http://site.name/hello/'.urlencode($product);

While urlencode() is the one you should be using, the thing you should do here is educate your friend into not doing this again, renaming it to have no space in it and updating the paths in the database. If you want a solution that will rectify this in your cart across all pages in your cart, you could use urlencode() or str_replace() in your /catalog/modl/tool/image.php file

        $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;

would need to be changed, either adding a line after it to add the %20 , or editing that line to include it in one single line. Of course you should also do this in a vQmod not just edit your core files so that it will remain when your friend's site is updated to newer versions

EDIT

If the above code isn't working for it, you could manipulate the returned URL's after they've been built directly. These are the two return lines (first for HTTPS images, second for HTTP)

        return HTTPS_IMAGE . $new_image;
    } else {
        return HTTP_IMAGE . $new_image;

Opencart image resize model really returns image path with spaces, if You have such in folder names or image filenames.

Try to edit catalog/model/tool/image.php last lines with rationalboss suggested workaround:

return $this->config->get('config_ssl') . 'image/' . str_replace(' ','%20', $new_image);

and

return $this->config->get('config_url') . 'image/' . str_replace(' ','%20', $new_image);

Opencart still uploads images with spaces, UTF-8 characters etc. I think, this must be fixed in next Opencart versions.

Simple solution is to edit catalog/model/tool/image.php

    if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
        return $this->config->get('config_ssl') . 'image/' . str_replace(' ','%20', $new_image);
    } else {
        return $this->config->get('config_url') . 'image/' . str_replace(' ','%20', $new_image);
    }

In your /catalog/model/tool/image.php file after

$new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type .'.' . $extension;

add

$new_image = implode('/', array_map('rawurlencode', explode('/', $new_image)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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