Using PHP to upload file two method

Source: Internet
Author: User
Tags copy file upload ftp connect php file php code string variable
Uploading PHP (hypertext preprocessor) is an HTML embedded language (similar to an ASP on IIS). PHP's unique syntax blends the new syntax of C, Java, Perl, and PHP. It can execute dynamic Web pages faster than CGI or Perl. In addition, the Web backend CGI program written in PHP can be easily ported to different system platforms.

When we do the website, we need visitors ' participation in order to make the website more eye-catching, which requires us to get articles, pictures, etc. from the visitors. As a result, file uploads are an essential feature of the Web page. Now I'm using PHP, the popular programming language, to illustrate the realization of its function in two ways.

First, the use of PHP file functions to achieve upload
This code is divided into two files, one for upload.html and one for upload.php.


Upload file selection: upload.html code as follows:


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


<body>

<form enctype= "Multipart/form-data" action= "upload.php" method= "POST" >

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

<center> Please select a file:

<input name= "UserFile" type= "File" >

<input type= "Submit" value= "Upload file" >

</center>

</form>

</body>



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

Description * * *

1, please note <form enctype= "Multipart/form-data" ......> This is a label, we want to implement file upload, must be designated as Multipart/form-data, otherwise the server will not know what you are doing!

2, note is the file upload.html in the form option max_file_size hidden range, by setting

Its value (value) can limit the size of the uploaded file.


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


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



<title> processing uploaded files </title>


<body>

?

Copy ($userfile, "NewFile");

echo $userfile. " -The name of the file that the user uploaded to the server is temporarily stored
";

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

echo $userfile _size. " -The actual number of bytes to upload the file
";

echo $userfile _type. " -If the user's browser provides this information, it represents the MIME type. such as Image/gif
";

?>

</body>



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

Description * * *

1. Use the php file function copy () Copy the files uploaded to the temp directory to a specific directory and rename it to "NewFile".

2, in the upload.html defined a variable userfile, in upload.php, we can use this variable, directly through the $userfile access to the uploaded file, but because this is a variable to save the file, so the file name must pass another $ Userfile_name variable is obtained.

Here is the specific use 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 system.

$userfile _size: The size of the uploaded file, measured in bytes.

$userfile _type: Multipurpose Internet Mail Extension Protocol type of file, provided that the browser provides such information, such as "Image/gif".



Second, the use of FTP function file upload
This code is also divided into two files, one for upload.php, and one for ftp.php.


Set FTP options and choose Upload file name: upload.php code is as follows:


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

<?php

$username = "User name";

$password = "User password";

$server = "host name";

$cdir = "Upload directory name";

Above set your FTP host name, username and user password

?>

<!--file Upload Settings tab-->

<form enctype= "Multipart/form-data" action=ftp.php method=post>

<!--transfer variable-->

<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> Upload File Selection

<input Type=file name=upfile>

</td>

</tr>

<tr>

<td>

<!--submitting Form-->

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

</td>

</tr>

</table>

</form>


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


Process upload file: ftp.php code as follows:


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

<?php

FTP Join host function

Function Connect ()

{

Global $server, $username, $password;

$conn = Ftp_connect ($server);

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

return $conn;

}

Establish an FTP join

$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 in bits

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

Determine if the correct upload

if ($res _code = = 1)

echo "Upload success!"

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. Parameter ftp_stream the connection code for FTP. Parameter remote_file is the name of the file that wants to exist remotely. Parameter local_file is the name of the file you want to upload. The value of parameter mode has two Ftp_ascii and Ftp_binary, representing the document or binary file, respectively. Success returns a value of true, and a false value if it fails.


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.