LoadFile

Internet
Aptilis 1

LoadFile(Url[, UserName[, Password[, Mode]]])

This is the special page for LoadFile, when you use URLs, instead of file names to load files. If you only load files from your local hard disk, see LoadFile

LoadFile understands the following URL types:
- file:// (normal files, check the File names topic)
- http:// to load a web page
- https:// to load a web page over SSL encryption
- ftp:// to get a file from an FTP server

Of course, to use the http:// and ftp:// types to access remote files, your computer needs to be connected permanently to the Internet or an intranet. If you are running a web server or an FTP server on your machine and want to retrieve your own files using http:// or ftp:// then of course, you do not need a connexion to a network.

If you want to use the POST method when loading an HTTP URL instead of GET (Which is limited to a couple hundred characters), use loadPostFile instead.

Specifying a Url
Here is an http:// Url: "http://www.cnn.com/"
Here is an ftp Url: "ftp://ftp.freeware-madness.org/pub/games/fry-em.zip"

You can use IP numbers and port numbers as well to override the default ports for http:// (80) and ftp:// (21), such as:
"http://www.xnn.org:8080/news/secret.html"
or:
"ftp://123.45.67.101:1040/pub/games/fry-em.zip"

In the case of web pages, the document returned contains the page itself and its header. The end of the header is after the occurrence of the header delimiter: "\n\n", or possibly "\r\n\r\n". I usually remove the "\r" 's anyway:

file = replace(file$, "\r", "") $
One of the applications of this is to be able to see the source of pages that might not be kept by your browser, and allow you to see how they do it. You can also 'nick' the content of different pages and re-arrange them in a page of your own. But that's quite naughty. :-)

When do I need to specify a username, password, and mode?
Those are needed when you access an FTP server. Indeed, FTP servers will not grant you access, unless you identify yourself. Fortunately, a lot of public servers allow you to login using the login 'anonymous' and your e-mail address as a password.
Here are the default values assigned to these parameters if you don't specify them:
UserName: "anonymous"
Password: "" (No password)
Mode: "binary"

The mode indicates how you want the file to be transfered. "Binary" indicates that you want the file to be transferred absolutely unchanged. "Ascii" asks the FTP server to try and be smart about how to store/send text files. As far as I am concerned I always use binary, and then use Replace to get the proper line separators.
That's "\n" under Unix, and "\r\n" under Windows. I start by removing all the "\r" and then replace all the "\n" by "\r\n" if I am aiming at a PC target.

LoadFile is very useful when used in conjonction with stuff to fill templates with data.

Return Value:
The whole file if everything was OK.
An empty string in case of error and the _errno variable contains more details about the error.

Example:

a = loadfile("http://www.mw-interactive.co.uk/") $
print(a$)
Result: (Note the header)
HTTP/1.1 200 OK
Date: Tue, 18 Aug 1998 12:47:44 GMT
Server: Apache/1.2.4
Last-Modified: Tue, 17 Mar 1998 15:55:52 GMT
ETag: "c2a0e-225-350e9d08"
Content-Length: 549
Accept-Ranges: bytes
Connection: close
Content-Type: text/html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Mediaworks Interactive</title>
</head>

<body bgcolor="#FFFFFF">

<img src="mwi.gif" />

<hr />

Welcome on our server.<br />
Etc...

See also: loadPostFile, saveFile, HTTPLoad, HTTPPostLoad.