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 written in PHP Program And can be easily transplanted to different system platforms.

When we are doing a website, we need the visitor's participation to make the website more eye-catching, which requires us to get from the visitorArticleAnd images. Therefore, file upload is an essential feature in Web pages. Now I will use the popularProgramming LanguagePHP: describes the implementation of its functions in two ways.

I. Use PHP file functions for upload
This sectionCodeDivided into two files, one is upload.html and the other is upload. php.

Select Upload File: upload.html code:

<HTML>

<Body>

<Form enctype = "multipart/form-Data" Action = "Upload. php" method = "Post">

<Input type = "hidden" name = "max_file_size" value = "100000">

<Center> select a file:

<Input name = "userfile" type = "file">

<Input type = "Submit" value = "Upload File">

</Center>

</Form>

</Body>

</Html>

* ** Description ***

1. Please note that <form enctype = "multipart/form-Data"……> This is a tag. to upload a file, you must specify it as multipart/form-Data. Otherwise, the server will not know what you are doing!

The value of 2XX is the hidden value range of the form option max_file_size in the file upload.html.

Its value can limit the size of the uploaded file.

Process the uploaded file: the upload. PHP code is as follows:

<HTML>

<Head>

<Title> process uploaded files </title>

</Head>

<Body>

<?

Copy ($ userfile, "newfile ");

Echo $ userfile. "-name of the temporary file uploaded to the server <br> ";

Echo $ userfile_name. "-original name of the file on the user's machine <br> ";

Echo $ userfile_size. "-actual number of bytes of the uploaded file <br> ";

Echo $ userfile_type. "-if the user's browser provides this information, it indicates the MIME type. For example, image/GIF <br> ";

?>

</Body>

</Html>

* ** Description ***

1. Use the PHP file function copy () to copy the files uploaded to the temporary directory to a specific directory and rename it "newfile ".

2 bytes defines a variable userfile in upload.html, in upload. in PHP, we can use this variable to directly access the uploaded file through $ userfile. However, because this is a variable for saving the file, the file name must be obtained through another $ userfile_name variable.

The specific usage of this variable is as follows:

$ Userfile: name of the temporary file on the server where the uploaded file is to be stored.

$ Userfile_name: initial file name in the sender system.

$ Userfile_size: the size of the uploaded file in bytes.

$ Userfile_type: multi-purpose Internet Mail Extension protocol files, provided that the browser provides such information, such as "image/GIF ".

Ii. use ftp to upload files
This code is also divided into two files: Upload. php and FTP. php.

Set FTP-related options and select the Upload File Name: the upload. PHP code is as follows:

<? PHP

$ Username = "username ";

$ Password = "User Password ";

$ Server = "Host Name ";

$ Cdir = "Upload directory name ";

// Set your FTP host name, user name, and user password.

?>

<! -- Set the File Upload tag -->

<Form enctype = "multipart/form-Data" Action = ftp. php method = post>

<! -- Pass variables -->

<Input type = hidden name = username value = <? Echo $ username;?>

<Input type = hidden name = password value = <? Echo $ password;?>

<Input type = hidden name = server value = <? Echo $ server;?>

<Input type = hidden name = cdir value = <? Echo $ cdir;?>

<Table>

<Tr>

<TD> select an upload file

<Input type = file name = upfile>

</TD>

</Tr>

<Tr>

<TD>

<! -- Submit a form -->

<Input type = submit name = action value = upload>

</TD>

</Tr>

</Table>

</Form>

Process uploaded files: the ftp. PHP code is as follows:

<? PHP

// 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.