【问题标题】:Unable to display output with php echo [closed]无法使用 php echo 显示输出 [关闭]
【发布时间】:2015-09-06 20:05:42
【问题描述】:

我正在处理 wordpress 文件,但无法显示总下载次数。

“下载计数”是我保存在活动主题文件内服务器上的文本文件“count.txt”中的数字。

现在为了显示计数,我使用下面的代码。

<?php 
$url= echo get_the_title();

echo file_get_contents( "http://example.com/wp-content/themes/themename/download/$url/count.txt" ); 
//the URL of the text file consist of current wordpress post title. 
?>

此代码无法显示 count.txt 。请帮帮我

【问题讨论】:

  • 这将失败 $url= echo get_the_title(); 一方面。 echo 不合适,应该返回解析错误。只需删除回声
  • 这不起作用,还有其他方法吗? count.txt的网址是>>当前文章的example.com/wp-content/themes/themename/download/title/count.txt
  • 该文件是否实际存在于服务器上并且可由运行代码的用户 (php/apache) 访问?另外,权限检查目​​录/download
  • 无论哪种方式,都使用错误报告。回声$url 没有回声,看看会出现什么。如果它显示完整的 URL,那么您需要重新考虑这一点。
  • 是的,它可用,问题是 count.txt 的 URL 有 wordpress 中当前帖子的标题,所以我必须回显 get_the_title()

标签: php wordpress


【解决方案1】:

您应该首先从$url 设置中删除echo,然后在使用file_get_contents() 时使用rawurlencode() 以确保URL 编码正确:

$url = get_the_title();

echo file_get_contents( 'http://example.com/wp-content/themes/themename/download/'.rawurlencode($url).'/count.txt' ); 

【讨论】:

  • 感谢您的回答,但仍然无法在文本文件中显示数字计数。文本文件的 URL 是 >> eragenx.com/wp-content/themes/Tesseract/download/…
  • 似乎这个站点需要rawurlencode() 而不是urlencode()。我已经相应地编辑了上面的答案。
  • rawurlencode() 有效。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 2022-12-10
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
相关资源
最近更新 更多