Php Tutorial 6 (connecting ftp server and IMAP server with php)

Source: Internet
Author: User
Tags file upload ftp connection ftp file imap nntp nntp server imap port port number

Use the php Tutorial to connect to the server, and connect to the imap server. Finally, use the code written in php to upload and download files.

The code is as follows: Copy code

<? Php
// Connect to the imap server, and the imap port is 143.
$ Mbox = imap_open ("{localhost: 143} inbox", "user_id", "password ");
// Connect to the pop3 server. The pop3 port is 110.
$ Mbox = imap_open ("{localhost/pop3: 110} inbox", "user_id", "password ");
// Connect to the nntp server. The port number of nntp is 119.
$ Nntp = imap_open ("{localhost/nntp: 119} comp. test ","","");
?>


Email sending function mail

The code is as follows: Copy code
<? Php
Mail ("163@111cn.net", "welcome", "hello, hello! ");
?>
The code is as follows: Copy code
<? Php
// Connect to the imap server
$ Mbox = imap_open ("{imap.example.org}", "username", "password", op_halfopen)
Or die ("connection failed:". imap_last_error ());
$ List = imap_getmailboxes ($ mbox, "{imap.example.org }","*");
If (is_array ($ list )){
Foreach ($ list as $ key => $ val ){
Echo "($ key )";
Echo imap_utf7_decode ($ val-> name ).",";
Echo "'". $ val-> delimiter ."',";
Echo $ val-> attributes. "<br/> n ";
    }
} Else {
Echo "imap_getmailboxes failed:". imap_last_error (). "n ";
}
// Close the imap connection
Imap_close ($ mbox );
?>

Connect to the ftp server

The code is as follows: Copy code

<? Php
// Open the file to be uploaded
$ File = 'demofile.txt ';
$ Fp = fopen ($ file, 'r ');

// Connect to the ftp server
$ Conn_id = ftp_connect ($ ftp_server );
// Log on to the ftp server
$ Login_result = ftp_login ($ conn_id, $ ftp_user_name, $ ftp_user_pass );

// Upload a file
If (ftp_fput ($ conn_id, $ file, $ fp, ftp_ascii )){
Echo "$ file uploaded successfully n ";
} Else {
Echo "failed to upload $ file n ";
}

// Close the ftp connection
Ftp_close ($ conn_id );
// Close the opened Upload file
Fclose ($ fp );
?>

Ftp file upload/download

The code is as follows: Copy code
<? Php
$ File = 'somefile.txt ';
$ Remote_file = 'readme.txt ';
// Connect to the ftp server
$ Conn_id = ftp_connect ($ ftp_server );
// Log on with the user name and password
$ Login_result = ftp_login ($ conn_id, $ ftp_user_name, $ ftp_user_pass );
// Upload a file
If (ftp_put ($ conn_id, $ remote_file, $ file, ftp_ascii )){
Echo "successfully uploaded $ file n ";
} Else {
Echo "failed to upload $ file n ";
}
// Close the ftp connection
Ftp_close ($ conn_id );
?>

Delete files through ftp

The code is as follows: Copy code
<? Php
$ File = 'public _ html/old.txt ';
// Connect to the ftp server
$ Conn_id = ftp_connect ($ ftp_server );
// Verify the user name and password
$ Login_result = ftp_login ($ conn_id, $ ftp_user_name, $ ftp_user_pass );
// Delete a specified object
If (ftp_delete ($ conn_id, $ file )){
Echo "$ file deleted successfully n ";
} Else {
Echo "failed to delete $ file n ";
}
// Close the ftp connection
Ftp_close ($ conn_id );
?>

Obtain the remote file size through ftp

The code is as follows: Copy code
<? Php
$ File = 'somefile.txt ';
// Connect to the ftp server
$ Conn_id = ftp_connect ($ ftp_server );
// Verify the user name and password
$ Login_result = ftp_login ($ conn_id, $ ftp_user_name, $ ftp_user_pass );
// Obtain the size of the specified object
$ Res = ftp_size ($ conn_id, $ file );
If ($ res! =-1 ){
Echo "$ file size: $ res bytes ";
} Else {
Echo "failed to get the remote file size ";
}
// Close the ftp connection
Ftp_close ($ conn_id );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.