-1

I Am trying to make a php program open a text file and read the contents. what i need to do is change the filename but keep the .txt extension.

$Task = $_POST['Task'];

$Breifing = file_get_contents("T001.txt");

So in the above example i would like to make T001 change depending on what the contents of $Task is.

I have Tried This line:

%Breifing = file_get_contents(%Task,".Txt");

And that didnt work.

Any Help would be Greatly Appreciated.

  • 1
    What about `$Breifing = file_get_contents($Task.".txt");` – mikesp Mar 03 '21 at 12:32
  • 1
    See [Mixing a PHP variable with a string literal](https://stackoverflow.com/questions/5368890/mixing-a-php-variable-with-a-string-literal). – El_Vanja Mar 03 '21 at 12:35

1 Answers1

0

You might find this a good read when you have a chance. To answer your question though, there are a few different ways to do this:

file_get_contents(sprintf('%s.txt', $Task)) // will probably work
// OR
file_get_contents("{$Task}.txt")
// OR (as mentioned in the comments above)
file_get_contents($Task . '.txt')
// OR since $Task is just a string you could concatenate '.txt' on it and just pass it wholesale
$Task = $Task . '.txt';
file_get_contents($Task);