简体   繁体   English

如何在包含图像的文件夹名称中用%20替换空格?

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

I'm getting error in W3 Validator on my friends opencart page: 我的朋友opencart页面上的W3验证程序出现错误:

Bad value http://myserver.com/cache/data/product/MY PRODUCT/product1-290x214.JPG for attribute src on element img: Whitespace in path component. 对于元素img上的属性src,路径http://myserver.com/cache/data/product/MY PRODUCT / product1-290x214.JPG的值错误。 Use %20 in place of spaces. 使用%20代替空格。

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? 需要修改代码以将文件夹名称中的空格转换为%20的地方?

Use urlencode . 使用urlencode This will solve the issue. 这样可以解决问题。

A simple str_replace() should do the trick if you have the full URL with you. 如果您拥有完整的URL,则简单的str_replace()应该可以解决问题。 Something like this: 像这样:

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

Otherwise, you may do as suggested by one commenter to use urlencode() . 否则,您可以按照一位评论者的建议使用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. 尽管urlencode()是您应该使用的那个,但是您在这里应该做的是教育您的朋友不要再这样做,将其重命名为没有空间并更新数据库中的路径。 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 如果您想要一个解决方案来纠正购物车中所有页面上的错误,则可以在/catalog/modl/tool/image.php文件中使用urlencode()str_replace()

        $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. 将需要更改,或者在其后添加一行以添加%20 ,或者编辑该行以将其包括在一行中。 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 当然,您还应该在vQmod中执行此操作,而不只是编辑您的核心文件,这样当您朋友的网站更新到较新版本时,该文件仍将保留

EDIT 编辑

If the above code isn't working for it, you could manipulate the returned URL's after they've been built directly. 如果上面的代码不起作用,则可以在直接构建返回的URL之后对其进行操作。 These are the two return lines (first for HTTPS images, second for HTTP) 这是两条返回线(第一条返回HTTPS图像,第二条返回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. 如果文件夹名称或图像文件名中包含OpenCart图像调整大小模型,则该模型实际上返回带空格的图像路径。

Try to edit catalog/model/tool/image.php last lines with rationalboss suggested workaround: 尝试使用Rationalboss建议的解决方法来编辑catalog / model / tool / image.php最后几行:

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. Opencart仍然上传带有空格,UTF-8字符等的图像。我认为,必须在下一版本的Opencart中修复此问题。

Simple solution is to edit catalog/model/tool/image.php 简单的解决方案是编辑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 在您的/catalog/model/tool/image.php文件中

$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)));

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

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