Oftentimes data gets stored on the server for future use, a cornerstone of Server-Side Scripting. You will need to be able to access these files in many occasions to determine how to present information to your users. The instructions here help you learn how to do so.
```perl
open(FILEHANDLE,"https://coding.ndcfinearts.ca/filename.txt") or die("Could not find file https://coding.ndcfinearts.ca/filename.txt on Line 123 in my code: $!");
```
The `open` function allows you to access a file. That file is then stored in what is known as a "file handle". That file handle can be any alphabetical name (in the above example we used `FILEHANDLE`, but it could just as easily be `CHOPSUEY`). You must then provide the filename to look for.
But what if that file doesn't exist? You don't want to mess with the rest of your code and possibly cause a server failure, so you kill the script from running anything else and potentially causing an error. To do so, we use the `die` command, including a personalized message we have, as well as the computer error message (represented by `$!`) thrown, so you have enough information to figure out what went wrong.
But opening a file isn't enough. There are many other steps you need to do as well.
```perl
open(FILEHANDLE,"https://coding.ndcfinearts.ca/filename.txt") or die("Could not find file https://coding.ndcfinearts.ca/filename.txt on Line 123 in my code: $!");
flock(FILEHANDLE,LOCK_SH);
seek(FILEHANDLE,0,0);
```
The `flock` command instructs the server whether or not the file should be made accessible by other uses of your script while you are using it. For example, User A calls on your script which asks for the file. If User B calls on the script at the exact same time, do you want them to be able to see the file? Passing a `LOCK_SH` will allow others "shared access" to see and read the file. `LOCK_EX` will give you "exclusive access", meaning nobody else can access the file while your script is using it, even if it is someone else using the exact same script.
The `seek` command places a "cursor" at a specific point in the file, ready for reading or writing. More often than not, you will only change the last parameter. A command of `seek(FILEHANDLE,0,0)` will set the cursor at the very beginning of the file, whereas `seek(FILEHANDLE,0,2)` will set the cursor at the end of the file (usually only used when you want to add data to the end of the file). In reading a file, you will only use `seek(FILEHANDLE,0,0)`.
There are still two more instructions you will need. While you have a file loaded into your script, it's not in a format you can use yet. You need to put it in a list variable so that you can work with it, and then you will need to close the file so that someone else can have access to it.
```perl
open(FILEHANDLE,"https://coding.ndcfinearts.ca/filename.txt") or die("Could not find file https://coding.ndcfinearts.ca/filename.txt on Line 123 in my code: $!");
flock(FILEHANDLE,LOCK_SH);
seek(FILEHANDLE,0,0);
@myData = <FILEHANDLE>; #places all the contents of the file in @myData
close(FILEHANDLE); #now someone else can use it.
```
# Using the File's Data
To access the file's data, you can use a for loop to get through every line.
>NOTE: Every line from a file's data will also have a way of saying the end of the line has been reached. This is usually represented with a "newline character", which perl recognizes as `\n`. To get rid of that newline character, use the `chomp` function.
```perl
for (@myData) {
chomp($_);
print "$_<br>\n";
}
```
This is a simple loop that will print a file's contents to your user's browser for you.
# File Structure on the Server
You should keep your data files in a hidden place on your server, easily accessible by you, but hidden from hackers and data miners. The best way to do so is to have a `data` or `myData` folder (or any other name that you will remember) in your `cgi-bin` folder.
>CHALLENGE: Make a data file with some random information in it, such as a list of colours. Save it in a data folder in your cgi-bin, then use the open process to list that file's information on the user's screen.