PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>Beginners> Using PHP to achieve file upload Law

Using PHP to achieve file upload Law

FROM: AUTHOR: TIME:2008-05-19 HITS:
PHP (Hypertext Preprocessor) is a built-in HTML language (similar to the IIS ASP). PHP syntax and the unique mixture of C, Java, Perl and PHP-the new grammar. It can be more than Perl CGI or the rapid implementation of dynamic pages. In addition, written with PHP the Web back-end CGI program, you can easily migrate to a different system platforms.
We are doing at the site, visitors need in the Senate will be even more eye-catching building site, which requires that we get visitors from the articles and pictures. Therefore, the file upload a page in the essential functions. Now I now popular on the use of the programming language PHP, used two methods to explain its function can be achieved.
First, the use of php file upload function to achieve
This code is divided into two documents, one for upload.html, is a upload.php.
Upload files choice: upload.html code is as follows:
<html> <head> <title> upload files form </ title> </ head>

phpsu.com is a free phpscool


<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="max_file_size" value="100000">
<center> Select documents:
<input name="userfile" type="file">
<input type="submit" value="上传文件">
</ Center>
</ Form>
</ Body>
</ Html>
Note *** ***
1, please note <form enctype = "multipart / form-data "......> This is a label, we are to achieve the upload documents to be designated as a multipart / form-data, or servers will not know you What is he doing!
2, it is worth noting that document in the form option MAX_FILE_SIZE upload.html hidden range, by setting
Its Value (value) may limit upload file size.
Just upload the file handle: upload.php code is as follows:
<html>
<head> http://www.phpsu.com
<title> Handling of documents containing </ title>
</ Head>
<body>
<?
Copy ($ userfile, "newfile");
echo $ userfile. "- users upload to a server on the temporary storage of paper the names of <br>";
echo $ userfile_name. "- on a user's machine original name of the document <br>";
echo $ userfile_size. "- upload the actual number of bytes <br>";
echo $ userfile_type. "- if the user's browser to provide this information, it said mime type. For example, image / gif <br>";
?>
</ Body>
</ Html>
Note *** ***
1, the use of documents PHP function of the copy () will be uploaded to a temporary directory under the file copied to a specific directory, and re-named as "newfile".
2, in upload.html in the definition of a variable userfile, in upload.php, we can use this variable, directly through the $ userfile visit on the set of documents, but because this is a variable save the file, file names must pass In addition to a $ userfile_name variables.
phpsu.com is a free phpscool

Below are the variables of the specific usage:
$ userfile: to be stored in files on the server containing the names of temporary files.
$ userfile_name: sender system in the initial file name.
$ userfile_size: bytes calculated by the upload file size.
$ userfile_type: multi-purpose Internet Mail agreement to expand the types of documents on the assumption that the browser to provide such information, for example, "image / gif".
Second, using FTP file upload function
The code also divided into two documents, one for upload.php, is a ftp.php.
Ftp settings related to the options and choose to upload the file name: upload.php code is as follows:
<? Php
$ Username = "user name";
$ Password = "user passwords";
$ Server = "host name";
$ Cdir = "upload directory name";
/ / Set up more than your FTP host name, user name and password
?>
<! - File upload settings tab ->

phpsu


<form enctype="multipart/form-data" action=ftp.php method=post>
<! - Variable transmission ->
<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 choice
<input Type=file name=upfile>
</ Td>
</ Tr>
<tr>
<td>
<! - Submit the form ->
<input type=submit name=action value=上传>
</ Td>
</ Tr>
</ Table>
</ Form>
Treatment upload: ftp.php code is as follows:
<? Php
/ / ftp host connectivity function
Function connect ()
{
Global $ server, $ username, $ password; phpsu.com is a free phpscool
$ Conn = ftp_connect ($ server);
Ftp_login ($ conn, $ username, $ password);
Return $ conn;
}
/ / Ftp to establish links
$ Result = connect ();
If ($ action == "upload")
{
/ / Ftp path to change
Ftp_chdir ($ result, $ cdir);
/ / Used to upload the specified documents, the same name and binary digital transmission
$ res_code = ftp_put ($ result, $ upfile_name, $ upfile, FTP_BINARY);
/ / Determine whether the right to upload
If ($ res_code == 1)
echo "Upload success!"
Else
echo "Upload wrong!"
}
/ / Close connection
Ftp_quit ($ result);
?>
Note *** ***
Function ftp_put (int ftp_stream, string remote_file, string local_file, int mode) Usage
Return value: boolean
This function used to upload specified documents. Ftp_stream parameters for the FTP connection code. Remote_file parameters for the remote to the existence of the file name. Local_file parameters for the document to upload the file name. Parameters mode and the value FTP_ASCII FTP_BINARY two kinds, namely that the document or a binary file. Successful return to the true value, then failed to return to false values.
TITLE:Using PHP to achieve file upload Law
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Fri Jun 6 22:58:41 GMT-8 2008