PHP and MySQL Web Development - P80 pps

5 176 0
PHP and MySQL Web Development - P80 pps

Đang tải... (xem toàn văn)

Thông tin tài liệu

367 Using FTP ftp_quit($conn); exit; } echo "Logged in as $user<br />"; // check file times to see if an update is required echo 'Checking file time <br />'; if (file_exists($localfile)) { $localtime = filemtime($localfile); echo 'Local file last updated '; echo date('G:i j-M-Y', $localtime); echo '<br />'; } else $localtime=0; $remotetime = ftp_mdtm($conn, $remotefile); if (!($remotetime >= 0)) { // This doesn't mean the file's not there, server may not support mod time echo 'Can\'t access remote file time.<br />'; $remotetime=$localtime+1; // make sure of an update } else { echo 'Remote file last updated '; echo date('G:i j-M-Y', $remotetime); echo '<br />'; } if (!($remotetime > $localtime)) { echo 'Local copy is up to date.<br />'; exit; } // download file echo 'Getting file from server <br />'; $fp = fopen ($localfile, 'w'); if (!$success = ftp_fget($conn, $fp, $remotefile, FTP_BINARY)) { echo 'Error: Could not download file'; ftp_quit($conn); exit; } Listing 17.4 Continued 22 525x ch17 1/24/03 3:40 PM Page 367 368 Chapter 17 Using Network and Protocol Functions fclose($fp); echo 'File downloaded successfully'; // close connection to host ftp_quit($conn); ?> </body> </html> The output from running this script on one occasion is shown in Figure 17.4. Listing 17.4 Continued Figure 17.4 The FTP mirroring script checks whether the local version of a file is up-to-date, and downloads a new version if not. This is quite a generic script.You’ll see that it begins by setting up some variables: $host = 'ftp.cs.rmit.edu.au'; $user = 'anonymous'; $password = 'laura@tangledweb.com.au'; $remotefile = '/pub/tsg/teraterm/ttssh14.zip'; $localfile = "/tmp/writable/ttssh14.zip"; The $host variable should contain the name of the FTP server you want to connect to, and the $user and $password correspond to the username and password you would like to log in with. Many FTP sites support what is called anonymous login, that is, a freely available user- name that anybody can use to connect. No password is required, but it is a common courtesy to supply your email address as a password so that the system’s administrators can see where their users are coming from.We have followed this convention here. The $remotefile variable contains the path to the file we would like to download. In this case we are downloading and mirroring a local copy of Tera Term SSH, an SSH client for Windows. (SSH stands for secure shell.This is an encrypted form of telnet.) 22 525x ch17 1/24/03 3:40 PM Page 368 369 Using FTP The $localfile variable contains the path to the location where we are going to store the downloaded file on our machine. In this case we have created a directory called /tmp/writable with permissions set up so that PHP can write a file there. You should be able to change these variables to adapt this script for your purposes. The basic steps we follow in this script are the same as if you wanted to manually FTP the file from a command line interface: 1. Connect to the remote FTP server. 2. Log in (either as a user or anonymous). 3. Check whether the remote file has been updated. 4. If it has, download it. 5. Close the FTP connection. Let’s take each of these in turn. Connecting to the Remote FTP Server This step is equivalent to typing ftp hostname at a command prompt on either a Windows or UNIX platform.We accomplish this step in PHP with the following code: $conn = ftp_connect("$host"); if (!$conn) { echo 'Error: Could not connect to ftp server<br />'; exit; } echo "Connected to $host.<br />"; The function call here is to ftp_connect().This function takes a hostname as parame- ter, and returns either a handle to a connection, or false if a connection could not be established.The function can also take the port number on the host to connect to as an optional second parameter. (We have not used this here.) If you don’t specify a port number, it will default to port 21, the default for FTP. Logging In to the FTP Server The next step is to log in as a particular user with a particular password.You can achieve this using the ftp_login() function: @ $result = ftp_login($conn, $user, $pass); if (!$result) { echo "Error: Could not log on as $user<br />"; ftp_quit($conn); 22 525x ch17 1/24/03 3:40 PM Page 369 370 Chapter 17 Using Network and Protocol Functions exit; } echo "Logged in as $user<br />"; The function takes three parameters: an FTP connection (obtained from ftp_ connect()), a username, and a password. It will return true if the user can be logged in, and false if he can’t.You will notice that we put an @ symbol at the start of the line to suppress errors.We do this because, if the user cannot be logged in, you will get a PHP warning in your browser window.You can catch the error as we have done here by test- ing $result, and supplying your own, more user-friendly error message. Notice that if the login attempt fails, we actually close the FTP connection using ftp_quit()—more on this in a minute. Checking File Update Times Given that we are updating a local copy of a file, it is sensible to check whether the file needs updating first because you don’t want to have to re-download a file, particularly a large one, if it’s up-to-date.This will avoid unnecessary network traffic. Let’s look at the code that does this. First, we check that we have a local copy of the file, using the file_exists() func- tion. If we don’t then obviously we need to download the file. If it does exist, we get the last modified time of the file using the filemtime() function, and store it in the $localtime variable. If it doesn’t exist, we set the $localtime variable to 0 so that it will be “older” than any possible remote file modification time: echo 'Checking file time <br />'; if (file_exists($localfile)) { $localtime = filemtime($localfile); echo 'Local file last updated '; echo date('G:i j-M-Y', $localtime); echo '<br />'; } else $localtime=0; (You can read more about the file_exists() and filemtime() functions in Chapter 2 and Chapter 16,“Interacting with the File System and the Server,” respectively.) After we have sorted out the local time, we need to get the modification time of the remote file.You can get this using the ftp_mdtm() function: $remotetime = ftp_mdtm($conn, $remotefile); This function takes two parameters—the FTP connection handle, and the path to the remote file—and returns either the UNIX time stamp of the time the file was last modi- fied, or -1 if there is an error of some kind. Not all FTP servers support this feature, so 22 525x ch17 1/24/03 3:40 PM Page 370 371 Using FTP we might not get a useful result from the function. In this case, we choose to artificially set the $remotetime variable to be “newer” than the $localtime variable by adding 1 to it.This will ensure that an attempt is made to download the file: if (!($remotetime >= 0)) { // This doesn't mean the file's not there, server may not support mod time echo 'Can't access remote file time.<br />'; $remotetime=$localtime+1; // make sure of an update } else { echo 'Remote file last updated '; echo date('G:i j-M-Y', $remotetime); echo '<br />'; } When we have both times, we can compare them to see whether we need to download the file or not: if (!($remotetime > $localtime)) { echo 'Local copy is up to date.<br />'; exit; } Downloading the File At this stage we will try to download the file from the server: echo 'Getting file from server <br />'; $fp = fopen ($localfile, 'w'); if (!$success = ftp_fget($conn, $fp, $remotefile, FTP_BINARY)) { echo 'Error: Could not download file'; fclose($fp); ftp_quit($conn); exit; } fclose($fp); echo 'File downloaded successfully'; We open a local file using fopen() as we have seen previously. After we have done this, we call the function ftp_fget(), which attempts to download the file and store in a local file.This function takes four parameters.The first three are straightforward—the FTP connection, the local file handle, and the path to the remote file.The fourth param- eter is the FTP mode. 22 525x ch17 1/24/03 3:40 PM Page 371 . two parameters—the FTP connection handle, and the path to the remote file and returns either the UNIX time stamp of the time the file was last modi- fied, or -1 if there is an error of some kind date('G:i j-M-Y', $localtime); echo '<br />'; } else $localtime=0; (You can read more about the file_exists() and filemtime() functions in Chapter 2 and Chapter 16,“Interacting. to, and the $user and $password correspond to the username and password you would like to log in with. Many FTP sites support what is called anonymous login, that is, a freely available user- name

Ngày đăng: 07/07/2014, 03:20

Từ khóa liên quan

Mục lục

  • PHP and MySQL Web Development

  • Copyright

  • Table of Contents

  • Introduction

  • Part I: Using PHP

    • Chapter 1: PHP Crash Course

    • Chapter 2: Storing and Retrieving Data

    • Chapter 3: Using Arrays

    • Chapter 4: String Manipulation and Regular Expressions

    • Chapter 5: Reusing Code and Writing Functions

    • Chapter 6: Object-Oriented PHP

    • Part II: Using MySQL

      • Chapter 7: Designing Your Web Database

      • Chapter 8: Creating Your Web Database

      • Chapter 9: Working with Your MySQL Database

      • Chapter 10: Accessing Your MySQL Database from the Web with PHP

      • Chapter 11: Advanced MySQL

      • Part III: E-commerce and Security

        • Chapter 12: Running an E-commerce Site

        • Chapter 13: E-commerce Security Issues

        • Chapter 14: Implementing Authentication with PHP and MySQL

        • Chapter 15: Implementing Secure Transactions with PHP and MySQL

        • Part IV: Advanced PHP Techniques

          • Chapter 16: Interacting with the File System and the Server

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan