1

I have a text file on my server, which I would like to display in a Label (or similar element). What is the best way to do this? If possible, I would like it to be formatted the same way as it is in the text file.

(I was thinking it would be similar to using "src" for images, but turns out you can't do that).

6
  • 1
    You can use fetch: stackoverflow.com/questions/50401390/… Commented Feb 22, 2022 at 9:12
  • 1
    What kind of server? What code do you have already? How are you passing data to your front end. Commented Feb 22, 2022 at 9:13
  • @FivePlyPaper maybe "server" is a bit misleading. It is a file sitting in a folder beside my HTML Commented Feb 22, 2022 at 9:20
  • @Stiegi I've tried this method and there is an error claiming "URL scheme "file" is not supported.". Is there no way to make this work locally too? Commented Feb 22, 2022 at 9:23
  • You have to spin up a HTTP server to make it work with fetch Commented Feb 22, 2022 at 9:58

2 Answers 2

1

I would try reading the file with PHP, process the text (split in parts, cut, whatever) and set up an array with the required texts.You can call it later with using the array.

Finally ur code should look something like this:

<img src="<?=$arrayText['test1]?>" class=".." alt="...">

More information

PHP read .txt files: https://www.w3schools.com/php/php_file_open.asp

Several functions for processing strings: https://www.php.net/manual/de/ref.strings.php

Several functions for using arrays in PHP: https://www.php.net/manual/de/ref.array.php

Sign up to request clarification or add additional context in comments.

Comments

0

You will need to have a local web server (NodeJS, Python, etc.) to serve your local assets and then either render the HTML file dynamically (eg: via Express for NodeJS) or make a fetch request (eg: fetch('/path/to/file.txt'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.