应用程序将文件保存到root而不是主题子文件夹

时间:2011-01-14 10:59:22

标签: wordpress path

我在Wordpress中设置了一个抓取工具,它抓取股票数据并将其写入文件。当用户输入符号/代码时,如果它与该特定公司的数据的先前爬网的数据匹配,则它将在页面上回显该文本文件。如果没有找到数据,则爬虫会抓取它并写入文件以保存以供下次使用该符号。

我遇到的问题是,当内容写入文件时,它会将其保存在Wordpress根目录中,而不是按照预期保存在主题的子文件夹中。我试过bloginfo和绝对的;两者都会失败。

这是我用来写入文件的代码:

<?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);
}
?>

在下面编辑

我也尝试了这个,并且发生了同样的事情

<?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);  
}  
?>  

2 个答案:

答案 0 :(得分:0)

它可能是某种文件权限问题。此外,无论如何,你最好还是使用WordPress上传目录,因为无论如何它都需要可写,才能正常运行。

使用wp_upload_dir()获取上传路径。返回值将是一个数组,其中包含与日期文件夹相关的数据(您不需要)但您可以从那里获取基本上传目录名称,然后使用该信息创建您自己的'ticker_data'文件夹来存储您的数据英寸

答案 1 :(得分:0)

这就是我现在所处的位置:

  • 我在上传中创建了一个文件夹,并将其命名为“company_details”,
  • 上传文件夹和公司详情文件夹中的写入权限设置为777,
  • 我使用过这段代码:
$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);  
}  

它不再将文件写入root,但它现在抛出“无法打开文件”错误