0

I have a file with file paths that is 802,876 lines, each line having a file path and info. I tried using the code here: PHP multidimensional array representing filepath and other code I found, but none work with my file, when using the whole file I get the error:

PHP Fatal error: [] operator not supported for strings in /root/test.php on line 39

When I use a sample of the lines where this error is occurring, my output from the function is just empty. The code i'm using is:

<?php
ini_set('memory_limit', '1024M');  

$fd = fopen ("results.txt", "r");
while (!feof ($fd))
{
    $buffer = fgets($fd, 4096);
    $lines[] = $buffer;
}
fclose ($fd);

$aryBackupData = parse_paths_of_files($lines);
unset($lines);

function parse_paths_of_files($array)
{
        $result = array();
        foreach ($array as $strFileLineData)
        {
        if(strpos($strFileLineData,"^") !== false) {
                    $aryFileLineData = explode("^",$strFileLineData);
                    if(substr($aryFileLineData[0],0,1) == "d")
                            $strType = "1"; // Folder/Directory
                    else
                            $strType = "2"; // File
                    $strItemPath = $aryFileLineData[2];
            if(strpos($strItemPath,"/") !== false) {
                        $strItemPath = substr_replace($strItemPath, $strType, strrpos($strItemPath,"/")+1, 0);
                        $parts = explode('/', $strItemPath);
                $current = &$result;
                for ($i = 1, $max = count($parts); $i < $max; $i++)
                {
                    if (!isset($current[$parts[$i-1]]))
                    {
                        $current[$parts[$i-1]] = array();
                    }
                    $current = &$current[$parts[$i-1]];
                }
                $current[] = $parts[$i-1];
            }
        }
        }
        return $result;
}
?>

Here are the sample lines from the file that is causing an issue and I cant see why it is:

-rw-r--r--^Fri, 2018-01-19 07:07:40^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/index.html
drwxr-xr-x^Tue, 2016-11-01 03:46:47^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/11
-rw-r--r--^Fri, 2018-01-19 07:07:40^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/11/index.html
-rw-r--r--^Tue, 2016-11-01 03:46:47^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/11/Informe-Alternativo-NNA-Ecuador.pdf
drwxr-xr-x^Mon, 2016-10-31 08:21:15^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/10
-rw-r--r--^Fri, 2018-01-19 07:07:40^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/10/index.html
-rw-r--r--^Mon, 2016-10-31 07:45:18^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/10/Informe-Alternativo-Ecuador.docx
-rw-r--r--^Mon, 2016-10-31 08:21:15^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/10/Alternative-Report-English.docx
drwxr-xr-x^Fri, 2016-12-09 06:14:23^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/12
-rw-r--r--^Fri, 2018-01-19 07:07:40^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/12/index.html
-rw-r--r--^Fri, 2016-12-09 06:14:26^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/12/Consultation-Children-and-adolescents.pdf
drwxr-xr-x^Wed, 2017-03-01 08:40:37^var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2017
user3232849
  • 13
  • 1
  • 3
  • Is this `ampersand` here for a reason? --> `$current = &$current[$parts[$i-1]];` – Zak Jan 22 '18 at 23:09
  • 1
    @user3232849: Could you give the string on line 39 ? This is where the error occurs. – TigerTV.ru Jan 22 '18 at 23:17
  • Its part of the code I got, but its used as a pointer, I dont know enough to know why its there. Here is the var_dump at the error: [12]=> string(4) "110 " [10]=> array(3) { [0]=> string(12) "2index.html " [1]=> string(34) "2Informe-Alternativo-Ecuador.docx " [2]=> string(33) "2Alternative-Report-English.docx " } } string(4) "110 " – user3232849 Jan 22 '18 at 23:52
  • The line right be fore the error is: var/www/vhtdocs/userweb12345/html/wp-content/uploads/gravity_forms/21-3d41a48e11dac6f966264ca0771ad7e1/2016/12/2index.html which is the tenth line in my example – user3232849 Jan 23 '18 at 00:00
  • The function runs on other paths without issue, but for some reason this path messes it up – user3232849 Jan 28 '18 at 22:25

0 Answers0