Make append in a specific line of a text file

0

Good morning, I have a function that makes an append in a certain line of my xml file.

function addInFile($file, $lineNumber, $content)
{
    $fileTemp = "$file.bak";
    $currentLine = 0;

    $fpRead = fopen($file, 'r');
    $fpWrite = fopen($fileTemp, 'w');

    try {
        if ($fpRead) {
            while (($linha = fgets($fpRead)) !== false) {
                if ($currentLine == $lineNumber) {
                    $linha .= $content . PHP_EOL;
                }

                fwrite($fpWrite, $linha);
                $currentLine += 1;
            }
        }
    } catch (\Exception $err) {
        echo $err->getMessage() . PHP_EOL;
    } finally {
        fclose($fpRead);
        fclose($fpWrite);
        unlink($file);
        rename($fileTemp, $file);
    }
}

I just need to save my file outside the root of the laravel project, I set the config/filesystem.php to receive a disk that I called public

'public' => [
            'driver' => 'local',
            'root' => storage_path('../../../public_html/xmls'),
            'visibility' => 'public',
        ],

My problem is that I do not know how to transform fopen, fgets, fclose, unlink, rename , etc ... with Storage::disk() do laravel . If anyone can help me, thank you in advance.

    
asked by anonymous 03.10.2016 / 15:47

1 answer

0

I was able to resolve it.

The php opens the files that are out of the root of laravel, the only thing I had to do was pass the complete path of the file.

In a function created in another class, it looks something like this:

public function test(){
      $url = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix();
      $fileManager->addInFile($url . $path, 7, $content);
}
    
03.10.2016 / 17:12