Reading txt file on network with php

104 views Asked by At

I need to read and parse the txt file on the network with PHP.

"Warning: file_get_contents(file//10.0.2.129/lims/lims.txt): failed to open stream: No such file or directory in C:\AppServ\www\mail\index.php on line 2"

This way I get the error message.

<?php
$txt_file = file_get_contents('file//10.0.2.129/lims/lims.txt');

function sp($x) {
    return preg_split("/\s\s+|\s*\((\d{4}).*\)/", $x,0,PREG_SPLIT_DELIM_CAPTURE);
}

$array = preg_split("/\n/", $txt_file);
$processed = array_map('sp', $array);

print_r(json_encode($processed));
?>
3

There are 3 answers

1
dragos.nicolae On

try using file_get_contents('./lims/lims.txt'); but make sure to choose the right path depending the location you are calling this function from.

3
Jerson On

Try this

\\\\10.0.2.129\\lims\\lims.txt
2
darkelfe On

file_get_contents can be used with local files or through protocols (listed here : https://www.php.net/manual/wrappers.php)

In your example, you missed the ":" between "file" and "//". And, based on IP looking, your file is on a shared disk, so you must add the network prefix \\ before.

So it should be file_get_contents('file://\\\\10.0.2.129/lims/lims.txt'). Since "file://" is the default protocol, you can remove it an only keep file_get_contents('\\\\10.0.2.129/lims/lims.txt')