dp6319 2014-08-06 19:51
浏览 16
已采纳

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

I set $userHome to C:/Users/(Users name here)/Desktop/Test

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

Then I get all of the names inside of the folder test inside of a array form

$testFolderArray = scandir($userHome);

Then I get the length of $testFolderArray

count = count($testFolderArray);

Then I run a for-loop for each of the folders inside of test. Inside of the for-loop it reads data.txt and properties.txt which are inside of each folder inside of the Test folder and stores content of the files inside of the two arrays.

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

The error I get when I run this PHP file is:

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

I know that the issue is that it is looking for a path that is inside of the root folder with the PHP file. So how could I change it so that it can read the context of file that is out the root folder and at the path specified?

How to fix:

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

My issue was that I accidentally names my file data.txt and not data so the program said that the files full name was 'data.txt.txt' which would cause a lot of errors.

  • 写回答

1条回答 默认 最新

  • doulu1968 2014-08-06 19:57
    关注

    Change this code:

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

    to:

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置