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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。