Luca Luca - 2 months ago 15
PHP Question

PHP - use of variable

I want my PHP code to display one out of N .txt files on my webpage. (If I visit example.com/index.php?l=1 it should display 1.txt) I´m using a variable for the numbers called $m. $m is defined correctly as it is working on other pages.

My problem is that I can´t define the location of "1.txt" with a variable. My code is:

$filename = "C:\xampp\project\Logs\.$m.'.txt';
if (file_exists($filename)) {
if (is_readable($filename)) {
$handle = fopen($filename, 'r');
if (filesize($filename) > 0) {
$contents = fread($handle, filesize($filename));
fclose($handle);
$contents = str_replace('\r\n', "\r\n", $contents);


echo ' <textarea data-autoresize class="form-control vresize" id="info" rows="20">'.$contents.'</textarea>';
} else {
echo ' <textarea data-autoresize class="form-control vresize" id="info" rows="20"></textarea>';
}
}
} else {
echo '<br><font color=red>Cant get any Logs for ID: '.$m.'</font>';
}


I tried a few different things but nothing worked out for me because $m does not get resolved in $filename.

Was anybody able to follow my description and understands my problem? Help is very appreciated!

Answer

Try:

$filename = 'C:/xampp/project/Logs/' . $m . '.txt';

You had mismatched quotes. I've also changed the backslashes to forward slashes, because backslashes have special meaning in PHP strings; Windows allows either style to be used in pathnames.