I have set a crawler up in WordPress which grabs stocks data and writes it to file. When a user enters a symbol/ticker, if it matches the data of a previous crawl for that particular company’s data, it will echo the text file on page. If no data is found the crawler then grabs it and writes to file to save for the next time that symbol is used.
The problem I’m having is that when the content is written to file, it saves it in the WordPress root and not inside a subfolder of the theme as intended. I have tried bloginfo and absolute; both return the same failure.
This is the code I am using to write to file:
<?php
$CompDetails = "http://another.example.org/mattv1/wp-content/themes/stocks/tools/modules/Stock_Quote/company_details/$Symbol.txt";
if (file_exists($CompDetails)) {}
else
{
include ('crawler_file.php');
$html = file_get_html("http://example.com/?ticker=$Symbol:US");
$es = $html->find('div[class="detailsDataContainerLt"]');
$tickerdetails = ("$es[0]");
$FileHandle2 = fopen($CompDetails, 'w') or die("can't open file");
fwrite($FileHandle2, $tickerdetails);
fclose($FileHandle2);
}
?>
edit below
I also tried this and the same happens as above
<?php
if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/stocks/tools/modules/Stock_Quote/company_details/$Symbol.txt")) {}
else
{
include ('crawler_file.php');
$html = file_get_html("http://example.com/?ticker=$Symbol:US");
$es = $html->find('div[class="detailsDataContainerLt"]');
$tickerdetails = ("$es[0]");
$FileHandle2 = fopen($_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/stocks/tools/modules/Stock_Quote/company_details/$Symbol.txt", 'w') or die("can't open file");
fwrite($FileHandle2, $tickerdetails);
fclose($FileHandle2);
}
?>
Its probably some kind of file permissions issue. Also, you’d be much better off using the WordPress upload directories anyway since it needs to be writable to function properly anyway.
Use
wp_upload_dir()to get the uploads path. The return value will be an array with data pertaining to dated folders (which you don’t need) but you can get the base uploads dir name out of there and then use the info to create your own ‘ticker_data’ folder to store your data in.