How to display content of a text file that is named the same as the data in a another text file

I have one text file that is updated with 4 characters periodically, I need to read this file and echo the content of another text file named the same name as the updated text file characters. For example, in the periodically updated text file, there might be “K04EW” or similar on the first line. There would be in the same directory a text file called K04EW.txt and others in which I need to echo the content of those files.

While it may seem that using data stored in files is a simple way of doing this, it’s not really the simplest or the best way. Due to file ownership/permissions (varies with how are the files being put/edited on the server), operating system file letter-case usage, different users creating/editing files with different formatting, …, using files takes more code and requires more effort and knowledge on the part of the users of the system. You should instead store data in a database and provide a simple and consistent user interface for creating and managing the selection of the data.

If your assignment is to use files, what have you tried, since we are not here to write code for you?

1 Like

Hello yes a database is the best option but considering we are just talking about one text file that is updated, that seemed ok. I was trying read the to content of the first file into a variable. Then append the suffix of the file I need to open, then use the variable as the filename argument to open another file and read it all being single files and 10 at the most with two lines of messaging…but a little stuck that’s all. Ill get it eventually.

Stuck with what? Without code we obviously can’t help you.

This really seems like you are trying to fix what someone thought was a creative solution to some problem, without actually explaining the problem itself.

So, I am hearing that specific content needs to be displayed based on a value. Currently, that value comes from a file.

Why? It really sounds like you are doing content management via file uploads in text documents? Can you give a summary of what this problem is attempting to solve, rather than how you went about solving it?

This was the answer I managed to arrive at. I failed to see that it was just a matter of changing
“sundata_alertmf” => file_get_contents(’…/sundata_alertmf.txt’),
"sundata_alert" => file_get_contents('../alert/sundata_alert.txt'),

which was reading the txt file that points to an image rather than plain text. so kept the latter as a fallback.

Sponsor our Newsletter | Privacy Policy | Terms of Service