PHP file upload examples and parameters detailed _php example

Source: Internet
Author: User
Tags chmod error code html form php file upload

1. Upload Form upload.html

Program code
Html

Copy Code code 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 label, we want to implement file upload, must be designated as Multipart/form-data, otherwise the server will not know what to do.
2. It is noteworthy that the hidden value range of the form options max_file_size in file upload.html can limit the size of the uploaded file by setting its value (values).
The value of 3.max_file_size is only a suggestion to the browser, and it can actually be bypassed simply. So do not put a limit on the browser to that value. In fact, the maximum value of the uploaded file in the PHP settings will not expire. But it's best to add max_file_size to the form because it avoids the hassle of a user spending time waiting to upload a large file before discovering the file is too big.

PHP upload files involved in the parameters

Program code
Php

Copy Code code as follows:

$f =& $HTTP _post_files[' Myfile '];
$dest _dir= ' uploads '/set upload directory
$dest = $dest _dir. '/'. Date ("Ymd"). " _ ". $f [' name '];//set file name to date plus filename to avoid duplication
$r =move_uploaded_file ($f [' Tmp_name '], $dest);
chmod ($dest, 0755);//Set the properties of the uploaded file

Or

Copy Code code as follows:

<?copy ($_files[myfile][tmp_name],$_files[myfile][name]);? >

The contents of the $_files array in the example above are shown below. We assume that the File upload field name is UserFile (name can be arbitrarily named)

$_files[the original name of the ' UserFile ' [' Name '] client machine file.
The MIME type of the $_files[' userfile ' [' type '] file requires the browser to provide support for that information, such as "Image/gif".
$_files[' userfile ' [' size '] the size of the uploaded file, in bytes.
$_files[' UserFile ' [' tmp_name '] files are stored in the server-side temporary file name after being uploaded.
$_files[' userfile ' [' Error '] and the file upload related error code
Value: 0; No error occurred, file upload succeeded.
Value: 1; The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini.
Value: 2; The size of the upload file exceeds the value specified by the Max_file_size option in the HTML form.
Value: 3; The file is only partially uploaded.
Value: 4; No files were uploaded.

PHP default upload limit is the maximum 2M, want to upload more than this set of files, need to adjust PHP, Apache and some other parameters. Below, we briefly introduce the PHP file upload some of the parameters involved:

File_uploads
Whether to allow the switch to upload files over HTTP, default to on that is open

Upload_tmp_dir
Upload_tmp_dir used to describe the temporary directory of files placed in PHP upload, to upload files, you have to ensure that the server does not turn off temporary files and have write permissions on the folder, if not specified then PHP use system defaults

Upload_max_filesize
The maximum size allowed for uploading files, the default is 2M

Php

Copy Code code as follows:

<?php
Define (' Muilti_file_upload ', ' 10 '); Up to 10 files uploaded at the same time
Define (' max_size_file_upload ', ' 500000 '); File size not exceeding 5MB
Define (' File_upload_dir ', ' d:/'); directory where files are uploaded
File name allowed to upload
$array _extention_interdite = Array ('. php ', '. PhP3 ', '. PhP4 ', '. exe ', '. msi ', '. htaccess ', '. gz '); Upload file extension

Public functions to display information
function func_message ($message = ', $ok = ') {
Echo ' <table width= "100%" cellspacing= "0" cellpadding= "5" >;
if ($ok = = True) {
Echo ' <tr bgcolor= "#99FF99" ><td width= "M" > </td><td class= "Text" > '. $message. ' </td></tr> ';
}//Www.jb51.net
if ($ok = = False) {
Echo ' <tr bgcolor= "#FF99CC" ><td width= "M" > </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 '];
}
Modification of File name
$file _name_final = STRTR ($file _name_final, ' aaaaaa ', ' aaaaaaceeeeiiiiooooouuuuyaaaaaaceeeeiiiioooooouuuuyy ');
$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. = ' already 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 for
Break
}
?>
<title> multi-File upload </title>
<style>
. Border{background-color: #000000}
. Box{background-color: #f8f8f9;}
. text{color: #000000;
Font-family: Song body;
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> uploading files: <?php echo $nb;?></td> <td><input type= "file" Name= "file_<?php echo $nb;?>" ></td>
<td> new file name (including extension): <?php echo $nb;?> </td><td><input type= "text" Name= "file_name_<?php echo $nb?> "></td>
</tr>
<?php}?>
<tr>
&LT;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>
&LT;TD colspan= "2" align= "right" ><input type= "Submit" value= "can simultaneously upload <?php echo $nb-1;?> files" ></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>

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.