Simple Solution for Separating Image servers from web application servers

Source: Internet
Author: User
Tags ftp connection remote ftp server domain name server ftp client

 

 

I just want to briefly describe the principle. Let's explore it by yourself :)

I. Two domain names: www.domain.com and img.domain.com
2. upload files to the WWW Domain Name Server:

Up.html
<Form name = "myform" Action = "Save. php? Action = OK "method =" Post "enctype =" multipart/form-Data ">
<Input type = "file" name = "itname" value = ">
<Input type = "Submit" name = "TT" value = "upit">
</Form>

Save. php

If (@ $ _ Get ['action'] = "OK ")
{
$ Picname = "upfile/". Rand (). ". jpg ";
Move_uploaded_file ($ _ FILES ['itname'] ['tmp _ name'], $ picname );
}

$ Ftp_server = "img.domain.com"; // The domain name of the server to be connected.
$ Con = ftp_connect ($ ftp_server); // connect to the FTP server
Ftp_login ($ con, "username", "password"); // send the user name and password
$ Tempstate = ftp_put ($ con, "drckness.jpg", $ picname, ftp_binary) // upload files in binary mode
If ($ tempstate ){
Echo "uploaded successfully ";
Unlik ($ picname); // Delete the image on the WWW server. Of course, you can retain it.
} Else {
Echo "Upload Failed ";

};
Ftp_quit ($ conn); // close the connection. Otherwise, the resource usage will remain active.

The preceding section describes how to separate images from the web. In actual development, operations such as upload detection, watermarks, thumbnail generation, redundant judgment, delayed operations, and image path recording are also performed.

But as long as you understand the principle, other things should be explained :)

========================================================== ==========

The following is my summary of common FTP functions in PHP :)

 

Use of FTP-related functions in PHP

 

// Connect to the FTP server
$ Conn = ftp_connect ("ftp.server.com ");

To complete an FTP connection, PHP provides the ftp_connect () function, which uses the host name and port as the parameter. In the preceding example, the host name is "ftp.server.com". If the port is not specified, PHP uses "21" as the default port to establish a connection.
After the connection is successful, ftp_connect () returns a handle. This handle will be used by later FTP functions.

========================================================== ========================================================== ======================================

// Use username and password to log on
Ftp_login ($ Conn, "John", "doe ");

Once a connection is established, use ftp_login () to send a user name and password. As you can see, this function ftp_login () uses handle from the ftp_connect () function to confirm that the user name and password can be submitted to the correct server.

========================================================== ========================================================== ======================================

 

// Close the connection
Ftp_quit ($ conn );

Remember to use ftp_quit to close the connection :)

========================================================== ====================

$ Here = ftp_pwd ($ conn); // obtain the current directory location

$ Server_ OS = ftp_0000ype ($ conn); // operating system type

Ftp_pasv ($ Conn, 1); // enable the passive transmission mode of FTP

(Note:

During FTP transmission, a dedicated connection is required to transmit data. There are two methods to establish a connection:

One is that the FTP Server opens a port, and the client can connect to the port to establish a connection. This is the PASV method.

One is that the FTP client opens a port, and the server connects to the port to establish a connection. This is the port mode.

Obviously, if the server opens a port, it means a port is opened to the Internet. Therefore, the port mode is safer than the PASV mode, but there are also clients because

The connection may fail due to a firewall problem. So I think the default value is :)

)

Ftp_chdir ($ Conn, "htmldir"); // enter the htmldir directory

Ftp_cdup ($ conn); // return to the upper-level directory

Ftp_mkdir ($ Conn, "mktest"); // create a directory named mktest. If the directory is successful, the directory name is returned. If the directory is unsuccessful, false is returned.

Ftp_rmdir ($ Conn, "rmtest"); // delete a directory. Success: true; Failure: false

Ftp_put ($ Conn, "xyz.txt", "abc.txt", ftp_ascii); // upload the file to the server, parameters (connection, remote file name, local file name, ftp_ascee for text transmission/ftp_binary for Binary upload)

Ftp_get ($ Conn, "hers.zip", "his.zip", ftp_binary); // download the file to the local server

$ Filelist = ftp_nlist ($ Conn, "."); // list the file names and directories.

$ Filelist = ftp_rawlist ($ Conn, "."); // list the file name size, permission, date, and other functions in detail. Both functions return an array.

$ Filelist = ftp_size ($ Conn, "data.zip"); // get the file size

 

========== More function reference manual ================================ ======================================

Ftp_alloc -- allocates space for a file to be uploaded
Ftp_cdup -- switch to the parent directory of the current directory
Ftp_chdir -- switch the current directory on the FTP server
Ftp_chmod -- Set permissions on a file via FTP
Ftp_close -- close an active FTP connection
Ftp_connect -- create a new FTP connection
Ftp_delete -- delete a file on the FTP server
Ftp_exec -- Request to run an FTP command
Ftp_fget -- download a file from the FTP server and save it to a local open file.
Ftp_fput -- upload an opened file to the FTP server
Ftp_get_option -- return various options for the current FTP connection
Ftp_get -- download an object from the FTP server
Ftp_login -- log on to the FTP server
Ftp_mdtm -- return the last modification time of the specified object
Ftp_mkdir -- create a new directory
Ftp_nb_continue -- continuous file acquisition/sending (non-blocking)
Ftp_nb_fget -- retrieves a file from the FTP server and writes it to an open file (non-blocking)
Ftp_nb_fput -- stores a file from an open file to the FTP server (non-blocking)
Ftp_nb_get -- get the file from the FTP server and write it to the local file (non-blocking)
Ftp_nb_put -- store a file to the FTP server (non-blocking)
Ftp_nlist -- returns the list of objects in the specified directory
Ftp_pasv -- return whether the current FTP passive mode is enabled
Ftp_put -- upload files to the FTP server
Ftp_pwd -- return the name of the current directory
Ftp_quit -- alias of ftp_close ()
Ftp_raw -- sends an arbitrary command to an FTP server
Ftp_rawlist -- return the detailed list of objects in the specified directory
Ftp_rename -- ü FTP · ¨"
Ftp_rmdir -- delete a directory
Ftp_set_option -- è ÷ FTP ±
Ftp_site -- send the SITE command to the server
Ftp_size -- returns the size of the specified file.
Ftp_ssl_connect -- open s an secure SSL-FTP connection
Ftp_0000ype -- return the system type of the remote FTP Server

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.