Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

While it is an error to open a file for writing when an HTTP URL is specified, this is not
the case with FTP. You may upload an FTP file by using write mode. However, this
functionality is limited. You can create remote files, but you may not overwrite existing
files. With either HTTP or FTP connections, you may only read from start to finish from
a file. You may not use fseek or similar functions.


Sometimes files on HTTP and FTP servers are protected by usernames and passwords.
You can specify a username and a password exactly as popular Web browsers allow you
to do. After the network protocol and before theserver name you may insert a username, a
colon, a password, and an at-symbol (@).


Three other ways to open a file are the fsockopen, gzopen, popen functions.


<?
print("

HTTP

\n");


//open a file using http protocol
//Use username and password
if(!($myFile = fopen("http://leon:[email protected]/", "r")))
{
print("file could not be opened");
exit;
}


while(!feof($myFile))
{
// read a line from the file
$myLine = fgetss($myFile, 255);
print("$myLine
\n");
}


// close the file
fclose($myFile);


print("

FTP

\n");
print("
\n");


// open a file using ftp protocol
if(!($myFile = fopen("ftp://ftp.php.net/welcome.msg", "r")))
{
print("file could not be opened");
exit;
}


while(!feof($myFile))
{
// read a line from the file
$myLine = fgetss($myFile, 255);
print("$myLine
\n");
}


// close the file
fclose($myFile);

Free download pdf