In the previous chunk, we downloaded a file by using FTP. In this chunk, we're going to upload a file by using ftp_put: ftp_put(resource ftp_stream, string remote_file, string local_file, int mode [, int startpos]) This function stores local_file (which can be specified with a pathname) on the FTP server as remote_file. The transfer mode specified must be either FTP_ASCII or FTP_BINARY. It returns trUE on success or FALSE on failure. As an example, we'll upload a file named newscript.php to an FTP site as script.php. We start by connecting: $connect = ftp_connect("ftp.ispname.com"); $result = ftp_login($connect, "username", "password"); if(!$result){ echo "Could not connect."; exit; } Then we use ftp_put to upload the file, and we check the result: $connect = ftp_connect("ftp.ispname.com"); $result = ftp_login($connect, "username", "password"); if(!$result){ echo "Could not connect."; exit; } $result = ftp_put($connect, "newscript.php", "script.php", FTP_ASCII); if($result){ echo "Sent the file."; } else { echo "Did not send the file."; } You can see this all at work in Example 9-6, phpftpput.php. Example 9-6. Uploading a file, phpftpput.php<HTML> <HEAD> <TITLE> Putting a file with FTP </TITLE> </HEAD> <BODY> <CENTER> <H1>Putting a file with FTP</H1> Uploading the file.... <BR> <?php $connect = ftp_connect("ftp.ispname.com"); $result = ftp_login($connect, "username", "password"); if(!$result){ echo "Could not connect."; exit; } $result = ftp_put($connect, "newscript.php", "script.php", FTP_ASCII); if($result){ echo "Sent the file."; } else { echo "Did not send the file."; } ftp_close($connect); ?> </CENTER> <BODY> </HTML> The results appear in Figure 9-7, where, as you can see, we've successfully uploaded the file. Figure 9-7. Uploading a file. |