0

I am working on wordpress file, and I am having trouble with displaying total download count.

"Download count" is a number that I have saved in a text file "count.txt" located on server inside the active theme file.

Now in order to display the count, I am using the below code.

<?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. 
?>

This code is unable to display count.txt . Please help me out

  • this will fail `$url= echo get_the_title();` for one thing. echo's out of place and should be returning a parse error. just remove the echo – Funk Forty Niner Sep 06 '15 at 20:14
  • this didnt work, is thr any other approach? the URL of the count.txt is >> http://example.com/wp-content/themes/themename/download/title of the current article/count.txt – Saurabh Chaudhary Sep 06 '15 at 20:15
  • Does the file actually exist on the server and is accessible by the user that's running the code (php/apache)? Also, permission check the directory `/download` – ScottMcGready Sep 06 '15 at 20:16
  • either way, use error reporting. Echo `$url` without the echo in there and see what shows up. If it shows a full URL, then you need to rethink this. – Funk Forty Niner Sep 06 '15 at 20:18
  • yes its available, the problem is the URL of count.txt has the title of current post in wordpress, so i have to echo out the get_the_title() – Saurabh Chaudhary Sep 06 '15 at 20:18
  • I check the code and wordpress tag echo get_the_title() displays the title of the current post. thats exactly what i need in my url, is their any other approach to achieve this? – Saurabh Chaudhary Sep 06 '15 at 20:26
  • @Fred-ii- its still not working.. the exact URL is http://eragenx.com/wp-content/themes/Tesseract/download/Freelancer%20portfolio%20theme/count.txt << here Freelancer portfolio theme is title of the current post, as u can see that the link is accessible. how can i display it with file_get_contents() – Saurabh Chaudhary Sep 06 '15 at 20:47

1 Answers1

1

You should first remove the echo from the $url settings, and then use rawurlencode() when using file_get_contents() to make sure the URL is properly encoded:

$url = get_the_title();

echo file_get_contents( 'http://example.com/wp-content/themes/themename/download/'.rawurlencode($url).'/count.txt' ); 
uri2x
  • 3,162
  • 1
  • 11
  • 25
  • Thank you for the answer, but still its not able to display the number count in the text file. the URL of the text file is >> http://eragenx.com/wp-content/themes/Tesseract/download/Freelancer%20portfolio%20theme/count.txt << how can I display it with file_get_contents , as u can see that the URL is accessible – Saurabh Chaudhary Sep 06 '15 at 20:43
  • Seems like this site requires `rawurlencode()` instead of `urlencode()`. I've edited my answer above accordingly. – uri2x Sep 06 '15 at 20:46
  • rawurlencode() worked. Thank you so much – Saurabh Chaudhary Sep 06 '15 at 20:54