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
5
  • Is this ampersand here for a reason? --> $current = &$current[$parts[$i-1]]; Commented Jan 22, 2018 at 23:09
  • 1
    @user3232849: Could you give the string on line 39 ? This is where the error occurs. Commented Jan 22, 2018 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 " Commented Jan 22, 2018 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 Commented Jan 23, 2018 at 0:00
  • The function runs on other paths without issue, but for some reason this path messes it up Commented Jan 28, 2018 at 22:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.