知识问答
如何修复织梦Dedecms保存远程图片到本地服务器的问题?
织梦CMS(DedeCMS)是一款广泛使用的开源内容管理系统,但在使用过程中可能会遇到无法将远程图片保存到本地服务器的问题,以下是针对这一问题的详细解决方法:
检查PHP配置
1、allow_url_fopen设置:
PHP配置文件(php.ini)中的allow_url_fopen
选项必须设置为On
,以允许通过URL打开文件。
如果allow_url_fopen
被禁用,可以通过编辑php.ini文件来启用它,找到disable_functions
项,确保其中没有包含fsockopen
。
2、upload_tmp_dir设置:
确保upload_tmp_dir
在php.ini中正确设置,并给予相应的目录读写权限。
修改DedeCMS代码
1、替换函数:
如果服务器上禁用了fsockopen
函数,可以在/include/dedehttpdown.class.php
文件中将其替换为stream_socket_client
函数,具体操作如下:
打开/include/dedehttpdown.class.php
文件。
找到第507行,将$this>m_fp = @fsockopen($this>m_host, $this>m_port, $errno, $errstr,10);
替换为$this>m_fp = @stream_socket_client($this>m_host . ':' . $this>m_port, $errno, $errstr,10);
。
保存文件并测试远程图片保存功能。
2、支持HTTPS:
对于使用HTTPS的网站,需要在dedehttpdown.class.php
中的PrivateStartSession
函数内添加代码以支持HTTPS,具体操作如下:
打开/include/dedehttpdown.class.php
文件。
在PrivateStartSession
函数的开头添加以下代码:
if ($this>m_scheme == "https") { $this>m_port = "443"; } if (function_exists('curl_init') && function_exists('curl_exec')) { $this>m_ch = curl_init(); curl_setopt($this>m_ch, CURLOPT_URL, $this>m_scheme.'://'.$this>m_host.':'.$this>m_port.$this>m_path); curl_setopt($this>m_ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this>m_ch, CURLOPT_FOLLOWLOCATION, 1); if (!empty($this>m_cookies)) { curl_setopt($this>m_ch, CURLOPT_COOKIE, $this>m_cookies); } if ($this>m_scheme == "https") { curl_setopt($this>m_ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($this>m_ch, CURLOPT_SSL_VERIFYHOST, false); } $this>m_html = curl_exec($this>m_ch); $status = curl_getinfo($this>m_ch); if (count($status) > 0) { foreach ($status as $key => $value) { $key = str_replace("_", "", $key); if ($key == "httpcode") { $this>m_httphead["httpstate"] = $value; } } } $this>m_error = curl_errno($this>m_ch); }
保存文件并测试远程图片保存功能。
其他注意事项
1、检查目录权限:
确保上传目录具有可写权限。
检查是否更改了图片上传目录,如果有更改,请确保新的目录存在且可写。
2、网络环境:
确认服务器可以访问外网,因为无法连接外网也会导致无法下载远程图片。
FAQs
1、Q: 为什么修改了dedehttpdown.class.php文件后仍然无法保存远程图片?
A: 确保已经清除缓存并重启服务器,如果问题依旧,请检查服务器是否有防火墙规则或其他安全设置阻止了相关操作,确认PHP版本和DedeCMS版本是否兼容。
2、Q: 如何快速判断服务器是否支持HTTPS?
A: 可以尝试在本地或服务器上运行一个简单的PHP脚本,使用curl
库发起一个HTTPS请求,看是否能成功获取响应,如果可以成功获取响应,则说明服务器支持HTTPS。