Luca Luca - 1 year ago 47
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 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));
$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 Source


$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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download