PHP Get path of file -
somewhere in pc there txt file, test.txt let's call it, location may vary pc pc. there way, path can read? can use of course
$file = file_get_contents(path); but comes when know current path. how can retrieve path in case? ty
if on linux/unix box, can use locate , parse result. windows has similar solution:
<?php $search = "test.txt"; $result = shell_exec("locate $search"); //array of files test.txt in name $matchingfiles = explode(php_eol, $result); //that gets files may named else test in name //like phptest.txt rid of junk $files = array(); //array possible candidates stored if (!empty($matchingfiles)) { //we found @ least 1 foreach ($matchingfiles $f) { //if file named test.txt, , not phptest.txt if (basename($f) === $search) { $files[] = $f; } } } if (empty($files)) { //we didn't find echo $search . ' not found.'; } else { if (count($files) > 1) { //we found many. 1 want? echo "more 1 match found." . php_eol; echo print_r($files, true); } else { //then found $file = file_get_contents($files[0]); } }
Comments
Post a Comment