Using PHP to upload files

Source: Internet
Author: User
Tags ftp connection
PHP (Hypertext Preprocessor) is an embedded HTML language (similar to ASP on IIS ). The unique PHP syntax is a mixture of new C, Java, Perl, and PHP syntaxes. It can execute dynamic web pages more quickly than CGI or Perl. In addition, the Web backend CGI program written in PHP can be easily transplanted to different system platforms.

When creating a website, we need the visitor's participation to make the website more eye-catching. This requires us to get articles and images from the visitor. Therefore, file upload is an essential feature in web pages. Now I will use the popular programming language PHP to describe its function implementation in two ways.

I. use php file functions for Upload
This code is divided into two files, one is upload.html and the other is upload. php.

Select Upload File: upload.html code:

Upload file Form










* ** Description ***

1. Please note


Process uploaded files: The ftp. php code is as follows:


// Ftp host connection function

Function connect ()

{

Global $ server, $ username, $ password;

$ Conn = ftp_connect ($ server );

Ftp_login ($ conn, $ username, $ password );

Return $ conn;

}

// Create an ftp connection

$ Result = connect ();

If ($ action = "upload ")

{

// Used to change the ftp path

Ftp_chdir ($ result, $ cdir );

// Used to upload the specified object, which has the same name and is passed in binary.

$ Res_code = ftp_put ($ result, $ upfile_name, $ upfile, FTP_BINARY );

// Determine whether the upload is correct

If ($ res_code = 1)

Echo "Upload successful! ";

Else

Echo "upload error! ";

}

// Close the connection

Ftp_quit ($ result );

?>


* ** Description ***

Function ftp_put (int ftp_stream, string remote_file, string local_file, int mode) usage

Return value: Boolean

This function is used to upload a specified object. The ftp_stream parameter is the FTP connection code. The remote_file parameter is the name of the target remote object. The local_file parameter is the name of the file to be uploaded. The mode parameter has two types of values: FTP_ASCII and FTP_BINARY, indicating the document or binary file respectively. If the call succeeds, the return value is true. if the call fails, the return value is false.

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.