[英]How to replace white spaces with %20 in folder names containing images?
我的朋友opencart页面上的W3验证程序出现错误:
对于元素img上的属性src,路径http://myserver.com/cache/data/product/MY PRODUCT / product1-290x214.JPG的值错误。 使用%20代替空格。
这表示用户创建了带有空格的文件夹(我的产品)。
需要修改代码以将文件夹名称中的空格转换为%20的地方?
使用urlencode 。 这样可以解决问题。
如果您拥有完整的URL,则简单的str_replace()应该可以解决问题。 像这样:
echo str_replace(' ','%20',$full_link);
否则,您可以按照一位评论者的建议使用urlencode() 。 如果您已经有一个指向该站点的路径,并且仅添加了诸如产品名称之类的内容,这将非常有用:
echo 'http://site.name/hello/'.urlencode($product);
尽管urlencode()
是您应该使用的那个,但是您在这里应该做的是教育您的朋友不要再这样做,将其重命名为没有空间并更新数据库中的路径。 如果您想要一个解决方案来纠正购物车中所有页面上的错误,则可以在/catalog/modl/tool/image.php
文件中使用urlencode()
或str_replace()
$new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
将需要更改,或者在其后添加一行以添加%20
,或者编辑该行以将其包括在一行中。 当然,您还应该在vQmod中执行此操作,而不只是编辑您的核心文件,这样当您朋友的网站更新到较新版本时,该文件仍将保留
如果上面的代码不起作用,则可以在直接构建返回的URL之后对其进行操作。 这是两条返回线(第一条返回HTTPS图像,第二条返回HTTP)
return HTTPS_IMAGE . $new_image;
} else {
return HTTP_IMAGE . $new_image;
如果文件夹名称或图像文件名中包含OpenCart图像调整大小模型,则该模型实际上返回带空格的图像路径。
尝试使用Rationalboss建议的解决方法来编辑catalog / model / tool / image.php最后几行:
return $this->config->get('config_ssl') . 'image/' . str_replace(' ','%20', $new_image);
和
return $this->config->get('config_url') . 'image/' . str_replace(' ','%20', $new_image);
Opencart仍然上传带有空格,UTF-8字符等的图像。我认为,必须在下一版本的Opencart中修复此问题。
简单的解决方案是编辑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);
}
在您的/catalog/model/tool/image.php文件中
$new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type .'.' . $extension;
加
$new_image = implode('/', array_map('rawurlencode', explode('/', $new_image)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.