开发者

App saving file to root and not theme subfolder

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/m开发者_如何学编程odules/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.


Here's where I stand now:

  • I have created a folder in uploads and named it "company_details",
  • Write permissions are set to 777 on both uploads folder and company details folder, and
  • I have used this code:
$CompDetails = wp_upload_dir();   
if (file_exists($CompDetails['basedir'].'/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($CompDetails, 'w') or die("can't open file");  
fwrite($FileHandle2, $tickerdetails);  
fclose($FileHandle2);  
}  

It is no longer writing the file into root, however it is now throwing out the "can't open file" error

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜