Two methods for uploading files in PHP

Source: Internet
Author: User
Tags ftp connection

PHP (PHP training PHP Tutorial) (hypertext Preprocessor) is an HTML-embedded language (similar to ASP on IIS). PHP's unique syntax mixes C, Java, Perl, and PHP-style new syntax. It can be more than CGI or PERL&NB

SP; faster execution of dynamic Web pages. In addition, the Web back-end CGI program written in PHP can be easily ported to different system platforms.

When we make the website, we need the visitor's participation in order to make the website more compelling, which requires us to get articles, pictures, etc. from the visitor. As a result, file uploads are an essential feature of Web pages. Now I take advantage of the popular programming language PHP, two ways to illustrate the implementation of its functions.

First, the use of PHP file functions to implement the upload

This code is divided into two files, one for upload.html and one for upload.php.

Upload file selection: The upload.html code is as follows:

--------------------------------------------------------------------------------

  

  

  

  

  

--------------------------------------------------------------------------------

Description * * *

1. Please note

--------------------------------------------------------------------------------

To process the upload file: The ftp.php code is as follows:

--------------------------------------------------------------------------------

  

FTP Join host functions

Function Connect ()

{

Global $server, $username, $password;

$conn = Ftp_connect ($server);

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

return $conn;

}

Creating an FTP connection

$result = connect ();

if ($action = = "Upload")

{

Used to change the FTP path

Ftp_chdir ($result, $cdir);

Used to upload the specified file with the same name and passed as Bits

$res _code = Ftp_put ($result, $upfile _name, $upfile, ftp_binary);

Determine if the upload is correct

if ($res _code = = 1)

echo "Upload succeeded!";

Else

echo "Upload error!";

}

Close Join

Ftp_quit ($result);

?>

--------------------------------------------------------------------------------

Description * * *

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

Return Value: Boolean value

This function is used to upload the specified file. The Ftp_stream parameter is the FTP connection code. The parameter remote_file is the file name to which the remote is to exist. The parameter local_file is the file name of the file to be uploaded. The value of the parameter mode is FTP_ASCII and Ftp_binary two, which represents the document or binary file respectively. Success returns a true value, and a value of false if it fails.

Two methods for uploading files in PHP

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.