Home  >  Article  >  Backend Development  >  Two methods of file upload using PHP_PHP tutorial

Two methods of file upload using PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:10:23806browse


PHP (Hypertext Preprocessor) is an HTML embedded language (similar to ASP on IIS). And PHP's unique syntax mixes C, Java, Perl, and new PHP-like syntax. It can execute dynamic web pages faster than CGI or Perl. In addition, web back-end CGI programs written in PHP can be easily ported to different system platforms.

When we build a website, we need the participation of visitors to make the website more eye-catching, which requires us to get articles, pictures, etc. from visitors. Therefore, file upload has become an essential function in web pages. Now I will use the popular programming language PHP to illustrate the implementation of its functions in two ways.

1. Use PHP’s file function to upload.
This code is divided into two files, one is upload.html and the other is upload.php.

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

Upload file form






Please select the file:














*** Description***

1. Please note that
This is a tag. If we want to upload files, we must specify for multipart/form-data, otherwise the server won't know what you are doing!

2. It is worth noting that the hidden value field of the form option MAX_FILE_SIZE in the file upload.html can limit the size of the uploaded file by setting

its Value.

Process the file just uploaded: upload.php code is as follows:





Process the uploaded file






copy($userfile,"newfile");

echo $userfile."-The name of the temporary storage of the file uploaded by the user to the server
";

echo $userfile_name."-The original name of the file on the user's machine
";

echo $userfile_size." - The actual number of bytes in the uploaded file
";

echo $userfile_type." - If the user's browser provides this information, It represents the mime type. For example, image/gif
";

?>






*** Description ***

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

2. A variable userfile is defined in upload.html. In upload.php, we can use this variable to directly access the uploaded file through $userfile, but since this is a variable that saves files , so the file name must be obtained through another $userfile_name variable.

The following is the specific usage of this variable:

$userfile: The name of the temporary file on the server where the uploaded file will be stored.

$userfile_name: The initial file name in the sender's system.

$userfile_size: The size of the uploaded file in bytes.

$userfile_type: Multipurpose Internet Mail Extensions protocol type file, provided that the browser provides this information, such as "image/gif".



2. Use the FTP function to upload files
This code is also divided into two files, one is upload.php and the other is ftp.php.

Set the ftp related options and select the upload file name: upload.php The code is as follows:


$username="username";

$password= "User password";

$server="Host name";

$cdir="Upload directory name";

//Set your FTP host name above, Username and user password

?>







>

>

>

>

< table>



Upload file selection



















< /table>




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


//ftp connection host function

function connect()

{

global $server, $username, $password;

$conn = ftp_connect($server);

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

return $conn;

}

//Establish 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 pass it in binary bits

$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 value

This Function is used to upload the specified file. The parameter ftp_stream is the FTP connection code. The parameter remote_file is the name of the file to be stored remotely. The parameter local_file is the file name of the file to be uploaded. There are two values ​​of parameter mode: FTP_ASCII and FTP_BINARY, which represent documents or binary files respectively. Returns a true value on success and a false value on failure.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314237.htmlTechArticlePHP (Hypertext Preprocessor) is an HTML embedded language (similar to ASP on IIS). And PHP's unique syntax mixes C, Java, Perl, and new PHP-like syntax. It can be better than CGI or...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn