PHP File Upload examples and Parameters

Source: Internet
Author: User
Tags php file upload

1. Upload the form upload.html

Program code
HTML
Copy codeThe Code is as follows:
<Form enctype = "multipart/form-data" action = "upload. php" method = "post">
<Input type = "hidden" name = "max_file_size" value = "100000">
<Input name = "userfile" type = "file">
<Input type = "submit" value = "Upload File">
</Form>

1. note <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 to do.
2.The Value is the hidden Value range of the form option MAX_FILE_SIZE in the file upload.html. You can set the Value to limit the size of the uploaded file.
3. The value of MAX_FILE_SIZE is only a suggestion for the browser. In fact, it can be simply bypassed. Therefore, do not forward browser restrictions to this value. In fact, the maximum size of uploaded files in PHP settings will not expire. But it is best to add MAX_FILE_SIZE to the form, because it can avoid the trouble of finding the file too big after you wait for the upload of a large file.

Parameters Involved in file upload in PHP

Program code
PHP
Copy codeThe Code is as follows:
$ F = & $ HTTP_POST_FILES ['myfile'];
$ Dest_dir = 'uploads'; // sets the upload directory.
$ Dest = $ dest_dir. '/'. date ("ymd"). "_". $ f ['name']; // set the file name to date and add the file name to avoid duplication.
$ R = move_uploaded_file ($ f ['tmp _ name'], $ dest );
Chmod ($ dest, 0755); // sets the attributes of the uploaded file

Or
Copy codeThe Code is as follows:
<? Copy ($ _ FILES [MyFile] [tmp_name], $ _ FILES [MyFile] [name]);?>

In the preceding example, the content of the $ _ FILES array is as follows. Assume that the name of the file upload field is userfile)

$ _ FILES ['userfile'] ['name'] the original name of the client machine file.
$ _ FILES ['userfile'] ['type'] indicates the MIME type of the file, which must be supported by the browser, for example, "image/gif ".
$ _ FILES ['userfile'] ['SIZE'] size of the uploaded file, in bytes.
$ _ FILES ['userfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded.
$ _ FILES ['userfile'] ['error'] error Code related to the File Upload
Value: 0. If no error occurs, the file is uploaded successfully.
Value: 1; the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.
Value: 2; the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
Value: 3; only part of the file is uploaded.
Value: 4; no file is uploaded.

PHP's default upload limit is 2 MB. to upload files that exceed this setting, you need to adjust some parameters such as PHP and apache. next, we will briefly introduce some parameters involved in PHP file upload:

File_uploads
Whether to allow file upload over HTTP. ON is enabled by default.

Upload_tmp_dir
Upload_tmp_dir indicates the temporary directory where PHP files are uploaded. to upload files, make sure the server does not have the permission to close the temporary files and write the files to the folder. If not specified, PHP uses the system default value.

Upload_max_filesize
Maximum file size that can be uploaded. The default value is 2 MB.

PHP
Copy codeThe Code is as follows:
<? Php
Define ('muilti _ FILE_UPLOAD ', '10'); // a maximum of 10 files can be uploaded simultaneously.
Define ('max _ SIZE_FILE_UPLOAD ', '000000'); // the file size cannot exceed 5 MB.
Define ('file _ UPLOAD_DIR ', 'd:/'); // directory of the uploaded FILE
// File name that can be uploaded
$ Array_extention_interdite = array ('. php ','. php3 ','. php4 ', '.exe ','. msi ','. htaccess', '.gz '); // File Upload Extension

// Public functions for displaying information
Function func_message ($ message = '', $ OK = ''){
Echo '<table width = "100%" cellspacing = "0" cellpadding = "5"> ';
If ($ OK = true ){
Echo '<tr bgcolor = "#99FF99"> <td width = "100"> </td> <td class = "text"> '. $ message. '</td> </tr> ';
} // Www.jb51.net
If ($ OK = false ){
Echo '<tr bgcolor = "# FF99CC"> <td width = "100"> </td> <td class = "text"> '. $ message. '</td> </tr> ';
}
Echo '</table> ';
}
// Process form submission
$ Action = (isset ($ _ POST ['action'])? $ _ POST ['action']: '';
$ File = (isset ($ _ POST ['file'])? $ _ POST ['file']: '';
If ($ file! = ''){
$ File = $ file .'/';
}
$ Message_true = '';
$ Message_false = '';

Switch ($ action ){
Case 'upload ':
Chmod (FILE_UPLOAD_DIR, 0777 );
For ($ nb = 1; $ nb <= MUILTI_FILE_UPLOAD; $ nb ++ ){
If ($ _ FILES ['file _ '. $ nb] ['SIZE']> = 10 ){
If ($ _ FILES ['file _ '. $ nb] ['SIZE'] <= MAX_SIZE_FILE_UPLOAD ){
If (! In_array (ereg_replace ('^ [[: alnum:] ([-_.]? [[: Alnum:]) * \. ','. ', $ _ FILES ['file _'. $ nb] ['name']), $ array_extention_interdite )){
If ($ _ POST ['file _ name _ '. $ nb]! = ''){
$ File_name_final =$ _ POST ['file _ name _ '. $ nb]. $ extension;
} Else {
$ File_name_final = $ _ FILES ['file _ '. $ nb] ['name'];
}
// Modify the file name
$ File_name_final = strtr ($ file_name_final, 'aaaaa', 'aaaaaaceeeeiiiiooooouuuuyaaaaceeiiiioooouuuuyy ');
$ File_name_final = preg_replace ('/([^. a-z0-1] +)/I', '_', $ file _ name_final );

$ _ FILES ['file _ '. $ nb] ['name'] = $ file_name_final;
Move_uploaded_file ($ _ FILES ['file _ '. $ nb] ['tmp _ name'], FILE_UPLOAD _ DIR. $ file. $ file_name_final );

$ Message_true. = 'uploaded file: '. $ _ FILES ['file _'. $ nb] ['name']. '<br> ';
} Else {
$ Message_false. = 'file Upload Failed: '. $ _ FILES ['file _'. $ nb] ['name']. '<br> ';
}
} Else {
$ Message_false. = 'file size exceeds '. MAX_SIZE_FILE_UPLOAD/1000. 'kb :"'. $ _ FILES ['file _'. $ nb] ['tmp _ name']. '"<br> ';}
}
} // End
Break;
}
?>
<Html>
<Head>
<Title> Multifile upload </title>
<Style>
. Border {background-color: #000000}
. Box {background-color: # f8f8f9 ;}
. Text {color: #000000;
Font-family:;
Font-size: 12px;
Font-weight: bold}
Input, select {font-size: 12px ;}
Body {
Margin-top: 8px;
}
</Style>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> <Body marginwidth = "0" bottommargin = "0" leftmargin = "0" rightmargin = "0">
<Form name = "form" enctype = "multipart/form-data" method = "post" action = "<? Php echo $ _ SERVER ['php _ SELF '];?> ">
<Input type = "hidden" name = "action" value = "upload">
<Table border = "0" cellspacing = "1" cellpadding = "0" align = "center" class = "border">
<Tr>
<Td>
<? Php
If ($ message_true! = '') {Func_message ($ message_true, true );}
If ($ message_false! = '') {Func_message ($ message_false, false );}
?>
<Table width = "100%" border = "0" cellspacing = "5" cellpadding = "2" align = "center" class = "box">
<? Php
For ($ nb = 1; $ nb <= MUILTI_FILE_UPLOAD; $ nb ++ ){
?>
<Tr class = "text">
<Td> upload a file: <? Php echo $ nb;?> </Td> <input type = "file" name = "file _ <? Php echo $ nb;?> "> </Td>
<Td> New file name (including the extension): <? Php echo $ nb;?> </Td> <input type = "text" name = "file_name _ <? Php echo $ nb;?> "> </Td>
</Tr>
<? Php }?>
<Tr>
<Td colspan = "2" align = "right" class = "text"> upload destination address: <? Php echo FILE_UPLOAD_DIR;?>
<Select name = "file">
<Option value = ""> </option>
<? Php
$ Repertoire = opendir (FILE_UPLOAD_DIR );
While ($ file = readdir ($ repertoire )){
$ File = str_replace ('.', '', $ file );
If (is_dir ($ file )){
?>
<Option value = "<? Php echo $ file;?> "> <? Php echo $ file;?> /</Option>
<? Php
}
}
Closedir ($ repertoire );
?>
</Select>
</Td>
<Td colspan = "2" align = "right"> <input type = "submit" value = "can be uploaded simultaneously <? Php echo $ nb-1;?> Files "> </td>
</Tr>
</Table>
</Td>
</Tr>
</Table>
</Form>
</Body>
</Html>

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.