访问和读取PHP中不位于根文件夹中的文件

时间:2014-08-06 19:51:03

标签: php

我将$ userHome设置为C:/ Users /(此处为用户名)/ Desktop / Test

$userHome = realpath("./../../../../")."\\Desktop\\Test";

然后我在数组表单

中获取文件夹测试中的所有名称
$testFolderArray = scandir($userHome);

然后我得到$ testFolderArray的长度

count = count($testFolderArray);

然后我为test中的每个文件夹运行for循环。在for循环中,它读取了Test文件夹内每个文件夹内的data.txt和properties.txt,并将这些文件的内容存储在两个数组中。

for($x = 0; $x < $count; $x++) {

$dataFile = $testFolder."/data.txt";

$propertiesFile = $testFolder."/properties.txt";


//Reads the data inside of those files
$data = file_get_contents($dataFile);
$properties = file_get_contents($propertiesFile);

$dataArray[$x] = $data;
$propertiesArray[$x] = $properties;
}

运行此PHP文件时出现的错误是:

Warning: file_get_contents(C:\Users\ltrujillo\Desktop\Test/TestSub/data.txt): failed to open          stream: No such file or directory i C:\Users\ltrujillo\Desktop\PHP\wamp\www\StackOverFlow_Question.php on line 30

Warning: file_get_contents(C:\Users\ltrujillo\Desktop\Test/TestSub/properties.txt): failed to open stream: No such file or directory in C:\Users\ltrujillo\Desktop\PHP\wamp\www\StackOverFlow_Question.php on line 31

我知道问题在于它正在寻找一个包含PHP文件的根文件夹内的路径。那么我该如何更改它以便它可以读取根文件夹和指定路径下的文件的上下文?

如何解决:

for($x = 0; $x < $count; $x++) {
    if($testFolderArr[$x] == '.' || $testFolderArr[$x] == '..')
        continue; //filter . and .. directories

    $testFolder = $userHome.'\\'.$testFolderArr[$x]; 
    $dataFile = $testFolder."\\data.txt";

    $propertiesFile = $testFolder."\\properties.txt";

    if(!file_exists($dataFile) || !file_exists($propertiesFile)) 
        continue; //check for data and properties files and continue if they arent exist

    //Reads the data inside of those files
    $data = file_get_contents($dataFile);
    $properties = file_get_contents($propertiesFile);

    $dataArray[$x] = $data;
    $propertiesArray[$x] = $properties;
}

我的问题是我不小心命名我的文件data.txt而不是数据,所以程序说文件全名是'data.txt.txt'会导致很多错误。

1 个答案:

答案 0 :(得分:0)

更改此代码:

for($x = 0; $x < $count; $x++) {

$dataFile = $testFolder."/data.txt";

$propertiesFile = $testFolder."/properties.txt";

//Reads the data inside of those files
$data = file_get_contents($dataFile);
$properties = file_get_contents($propertiesFile);

$dataArray[$x] = $data;
$propertiesArray[$x] = $properties;
}

为:

for($x = 0; $x < $count; $x++) {
    if($testFolderArr[$x] == '.' || $testFolderArr[$x] == '..')
        continue; //filter . and .. directories

    $testFolder = $userHome.'\\'.$testFolderArr[$x]; //add this to obtain name of folder on each loop
    $dataFile = $testFolder."\\data.txt";

    $propertiesFile = $testFolder."\\properties.txt";

    if(!file_exists($dataFile) || !file_exists($propertiesFile)) 
        continue; //check for data and properties files and continue if they arent exist

    //Reads the data inside of those files
    $data = file_get_contents($dataFile);
    $properties = file_get_contents($propertiesFile);

    $dataArray[$x] = $data;
    $propertiesArray[$x] = $properties;
}